-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicksort2.cpp
132 lines (106 loc) · 2.8 KB
/
quicksort2.cpp
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
// {"category": "Sort", "notes": "Quicksort (non-recursive)"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <stack>
#include <Windows.h>
using namespace std;
//------------------------------------------------------------------------------
//
// Quicksort
//
//------------------------------------------------------------------------------
template<class Object>
void quicksort(Object objects[], int start, int end);
template<class Object>
void median3(Object objects[], int start, int end);
template<class Object>
int partition(Object objects[], int start, int end);
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
template<class Object>
void quicksort(Object objects[], int start, int end)
{
struct Span
{
int start;
int end;
Span(int s, int e) : start(s), end(e) {}
};
stack<Span> spans;
spans.push(Span(start, end));
while (!spans.empty())
{
start = spans.top().start;
end = spans.top().end;
spans.pop();
if (start < end)
{
int pivot = partition(objects, start, end);
spans.push(Span(pivot + 1, end));
spans.push(Span(start, pivot - 1));
}
}
}
template<class Object>
void median3(Object objects[], int start, int end)
{
int center = (start + end) / 2;
if (objects[start] > objects[center])
{
swap(objects[start], objects[center]);
}
if (objects[start] > objects[end])
{
swap(objects[start], objects[end]);
}
// Place pivot at the end.
if (objects[end] > objects[center])
{
swap(objects[end], objects[center]);
}
}
template<class Object>
int partition(Object objects[], int start, int end)
{
int indexPivot = start;
median3(objects, start, end);
for (int i = start; i <= end; i++)
{
// Pivot is at the end.
if (objects[i] < objects[end])
{
if (i != indexPivot)
{
swap(objects[indexPivot], objects[i]);
}
++indexPivot;
}
}
if (indexPivot != end)
{
// Restore pivot.
swap(objects[indexPivot], objects[end]);
}
return indexPivot;
}
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
int numbers[] = { 13, 81, 92, 43, 65, 31, 57, 26, 75, 0 };
quicksort(numbers, 0, ARRAYSIZE(numbers) - 1);
for (int i = 0; i < ARRAYSIZE(numbers); i++)
{
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}