快速排序的非递归实现代码
来源:互联网 发布:珂润洗发水 知乎 编辑:程序博客网 时间:2024/06/05 16:28
#include <iostream>
#include <stack>
using namespace std;
int partition(int * p,int a,int b);
void QuickSort(int * p,int size)
{
stack<int> s;
int low=0,high=size-1;
while(!s.empty()||low<high)
{
while(low<high)
{
int t=partition(p,low,high);
s.push(t);
high=t-1;
}
int t=s.top();
s.pop();
low=t+1;
if(s.empty())
high=size-1;
else
high=s.top()-1;
}
}
int partition(int * p,int a,int b)
{
int t=p[a];
int m=a,n=b;
while(m<n)
{
while(p[n]>=t&&m<n)
{
n--;
}
p[m]=p[n];
while(p[m]<t&&m<n)
{
m++;
}
p[n]=p[m];
}
p[m]=t;
return m;
}
void main()
{
int size=0;
cin>>size;
int * p=new int[size];
for(int i=0;i!=size;++i)
{
cin>>p[i];
}
QuickSort(p,size);
for(int i=0;i!=size;++i)
cout<<p[i]<<" ";
::system("pause");
}
#include <stack>
using namespace std;
int partition(int * p,int a,int b);
void QuickSort(int * p,int size)
{
stack<int> s;
int low=0,high=size-1;
while(!s.empty()||low<high)
{
while(low<high)
{
int t=partition(p,low,high);
s.push(t);
high=t-1;
}
int t=s.top();
s.pop();
low=t+1;
if(s.empty())
high=size-1;
else
high=s.top()-1;
}
}
int partition(int * p,int a,int b)
{
int t=p[a];
int m=a,n=b;
while(m<n)
{
while(p[n]>=t&&m<n)
{
n--;
}
p[m]=p[n];
while(p[m]<t&&m<n)
{
m++;
}
p[n]=p[m];
}
p[m]=t;
return m;
}
void main()
{
int size=0;
cin>>size;
int * p=new int[size];
for(int i=0;i!=size;++i)
{
cin>>p[i];
}
QuickSort(p,size);
for(int i=0;i!=size;++i)
cout<<p[i]<<" ";
::system("pause");
}
- 快速排序的非递归实现代码
- 快速排序递归与非递归代码实现与思考
- 快速排序的递归和非递归实现 -----C++、JAVA代码实现
- 快速排序的非递归实现
- php的非递归快速排序实现
- 非递归的快速排序实现(转)
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序算法的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- cygwin常用命令大全
- Pull error. No value for key branch.master.merge found in configuration
- 构建高性能的 HTTP 服务器 (一)--初涉Java HTTP 服务器
- linux下vi命令大全
- linux中查看指定进程socket连接数的指令
- 快速排序的非递归实现代码
- shaderlab 关键字
- apache host 泛域名解析
- flash与游戏笔记:滚动数字
- 趣文:程序员的进化史
- 从零开始学C++之构造函数与析构函数(三):深拷贝与浅拷贝、空类与空数组
- cuzy sdk之起源
- 谈一个软件工程师创业的前期投资和成功的可能性
- DLL创建和调用