快速排序

练练手用的。

#include<stdio.h>
void qsort(int a[],int low,int high)
{
        int i=low,j=high;
        int key=a[low];
        while(low<high)
        {
                while(low<high&&a[high]>key)
                        high--;
                a[low]=a[high];
                while(low<high&&a[low]<key)
                        low++;
                a[high]=a[low];
        }
        a[low]=key;
        if(i<low-1)
                qsort(a,i,low-1);
        if(j>low+1)
                qsort(a,low+1,j);
}
int main()
{
        int i;
        int a[6]={7,2,4,29,16,5};
        qsort(a,0,5);
        for(i=0;i<6;i++)
                printf("%d ",a[i]);
        printf("\n");
}



你可能感兴趣的:(快速排序)