不适用qsort(函数
时间: 2024-06-01 10:11:39 浏览: 28
)的快速排序程序示例(使用递归实现):
#include <stdio.h>
void quick_sort(int arr[], int left, int right) {
int i, j, pivot, tmp;
if (left < right) {
pivot = left;
i = left;
j = right;
while (i < j) {
while (arr[i] <= arr[pivot] && i < right)
i++;
while (arr[j] > arr[pivot])
j--;
if (i < j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = tmp;
quick_sort(arr, left, j - 1);
quick_sort(arr, j + 1, right);
}
}
int main() {
int arr[6] = {5, 3, 2, 4, 1, 6};
int i;
quick_sort(arr, 0, 5);
printf("Sorted array: ");
for (i = 0; i < 6; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
输出:
Sorted array: 1 2 3 4 5 6
阅读全文