
#include#include #include #define pi 3.14 typedef long long LL; using namespace std; int Partition(int a[],int l,int r){ int temp=a[l]; while(l temp) r--; a[l]=a[r]; while(l 以上是快速排序的递归实现,那么如何使用非递归实现呢?
递归转非递归首先想到的就是栈这个数据结构!!
很简单!
void quicksort2(int* a,int l,int r){ stacks; s.push(l); s.push(r); while(!s.empty()){ int r=s.top(); s.pop(); int l=s.top(); s.pop(); int p= Partition(a,l,r); if((p-1)>l){ s.push(l); s.push(p-1); } if((p+1) 记得引用头文件
#include欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)