快速排序(C/C++)
int partition(int arr[], int low, int high) { int i = low; int j = high; int base = arr[low]; if (low < high) { while (i < j) { while (i < j && arr[j] >= base) { j--; } if (i < j)//右边小于基数 { arr[i++] = arr[j]; //i++;//或arr[i++] = arr[j]; } while (i < j && arr[i] < base) { i++; } if (i < j)//左侧大于基数 { arr[j--] = arr[i]; //j--; } arr[i] = base; } } return i; } void QuickSort(int *arr, int low, int high) { if (low < high) { int index = partition(arr, low, high ); cout << index << endl; QuickSort(arr, low, index - 1); QuickSort(arr, index + 1, high); } } int main(void) { int arr[] = { 163, 161, 158, 165, 171, 170, 163, 159, 162 }; int len = sizeof(arr) / sizeof(arr[0]); QuickSort(arr, 0, len - 1); for (int i = 0; i < len; i++) { cout << arr[i] << "\t"; } return 0; }