-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.cpp
217 lines (173 loc) · 4.57 KB
/
QuickSort.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// T(n) -> O(n^2) : worst time
// O(n log n) : avg
/*
1) Divide & conquer algortithm
2) Recursive
3) in-place sorting
position p in such a way after reposition all elements in the left side should be lesser than P (sorted or unsorted)
all elements to to the right should be greater than P
we do that by taking the pivot
we begin from start 0
iterate from start to end - 1 // because pivot is at end already
compare A[i] to pivot
while comparing
well see :
for i i < end -1 i ++
if (A[i]<= A[P])
{
Swap (A[i], A[pIndex])
pIndex = pIndex + 1
}
Swap(A[pIndex], A[P)])
S E
0 1 2 3 4 5 6
A = [89,45,68,90,29,34,50] i = 0
||i |P
|pIndex
[45,89,68,90,29,34,50] i = 1
| |i |P
|pIndex
swap
[45,89,68,90,29,34,50] i = 2
| |i |P
|pIndex
[45,89,68,90,29,34,50] i = 3
| |i |P
|pIndex
[45,89,68,90,29,34,50] i = 4
| |i |P
|pIndex
swap
//after swap increment p index
[45,29,34,90,89,68,50] i = 5
| |i|P
|pIndex
end:swap pIndex with P
[45,29,34,50,89,68,90] i = 5
| |i|P
|pIndex
P = pivot
i = 0
compare a[i]= 89 to a[P] = 50
A[i] !<= A[P]
increment i
i = 1
compare A[i] = 45 to A[P] = 50
(since) 45 <= 50 (is true)
Swap (A[i] = 45 with A[pIndex])
increment pIndex to 1
compare A[i] = 89 <= A[P] = 50
increment i
i = 2
compare A[i] = 68 to A[P] = 50
increment i
i = 3
compare A[i] = 90 to A[P] = 50
increment i
i = 4
compare A[i] = 29 to a[P] = 50
29<=50 T
Swap (A[pIndex] with A[i])
increment pIndex to 2
compare A[i] = 89 to A[P] = 50
pIndex always points at element bigger than Pivot
increment i
i = 5
compare A[i] = 34 to a[P] = 50
swap (a[pIndex] = 68 with a[i] = 34)
increment pIndex to 3
finally
after loop Swap A[pIndex] and A[P]
now P is in the new location
after partioning [45,29,34,|50|,89,68,90]
sorted [29,34,45,|50|,68,89,90]
[45,29,34,50,89,68,90]
| | | |
________ ________
pI P "" ""
2 new arrays, take a pivot P for each
take pindex and do the whole thing for them
1) select a pivot P eg: the last element
2) rearrange the list sucth that
a[i] <= A[P] && a[i] >= A[P]
all the elements on the left of P are smaller than P
all the elements on the right of P are greater than P
3) exchange the A[P] with the first element of the second sub array
Swap(A[pIndex],A[P])
4) sort the two sub_arra recursively
pseudo -code
partition(array A, start,end)
{
int P;
int pIndex;
P = end
pIndex = start
for(i = start i < end-1, i++)
{
if(A[i]<=A[P])
{
Swap(A[i],A[pIndex])
pIndex = pIndex+1
}
}
Swap(A[P],A[pIndex])
return pIndex;
}
quicksort(array A,int left,int right)
{
if(left < right)
{
int P = partition(A,left,right)
quicksort(array A, left,p-1)
quicksort(array A, p +1,right)
}
}
7
9 6
10 3 5 8
*/
#include <iostream>
#include "eduardoaparicio_displayVector.h"
#include "eduardoaparicio_swap.h"
#include <vector>
using namespace std;
int partition(vector<int>& A,int start,int end)
{
int P;
int pIndex;
P = end;
pIndex = start;
for (int i = start; i < end ; i++) //i <= end - 1
{
if (A[i] <= A[P])
{
swap(A[i], A[pIndex]);
pIndex = pIndex + 1;
}
}
swap(A[pIndex], A[P]);
return pIndex;
}
void quicksort(vector <int>& A, int left, int right)
{
if (left < right)
{
int P = partition( A, left, right);
quicksort(A, left, P - 1);
quicksort(A, P + 1, right);
}
}
int main()
{
vector<int> list = {15,1,200,999,2,3};
displayVector(list);
int left = 0;
int right = (list.size()-1);
cout << left << endl;
cout << right << endl;
quicksort(list, left, right);
displayVector(list);
cout << right << endl;
displayVector(list);
return 0;
}