Gaby Ivanushka

来源:互联网 发布:通话声音分析软件 编辑:程序博客网 时间:2024/06/10 10:53



这道题就是快排的变形


#include<iostream>

using namespacestd;

void quickSort(int a[],int,int);

int main()

{

    int array[]={34,65,12,43,67,5,78,10,3,70},k;

    int len=sizeof(array)/sizeof(int);

    cout<<"The orginal arrayare:"<<endl;

    for(k=0;k<len;k++)

        cout<<array[k]<<",";

    cout<<endl;

    quickSort(array,0,len-1);

    cout<<"The sorted arrayare:"<<endl;

    for(k=0;k<len;k++)

        cout<<array[k]<<",";

    cout<<endl;

    system("pause");

    return0;

}


void quickSort(int s[],int l, int r)

{

    if(l < r){

        int p = l;

        int q = r;

        int key = s[l];

        while (p<q) {

            while(p < q && s[q] >= key){

                q--;

            }

            if(p < q){

                s[p++] = s[q];

            }

            while(p<q && s[p] < key){

                p++;

            }

            if(p < q){

                s[q--] = s[p];

            }

        }

        s[p] = key;

        quickSort(s,l,p-1);

        quickSort(s,q+1,r);

    

    }

}

原创粉丝点击