-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSorter.py
More file actions
68 lines (53 loc) · 2.08 KB
/
QuickSorter.py
File metadata and controls
68 lines (53 loc) · 2.08 KB
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
from Sorter import Sorter
class QuickSorter(Sorter):
def sort(self, data):
self.quick_sort_iterative(data, 0, 63)
# sorts a partition
def partition(self, data, starting_index, ending_index):
i = (starting_index - 1)
x = data.my_list[ending_index]
for j in range(starting_index, ending_index):
if data.my_list[j].height <= x.height:
# increment index of smaller element
i = i + 1
data.my_list[i], data.my_list[j] = data.my_list[j], data.my_list[i]
self.force_update()
data.my_list[i + 1], data.my_list[ending_index] = data.my_list[ending_index], data.my_list[i + 1]
self.force_update()
return i + 1
# data: Array to be sorted
def quick_sort_iterative(self, data, starting_index, ending_index):
# Create an auxiliary stack
size = ending_index - starting_index + 1
stack = [0] * size
# initialize top of stack
top = -1
# push initial values of l and h to stack
top = top + 1
stack[top] = starting_index
top = top + 1
stack[top] = ending_index
# Keep popping from stack while is not empty
while top >= 0:
# Pop h and l
ending_index = stack[top]
top = top - 1
starting_index = stack[top]
top = top - 1
# Set pivot element at its correct position in
# sorted array
p = self.partition(data, starting_index, ending_index)
# If there are elements on left side of pivot,
# then push left side to stack
if p - 1 > starting_index:
top = top + 1
stack[top] = starting_index
top = top + 1
stack[top] = p - 1
# If there are elements on right side of pivot,
# then push right side to stack
if p + 1 < ending_index:
top = top + 1
stack[top] = p + 1
top = top + 1
stack[top] = ending_index