关于快速排序算法

1 篇文章 0 订阅
1 篇文章 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;   //这里数组下标为first 不是low
37     Qsort(a,low,first-1);//递归调用
38     Qsort(a,first+1,high);// 注意这里的first+1和 first-1
39 } 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值