快排非递归实现
来源:互联网 发布:大数据毕业摄影 编辑:程序博客网 时间:2024/05/29 17:41
int partition(int* arr, int low, int high)
{int pivot = arr[low];
while(low < high)
{
while(low < high && arr[high] >= pivot)
high--;
arr[low] = arr[high];
while(low < high && arr[low] <= pivot)
low--;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
void non_recursive_qsort(int* arr, int low, int high)
{
stack<int> s;
int pivot;
if(low < high)
return ;
pivot = partition(arr, low, high)
if(low < pivot - 1)
{
s.push(low);
s.push(pivot - 1);
}
if(high > pivot + 1)
{
s.push(high);
s.push(pivot + 1);
}
//其实就是用栈保存每一个待排序子串的首尾元素下标,下一次while循环时取出这个范围,对这段子序列进行partition操作
//如果pivot的左段或右段已经少于两个元素了,则无需再对这个子段partition了
while(!s.empty())
{
high = s.top();
s.pop();
low = s.top();
s.pop();
pivot = partition(arr, low, high)
if(low < pivot - 1)
{
s.push(low);
s.push(pivot - 1);
}
if(high > pivot + 1)
{
s.push(high);
s.push(pivot + 1);
}
}
}
0 0
- 快排非递归实现
- 快排非递归实现
- 递归实现
- c++实现递归的递归
- 八皇后递归实现
- 存储过程实现递归
- 使用freemarker实现递归
- sql 实现递归算法
- sqlServer2005递归的实现
- 递归实现数字排列组合
- 递归实现itoa函数
- 用递归实现排列组合
- 递归实现节点查询
- 斐波拉挈 递归实现
- 递归实现CSS菜单
- 递归实现字符串换行
- 用bom实现递归
- C#递归实现Treeview
- 关于typedef的讨论
- DIV+CSS布局问题,让两个DIV标签并排
- 今天学了新内容 JavaScript
- Android下载图片 图片的异步加载 和缓存存取
- dataTables 用法荟萃
- 快排非递归实现
- 自定义控件(四)自定义点击效果Button
- Button鼠标箭头变手势
- SVN版本控制工具的权限设置
- android获取目录文件values/arrays.xml数据的方法,简易快捷
- BootStrap进度条
- hdu 5360 Hiking
- 使用ssh-keygen生成私钥和公钥
- 用Cygwin在Windows上架设NFS Server