100 20 10 7 8 9 1 5 6 3 2 4
1 3 2 4 8 9 100 5 6 20 10 71 3 2
1 2 38 9 100 5 6 20 10 7
5 6 7 8 9 20 10 1005 6
5 68 9 20 10 100
8 9 20 10 1008 9 20 10
8 9 10 208 9
8 9Sorted array:
1 2 3 4 5 6 7 8 9 10 20 100
PS F:\BC\2024\9\28> ./Main7
100 20 10 7 8 9 1 5
1 5 10 7 8 9 100 2010 7 8 9 100 20
10 7 8 9 20 10010 7 8 9
7 8 9 107 8
7 8Sorted array:
1 5 7 8 9 10 20 100
#include<iostream> using namespace std;void printArray(int arr[], int low, int high) {for (int i = low; i <= high; i++) {cout << arr[i] << " ";}cout << endl; }int partition(int arr[], int low, int high) { printArray(arr,low,high);int pivot = arr[high];// 选择最后一个元素作为基准int i = (low - 1);//小于基准的元素索引for (int j = low; j <= high - 1; j++) {if (arr[j] < pivot) {i++;swap(arr[i], arr[j]);//交换元素}}swap(arr[i + 1], arr[high]); // 将基准放到正确的位置printArray(arr,low,high);cout<<endl;return (i + 1);//返回基准的最终位置 }void quickSort(int arr[], int low, int high) {if (low < high) {int pi = partition(arr, low, high);// 获取分区点quickSort(arr, low, pi - 1);// 递归排序左子数组quickSort(arr, pi + 1, high);// 递归排序右子数组} }int main() {int arr[] = { 100,20,10, 7, 8, 9, 1, 5 };int n = sizeof(arr) / sizeof(arr[0]);//printArray(arr, 0, n - 1);quickSort(arr, 0, n - 1);cout << "Sorted array: \n";for (int i = 0; i < n; i++) {cout << arr[i] << " ";}return 0; }