国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤山县| 清丰县| 家居| 灵台县| 彰化市| 西林县| 来凤县| 沂水县| 台南县| 屯门区| 尚志市| 绥化市| 建宁县| 黔江区| 洛扎县| 镇原县| 南溪县| 八宿县| 贺兰县| 岳池县| 阳春市| 浙江省| 油尖旺区| 永新县| 尖扎县| 牡丹江市| 西青区| 宜春市| 芜湖县| 栖霞市| 宁强县| 三都| 龙岩市| 泰宁县| 岢岚县| 高阳县| 三原县| 扶沟县| 资阳市| 大冶市| 建湖县|