首页 > 代码库 > 关于快速排序算法

关于快速排序算法

 1 #include<iostream> 2 using namespace std; 3 void Qsort(int a[],int,int);  //注意声明格式 4 int main(){ 5      6     int a[]={57,68,59,52,72,28,96,33,24}; 7     for(int i=0;i<sizeof(a)/sizeof(a[0]);i++){//注意取数组长度的方法 8         cout<<a[i]<<" "; 9     }10     cout<<endl;11     Qsort(a,0,sizeof(a)/sizeof(a[0])-1);//12     for(int i=0;i<sizeof(a)/sizeof(a[0]);i++){13         cout<<a[i]<<" ";14     }15     return 0;16 }17 void Qsort(int a[],int low,int high){18     if(low>=high){//注意,递归的出口!19         return; 20     } 21     int first=low;//后面递归调用处要用到low,high22     int last=high;23     int key=a[first];24     while(first<last){25         while(first<last&&a[last]>=key){26             --last;27         }28         if(first<last)29             a[first]=a[last];//注意可改成a[first++]30         while(first<last&&a[first]<=key){//<=31             ++first;32         }33         if(first<last)34             a[last]=a[first];35     }36     a[first]=key;37     Qsort(a,low,first-1);//递归调用38     Qsort(a,first+1,high);39 }