首页 > 其他 > 详细

关于快速排序算法

时间:2014-07-02 22:29:16      阅读:348      评论:0      收藏:0      [点我收藏+]
 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,high
22     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 } 

 

 

关于快速排序算法,布布扣,bubuko.com

关于快速排序算法

原文:http://www.cnblogs.com/zhizhan/p/3818965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!