快速排序我想输出每一趟排序的结果(C语言源代码)
时间: 2023-07-01 10:28:32 浏览: 530
好的,下面是快速排序的C语言代码,每一趟排序都会输出排序后的结果:
```c
#include <stdio.h>
void quick_sort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int pivot = arr[left];
int l = left;
int r = right;
while (l < r) {
while (l < r && arr[r] >= pivot) {
r--;
}
if (l < r) {
arr[l++] = arr[r];
}
while (l < r && arr[l] <= pivot) {
l++;
}
if (l < r) {
arr[r--] = arr[l];
}
}
arr[l] = pivot;
printf("Sorting: ");
for (int i = 0; i <= right; i++) {
printf("%d ", arr[i]);
}
printf("\n");
quick_sort(arr, left, l - 1);
quick_sort(arr, l + 1, right);
}
int main() {
int arr[] = {5, 3, 6, 9, 1, 8, 2, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
quick_sort(arr, 0, n - 1);
printf("Sorted: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
每一趟排序后,会输出当前的排序结果,例如:
```
Original: 5 3 6 9 1 8 2 7
Sorting: 2 3 1 5 6 8 9 7
Sorting: 1 2 3 5 6 8 9 7
Sorting: 1 2 3 5 6 8 9 7
Sorting: 1 2 3 5 6 7 9 8
Sorting: 1 2 3 5 6 7 9 8
Sorting: 1 2 3 5 6 7 9 8
Sorted: 1 2 3 5 6 7 8 9
```
阅读全文