1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
|
// 69_QuickSort_NonRecursive.cpp : Defines the entry point for the console application.
//
/*
version: 1.0
author: hellogiser
blog: http://www.cnblogs.com/hellogiser
date: 2014/9/18
*/
#include "stdafx.h"
#include "iostream"
#include <ctime>
#include <algorithm>
#include <stack>
using namespace std;
void myswap(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
void print(int *a, int n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
//========================================================
// recursively
//========================================================
int partition1(int *a, int left, int right)
{
// a[left,...p-1]<=a[p]<a[p+1,...right]
int i = left;
int j = right;
int key = a[left];
while(i < j)
{
while(a[i] <= key) i++;
while(a[j] > key) j--;
if (i < j)
{
myswap(a[i], a[j]);
}
}
// left---j---i---right
myswap(a[left], a[j]);
return j;
}
int partition2(int *a, int left, int right)
{
// left, a[left+1...i]<=key, a[i+1,...j-1]>key
int i = left;
int j = left + 1;
int key = a[left];
while(j <= right)
{
if (a[j] < key)
{
i++; // so that a[i]>=key
myswap(a[i], a[j]);
}
j++;
}
//left---i---right---j
myswap(a[left], a[i]);
return i;
}
void quicksort_r(int *a, int left, int right)
{
if (left < right)
{
int p = partition1(a, left, right);
quicksort_r(a, left, p - 1);
quicksort_r(a, p + 1, right);
}
}
void QuickSort_Recursively(int *a, int n)
{
if (a == NULL || n <= 1)
return;
quicksort_r(a, 0, n - 1);
}
//========================================================
// non recursively
//========================================================
void quicksort_non_r(int *a, int left, int right)
{
// view [left,right] range as a node, which then has 2 child nodes [left,p-1] and [p+1,right] after partitioning
if (left >= right)
return;
// init stack with one range node
stack<int> s;
s.push(left);
s.push(right);
int temp_right, temp_left;
int p;
while(!s.empty())
{
temp_right = s.top();
s.pop();
temp_left = s.top();
s.pop();
if (temp_left < temp_right)
{
p = partition1(a, temp_left, temp_right);
// add node [left,p-1] and [p+1,right] to stack
s.push(temp_left);
s.push(p - 1);
s.push(p + 1);
s.push(temp_right);
}
}
}
void QuickSort_NonRecursively(int *a, int n)
{
if (a == NULL || n <= 1)
return;
quicksort_non_r(a, 0, n - 1);
}
typedef void (*fun)(int *a, int n);
void test_base(int *a, int n, fun f)
{
print(a, n);
f(a, n);
print(a, n);
cout << "==============================\n";
}
void test_recursively()
{
srand((unsigned int)time(NULL));
const int n = 10;
int a[n];
for (int i = 0; i < n; i++)
{
a[i] = rand() % 100;
}
cout << "recursively..." << endl;
test_base(a, n, QuickSort_Recursively);
}
void test_non_recursively()
{
srand((unsigned int)time(NULL));
const int n = 20;
int a[n];
for (int i = 0; i < n; i++)
{
a[i] = rand() % 100;
}
cout << "non recursively..." << endl;
test_base(a, n, QuickSort_NonRecursively);
}
void test_main()
{
test_recursively();
test_non_recursively();
}
int _tmain(int argc, _TCHAR *argv[])
{
test_main();
return 0;
}
/*
recursively...
43 5 68 83 74 12 80 61 84 48
5 12 43 48 61 68 74 80 83 84
==============================
non recursively...
43 5 68 83 74 12 80 61 84 48 67 95 57 2 17 17 98 94 17 76
2 5 12 17 17 17 43 48 57 61 67 68 74 76 80 83 84 94 95 98
==============================
*/
|