首页 > 代码库 > 关于快速排序算法
关于快速排序算法
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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。