快速排序

来源:互联网 发布:手机电脑连接软件 编辑:程序博客网 时间:2024/04/30 23:38
#include <iostream>using namespace std;const int MAX = 10;int swap(int *a,int *b){    int tmp;    tmp=*b;    *b = *a;    *a = tmp;    return 1;}int swap_pro(int &a,int &b){    int tmp;    tmp=b;    b = a;    a = tmp;    return 1;}int Partition(int *R ,int low, int high){    int i=low;    int j=high;    int pivot = R[low];    while(i<j)    {        while(i<j&&R[j]>pivot)            j--;        if(i<j)        {            swap_pro(R[i],R[j]);            i++;        }        while(i<j&&R[i]<=pivot)            i++;        if(i<j)        {            swap_pro(R[i],R[j]);            j--;        }    }    return j;}void QuickSort(int *R,int low, int high){    int pivot;    if(low<high)    {        pivot =Partition(R,low,high);        QuickSort(R,low,pivot-1);        QuickSort(R,pivot+1,high);    }}int main(){/*    10    3 4 8 5 9 2 6 3 1 8    */    int ext=1;    int count=0;    while(ext!=0)    {        cout<<"Please input Count of number:"<<endl;        cin>>count;        cout<<"Please input data(X count):"<<endl;        int R[count];        int index=0;        while(index<count)        {            cin>>R[index];            index++;        }        QuickSort(R,0,count);        cout<<"The result:"<<endl;        int i=0;        while(i<count)        {            cout <<R[i]<<endl;            i++;        }        cout<<"Input 0 to exit,else continue"<<endl;         cin>>ext;    }    /*    *test code    */    /*int R[MAX]={5,1,4,6,3,9,2,7,4,8};    QuickSort(R,0,MAX);    int i=0;    while(i<MAX)    {    cout <<R[i]<<endl;    i++;    }*/    /*cout<<"a="<<a[i]<<endl;    cout<<"b="<<b[i]<<endl;    cout << "Hello world!" << endl;*/    return 0;}

原创粉丝点击