快速排序代码 c语言
时间: 2023-11-19 16:57:06 浏览: 121
以下是C语言实现的快速排序代码:
```c
#include <stdio.h>
void quick_sort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right, pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
arr[i] = arr[j];
while (i < j && arr[i] <= pivot) {
i++;
}
arr[j] = arr[i];
}
arr[i] = pivot;
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
int main() {
int arr[] = {5, 2, 8, 4, 7, 1, 9, 3, 6};
int len = sizeof(arr) / sizeof(arr[0]);
quick_sort(arr, 0, len - 1);
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
阅读全文