-
Notifications
You must be signed in to change notification settings - Fork 4
/
sort.py
232 lines (176 loc) · 5.57 KB
/
sort.py
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from typing import List, Union
from random import randint
def bubble_sort(arr : List , simulation : bool = False) -> List:
"""Sorts A List using bubble sort algorithm
https://en.wikipedia.org/wiki/Bubble_sort
Worst-case performance: O(N^2)
Returns:
arr(List) : Returns sorted List
"""
def swap(i : int, j : int) -> None:
"""Swaps two element of List
Parameters:
i(int) : index of first element
j(int) : index of second element
Returns:
None : Function returns nothing
"""
arr[i], arr[j] = arr[j], arr[i]
n : int = len(arr)
swapped : bool = True
iteration : int = 0
if simulation:
print("iteration",iteration,":",*arr)
x : int = -1
while swapped:
swapped = False
x = x + 1
for i in range(1, n-x):
if arr[i - 1] > arr[i]:
swap(i - 1, i)
swapped = True
if simulation:
iteration = iteration + 1
print("iteration",iteration,":",*arr)
return arr
def insertion_sort(arr : List , simulation : bool = False) -> List:
""" Insertion Sort
Complexity: O(n^2)
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.
"""
iteration : int = 0
if simulation:
print("iteration", iteration, ":", *arr)
for i in range(len(arr)):
cursor : Union[int, float, complex, str] = arr[i]
pos : int = i
""" Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position"""
while pos > 0 and arr[pos - 1] > cursor:
""" Swap the number down the list"""
arr[pos] = arr[pos - 1]
pos = pos - 1
""" Break and do the final swap"""
arr[pos] = cursor
if simulation:
iteration = iteration + 1
print("iteration",iteration,":",*arr)
return arr
def merge_sub(a : List[int], b : List[int]) -> List[int]:
"""A subroutine of merge sort
Worst-case performance: O(N)
Parameters:
a(List) : Unsorted List
b(List) : Unsorted List
Returns:
c(List) : Two pointer sorted List
"""
n : int = len(a)
m : int = len(b)
i : int = 0
j : int = 0
k : int = 0
c : List[int] = [ 0 for i in range(n+m)]
while(i < n or j < m):
if(j == m or (i < n and a[i] < b[j])):
c[k] = a[i]
k += 1
i += 1
else:
c[k] = b[j]
k += 1
j += 1
return c
def merge_sort(a : List[int]) -> List[int]:
"""Sorts A List using merge sort algorithm
https://en.wikipedia.org/wiki/Merge_sort
Worst-case performance: O(Nlog(N))
Parameters:
a(List) : Unsorted List
Returns:
a(List) : Returns sorted List
"""
n : int = len(a)
if(n < 2):
return a
b : List[int] = [ 0 for i in range(n//2)]
c : List[int] = [ 0 for i in range(n - n//2)]
for i in range(n):
if (i < n//2):
b[i] = a[i]
else:
c[i-(n//2)] = a[i]
return merge_sub(merge_sort(b), merge_sort(c))
def insert_heap(h : List[int], x : int) -> None:
"""Inserting an element to heap
https://en.wikipedia.org/wiki/Heap_(data_structure)
Worst-case performance: O(log(N))
Parameters:
h(List) : List that represents heap
x(int) : element to be inserted
"""
h.append(x)
n : int = len(h)
i : int = n-1
while(i > 0 and h[i] < h[(i-1)//2]):
h[i], h[(i-1)//2] = h[(i-1)//2], h[i]
i = (i-1)//2
def remove_min(h : List[int]) -> int:
"""Removing an element from heap
https://en.wikipedia.org/wiki/Heap_(data_structure)
Worst-case performance: O(log(N))
Parameters:
h(List) : List that represents heap
Returns:
last_element(int) : Returns the smallest element of the heap
"""
n : int = len(h)
last_element : int = h[0]
h[0], h[n-1] = h[n-1], h[0]
h.pop()
n = len(h)
i : int = 0
j : int = 0
while(2*i+1 < n):
j = 2*i+1
if((2*i+2 < n) and h[2*i+2] < h[j]):
j = 2*i+2
if(h[i] <= h[j]):
break
h[i], h[j] = h[j], h[i]
i = j
return last_element
def heap_sort(a : List[int]) -> None:
"""Sorts A List using heap sort algorithm
https://en.wikipedia.org/wiki/Heapsort
Worst-case performance: O(Nlog(N))
Parameters:
a(List) : Unsorted List
"""
temp : List[int] = []
for i in a:
insert_heap(temp, i)
for i in range(len(a)):
a[i] = remove_min(temp)
def quick_sort(a : List[int], l : int, r : int) -> None:
"""Sorts A List using quick sort algorithm
https://en.wikipedia.org/wiki/Quicksort
Worst-case performance: O(N^2)
Average performance: O(Nlog(N))
Parameters:
arr(List) : Unsorted List
l(int) : left index (present in List)
r(int) : Right index (not present in List)
"""
if(r - l <= 1):
return None
idx : int = randint(l, r-1)
x : int = a[idx]
m : int = l
for i in range(l, r, 1):
if (a[i] < x):
a[i], a[m] = a[m], a[i]
m += 1
quick_sort(a, l, m)
quick_sort(a, m, r)