1,void quicksort(int *a,int low,int high)这个函数是排序的递归部分,mid就是已经确定的基准元素的位置。
2,int partition(int *a,int low,int high)这个函数干了两件事
1、挑出来一个基准元素(这里选的是最后一个作为基准),找它的正确的位置,把它赋给该位置,并返回该位置的坐标
2、把所有比基准元素小的挪到他前面,比基准大的挪到他后面
#include <stdio.h> #include <stdlib.h> int partition(int *a,int low,int high) { int x = a[high]; int i,j,t; for(i = j = low;j < high;j++) { if(a[j] < x) { t = a[j]; a[j] = a[i]; a[i] = t; i++; } } a[high] = a[i]; a[i] = x; return i; } void quicksort(int *a,int low,int high) { int mid; if(low < high) { mid = partition(a,low,high); quicksort(a,low,mid-1); quicksort(a,mid+1,high); } } int main(void) { int a[100],n,i; printf("输入n:"); scanf("%d",&n); printf("输入n个数字:"); for(i=0;i<n;i++) scanf("%d",&a[i]); quicksort(a,0,n-1); //调用 for(i=0;i<n;i++) printf("%d ",a[i]); qsort(); system("pause"); return 0; }