c语言实现快速排序完整代码
时间: 2023-11-20 17:58:49 浏览: 89
以下是C语言实现快速排序的完整代码:
```
#include <stdio.h>
void quicksort(int a[], int first, int end);
int main() {
int arr[10] = {9, 5, 3, 8, 1, 2, 6, 7, 4, 10};
printf("排序前:");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
quicksort(arr, 0, 9);
printf("排序后:");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
void quicksort(int a[], int first, int end) {
if (first >= end) {
return;
}
int i = first, j = end, flag = a[first], exchange = 0;
while (i < j) {
while (i < j && a[j] >= flag) {
j--;
}
while (i < j && a[i] <= flag) {
i++;
}
if (i < j) {
exchange = a[i];
a[i] = a[j];
a[j] = exchange;
}
}
a[first] = a[i];
a[i] = flag;
quicksort(a, first, i - 1);
quicksort(a, i + 1, end);
}
```
阅读全文