快速排序样例

将《C程序设计(谭浩强著)》的快速排序样例做了小更改。

int partition(int* array,int low,int high)
{
    int temp_1=array[low];
    while(low<high)
    {
        while(low<high&&array[high]>=temp_1)
            high--;
        if(low<high) 
        {
            array[low]=array[high];
            low++;
        }
        while(low<high&&array[low]<temp_1)
            low++;
        if(low<high)
        {
            array[high]=array[low];
            high--;
        }
    }
    array[low]=temp_1;
    return low;
}
void quick_sort(int* array,int s,int t)
{   
    int i=0;
    if(s<t)
    {
        i=partition(array,s,t);
        quick_sort(array,s,i-1);
        quick_sort(array,i+1,t);
    }
}
int* main(int* nums,int numsSize)
{
     quick_sort(nums,0,numsSize-1);
     return nums;    
}

 qsort()是编译器函数库自带的快速排序函数。

1 //比如:对一个长为1000的数组进行排序时,int a[1000]; 那么base应为a,
2 //num应为 1000,width应为 sizeof(int),comp函数随自己的命名。
3 qsort(a,1000,sizeof(int),comp);
4 int comp(const void*a,const void*b)
5 {
6     return *(int*)a-*(int*)b;
7 }
8 //上面是由小到大排序,return *(int *)b - *(int *)a; 为由大到小排序。

 

posted @ 2017-11-08 21:44  Blue_Keroro  阅读(355)  评论(0编辑  收藏  举报