-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
65 lines (45 loc) · 1.58 KB
/
project.py
File metadata and controls
65 lines (45 loc) · 1.58 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
import random
import matplotlib.pyplot as plt
amount = 50
numbers = [random.randint(0, 1000) for _ in range(amount)]
def merge_sort(number_list, left, right):
if left >= right:
return
mid = (left + right) // 2
plt.bar(list(range(amount)), number_list)
plt.pause(0.001)
plt.clf()
merge_sort(number_list, left, mid)
merge_sort(number_list, mid + 1, right)
plt.bar(list(range(amount)), number_list)
plt.pause(0.001)
plt.clf()
merge(number_list, left, right, mid)
plt.bar(list(range(amount)), number_list)
plt.pause(0.01)
plt.clf()
def merge(number_list, left, right, mid):
left_copy = number_list[left:mid + 1]
right_copy = number_list[mid + 1:right + 1]
l_counter, r_counter = 0, 0
sorted_counter = left
while l_counter < len(left_copy) and r_counter < len(right_copy):
if left_copy[l_counter] < right_copy[r_counter]:
number_list[sorted_counter] = left_copy[l_counter]
l_counter += 1
else:
number_list[sorted_counter] = right_copy[r_counter]
r_counter += 1
sorted_counter += 1
while l_counter < len(left_copy):
number_list[sorted_counter] = left_copy[l_counter]
l_counter += 1
sorted_counter += 1
while r_counter < len(right_copy):
number_list[sorted_counter] = right_copy[r_counter]
r_counter += 1
sorted_counter += 1
plt.bar(list(range(amount)), numbers)
plt.show()
merge_sort(numbers, 0, len(numbers) - 1)
print(numbers)