实现快速排序的非递归

来源:互联网 发布:2016乒超联赛网络 编辑:程序博客网 时间:2024/06/08 18:52
实现快速排序的非递归

关于排序的其他算法如冒泡、插入、希尔等排序方法见:http://blog.csdn.net/step_ma/article/details/74115184
这里主要用非递归实现快速排序,化为子区间时可采用左右指针法、挖坑法和前后指针法,这里测的是挖坑法,其他两种均可实现,关于各种 主要是利用栈实现。
代码实现:
#include #include #include using namespace std;int PartSort2(int *a, int start, int end)          //挖坑法{//int a[] = { 3, 1, 4, 9, 5, 8, 3, 0, 2 };int key = a[end];while (start < end){while (start < end && a[start] <= key){++start;}if (start < end){a[end] = a[start];}while (start < end && a[end] >= key){--end;}if (start < end){a[start] = a[end];}}a[start] = key;return start;}void QuickSort(int* a, int left, int right)             //非递归实现,利用栈{stack t;if (leftleft)                           {t.push(left);t.push(div - 1);}if (div + 1l){t.push(l);t.push(div - 1);}if (div + 1
测试结果:

原创粉丝点击