-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (105 loc) · 3.14 KB
/
main.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
from random import randint
from utils import gen_array, run_algo
def main():
array = gen_array(1000)
run_algo("base_sort", array)
run_algo("base_sort_reverse", array)
run_algo("bubble_sort", array)
run_algo("insertion_sort", array)
run_algo("merge_sort", array)
run_algo("quick_sort", array)
# ---------------------- base sort ---------------------- #
def base_sort(array):
"""
Sorts an array using the built-in sorted() function
"""
return sorted(array)
# ---------------------- base sort (reverse) ---------------------- #
def base_sort_reverse(array):
"""
Sorts an array using the built-in sorted() function in reverse order
"""
return sorted(array, reverse=True)
# ---------------------- bubble sort ---------------------- #
def bubble_sort(array):
"""
Sorts an array using the bubble sort algorithm
"""
n = len(array)
for i in range(n):
already_sorted = True
for j in range(n - i - 1):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
already_sorted = False
if already_sorted:
break
return array
# ---------------------- insertion sort ---------------------- #
def insertion_sort(array):
"""
Sorts an array using the insertion sort algorithm
"""
for i in range(1, len(array)):
key = array[i]
j = i - 1
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j -= 1
array[j + 1] = key
return array
# ---------------------- merge sort ---------------------- #
def merge(left, right):
"""
Merges two arrays
"""
if len(left) == 0:
return right
if len(right) == 0:
return left
result = []
index_left = index_right = 0
while len(result) < len(left) + len(right):
if left[index_left] <= right[index_right]:
result.append(left[index_left])
index_left += 1
else:
result.append(right[index_right])
index_right += 1
if index_right == len(right):
result += left[index_left:]
break
if index_left == len(left):
result += right[index_right:]
break
return result
# ---------------------- merge sort ---------------------- #
def merge_sort(array):
"""
Sorts an array using the merge sort algorithm
"""
if len(array) < 2:
return array
mid = len(array) // 2
left = merge_sort(array[:mid])
right = merge_sort(array[mid:])
return merge(left, right)
# ---------------------- quick sort ---------------------- #
def quick_sort(array):
"""
Sorts an array using the quick sort algorithm
"""
if len(array) < 2:
return array
low, same, high = [], [], []
pivot = array[randint(0, len(array) - 1)]
for item in array:
if item < pivot:
low.append(item)
elif item == pivot:
same.append(item)
elif item > pivot:
high.append(item)
return quick_sort(low) + same + quick_sort(high)
if __name__ == "__main__":
main()