#includeint CocktaiSort(int *array, int len) { if (array == NULL) return -1; int i = 0; int j = 0; int temp = 0; for (i = 0; i < len / 2; i++) { int isSorted = 1; for (j = i; j < len - i - 1; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; isSorted = 0; } } if (isSorted) { break; } // 偶数轮,从由右到左进行比较和交换 isSorted = 1; for (j = len - i - 1; j > i; j--) { if (array[j] < array[j - 1]) { temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; isSorted = 0; } } if (isSorted) { break; } } return 0; } int main() { int i = 0; int array[8] = {2, 3, 4, 5, 6, 7, 8, 1}; CocktaiSort(array, 8); for (i = 0; i < 8; i++) { printf("%d ", array[i]); } return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)