#include <stdio.h>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int 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]);
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);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr1[] = { 10, 7, 8, 9, 1, 5 };
int n1 = sizeof(arr1) / sizeof(arr1[0]);
printf("排序前的数组: ");
printArray(arr1, n1);
quickSort(arr1, 0, n1 - 1);
printf("排序后的数组: ");
printArray(arr1, n1);
int arr2[] = {8,2,5,23,11,21,7 };
int n2 = sizeof(arr2) / sizeof(arr2[0]);
printf("排序前的数组: ");
printArray(arr2, n2);
quickSort(arr2, 0, n2 - 1);
printf("排序后的数组: ");
printArray(arr2, n2);
int arr3[] = { 5,7,1,12,31,11 };
int n3 = sizeof(arr3) / sizeof(arr3[0]);
printf("排序前的数组: ");
printArray(arr3, n3);
quickSort(arr3, 0, n3 - 1);
printf("排序后的数组: ");
printArray(arr3, n3);
return 0;
}