qSort implemented in recursion and non-recuision version
来源:互联网 发布:ubuntu更新软件的方法 编辑:程序博客网 时间:2024/05/16 07:56
#include<stdio.h>#include<stack>using namespace std;int a[10000];struct Node{int l;int r;};stack<Node> myStack; // for non-recursion version qSort()// no-recursion versionvoid qSort(int l, int r){Node first;first.l = l;first.r = r;myStack.push(first);while(!myStack.empty()){ // stored in stackNode cNode = myStack.top();myStack.pop();int pl = cNode.l;int pr = cNode.r;if(pl >= pr) continue;// one times quick sortint tmp = a[pl];while(pl < pr){while(a[pr]>=tmp && pl<pr) pr--;a[pl] = a[pr];while(a[pl]<=tmp && pl<pr) pl++;a[pr] = a[pl];}a[pl] = tmp;Node lNode;lNode.l = cNode.l;lNode.r = pl-1;myStack.push(lNode);Node rNode;rNode.l = pr + 1;rNode.r = cNode.r;myStack.push(rNode);} // end of while} // end of qSort()// recursion versionvoid qqSort(int l, int r){if(l >= r)return;int pl = l;int pr = r;// one times quick sortint tmp = a[pl];while(pl < pr){while(a[pr]>=tmp && pl<pr) pr--;a[pl] = a[pr];while(a[pl]<=tmp && pl<pr) pl++;a[pr] = a[pl];}a[pl] = tmp;qqSort(l, pl-1);qqSort(pr+1, r);}// end of qqSort()int main(){int i, n;scanf("%d", &n);for(i=0; i<n; i++){scanf("%d", &a[i]);}qSort(0, n-1); // non-recursion version //qqSort(0, n-1); // recursion version// output the array after sortedfor(i=0; i<n; i++){if(i>0) printf(" ");printf("%d", a[i]);}printf("\n");return 0;}
0 0
- qSort implemented in recursion and non-recuision version
- MatrixMultiply(recursion、non-recursion)
- Class _NSZombie_xxxxxxx is implemented in both ?? and?
- About sort and qsort in c
- Tail recursion and normal recursion
- IDENTIFYING AND NON-IDENTIFYING RELATIONSHIPS in Database
- recursion in jsp
- recursion in prolog
- FileDirectory And Recursion
- asymptotic notation and recursion
- Exhaustive recursion and backtracking
- iteration and recursion
- [Dynamic Programming]Recursion/Non-recursion to find the max number
- objc[2213]: Class _NSZombie_BSXPCMessage is implemented in both ?? and ??. One of the two will be us
- objc[2213]: Class _NSZombie_BSXPCMessage is implemented in both ?? and ??. One of the two will be us
- 【归并排序(非递归)】MergeSort(non-recursion)
- 迷宫(non-recursion)(即栈实现)
- [Algorithm]九章必背程序--stack:Non Recursion
- DLL 实现单元
- 【unix网络编程】4-11显示客户IP地址和端口号的时间获取服务程序
- Codeforces 443A Anton and Letters(水题)
- leetcode:Remove Duplicates from Sorted List
- Android笔记之 Web Service 基础
- qSort implemented in recursion and non-recuision version
- Codeforces 19D Points 线段树+set
- The Problems of zoj By watashi
- (转)logback 配置详解(一)
- 如何判断两个链表相交及找到第一个相交点
- 关于S3C2440de6410的CPU,关于FPU方面的配置
- IO(二)
- 回顾一下著名的BigTable论文
- 每天更新win8 win8.1 office2013 在线激活密钥 - www.yishimei.cn