#include "stdio.h"
void quick_sort(int a[], int left, int right) {
if (left >= right) {
return;
}
int mid = partion(a, left, right);
quick_sort(a, left, mid - 1);
quick_sort(a, mid + 1, right);
}
// 将小于 pivot 的元素移到左边,将大于 pivot 的元素移到右边
int partion(int a[], int left, int right) {
int pivot = a[left];
while(left < right) {
while(left < right && a[right] >= pivot) {
right--;
}
a[left] = a[right];
while(left < right && a[left] <= pivot) {
left++;
}
a[right] = a[left];
}
a[left] = pivot;
return left;
}
int main()
{
int a[100];
int len;
int i;
printf("输入元素个数:");
scanf("%d", &len);
for(i=0; i<len; i++)
scanf("%d", &a[i]);
quickSort(a, 0, len-1);
for(i=0; i<len; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
快速排序 -- C 语言
于 2015-10-10 22:20:03 首次发布