国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ 中快排的遞歸和非遞歸實現

2020-01-26 14:05:09
字體:
來源:轉載
供稿:網友

快排的遞歸

void quickSort1(int* root,int low,int high){ int pat=root[low]; if(low<high) { int i=low,j=high; while(i<j) {   while(i<j&&root[j]>pat)  j--;  root[i]=root[j];  while(i<j&&root[i]<pat)  i++;  root[j]=root[i]; } root[i]=pat; quickSort1(root,low,i-1); quickSort1(root,i+1,high); } }

快排的非遞歸

int partion(int* root,int low,int high){ int part=root[low]; while(low<high) { while(low<high&&root[high]>part) high--; root[low]=root[high]; while(low<high&&root[low]<part) low++; root[high]=root[low]; } root[low]=part; return low;}void quickSort2(int* root,int low,int high){ stack<int> st; int k; if(low<high) { st.push(low); st.push(high); while(!st.empty()) {  int j=st.top();st.pop();  int i=st.top();st.pop();  k=partion(root,i,j);  if(i<k-1)  {  st.push(i);  st.push(k-1);  }  if(k+1<j)  {  st.push(k+1);  st.push(j);  } } } }int main(){ int a[8]={4,2,6,7,9,5,1,3}; quickSort1(a,0,7); //quickSort2(a,0,7); int i; for(i=0;i<8;i++) cout<<a[i]<<" "; cout<<endl; getchar(); return 0;}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 水城县| 应城市| 屏东市| 新源县| 县级市| 蒲江县| 七台河市| 鱼台县| 丰镇市| 玛多县| 馆陶县| 芜湖县| 肇东市| 绥阳县| 莱州市| 闽清县| 额尔古纳市| 海丰县| 靖江市| 左云县| 清新县| 陈巴尔虎旗| 龙井市| 泸定县| 杨浦区| 十堰市| 博兴县| 思茅市| 宁城县| 太仓市| 宝应县| 定州市| 榕江县| 扶绥县| 吉木萨尔县| 彭水| 三台县| 嘉峪关市| 清河县| 锡林郭勒盟| 墨竹工卡县|