码迷,mamicode.com
首页 > 其他好文 > 详细

关于快速排序算法

时间:2014-07-02 22:29:34      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   算法   for   

 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

关于快速排序算法

标签:style   blog   color   os   算法   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!