-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_algo.py
67 lines (55 loc) · 1.57 KB
/
sort_algo.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
from timeit_helper import profile_func
import random
def create_matrix(n):
matrix = []
for i in range(n):
matrix.append([0] * n)
return matrix
def bubble_sort(arr): # Big O(n2)
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
def quick_sort(arr): # Big O(nxlogn)
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
def merge_sort(arr): # Big O(n)
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1
def main():
arr = tuple(range(1, 1000000))
random_num = random.choice(arr)
print(f"Random Number: {random_num}")
func_maps = {
}
for func_algo, func_test in func_maps.items():
profile_func(lambda: func_test(arr, random_num),func_algo, 100)
if __name__ == "__main__":
main()