-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting Visualizer(Matplotlib).py
More file actions
150 lines (120 loc) · 3.63 KB
/
Copy pathSorting Visualizer(Matplotlib).py
File metadata and controls
150 lines (120 loc) · 3.63 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
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
import random
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animate
def swap(A, i, j):
if(i!=j):
A[i], A[j] = A[j], A[i]
def bubblesort(A):
if len(A) == 1:
return
swapped = True
for i in range(len(A)-1):
if not swapped:
break
swapped = False
for j in range(len(A) - 1 - i):
if A[j] > A[j+1]:
swap(A, j, j+1)
swapped = True
yield A
def insertionsort(A):
for i in range(1, len(A)):
j = i
while j>0 and A[j] < A[j-1]:
swap(A, j, j-1)
j-=1
yield A
def mergesort(A, start, end):
if end <= start:
return
mid = start + ((end - start + 1) // 2) - 1
yield from mergesort(A, start, mid)
yield from mergesort(A, mid + 1, end)
yield from merge(A, start, mid, end)
yield A
def merge(A, start, mid, end):
merged = []
leftIdx = start
rightIdx = mid + 1
while leftIdx <= mid and rightIdx <= end:
if A[leftIdx] < A[rightIdx]:
merged.append(A[leftIdx])
leftIdx += 1
else:
merged.append(A[rightIdx])
rightIdx += 1
while leftIdx <= mid:
merged.append(A[leftIdx])
leftIdx += 1
while rightIdx <= end:
merged.append(A[rightIdx])
rightIdx += 1
for i, sorted_val in enumerate(merged):
A[start + i] = sorted_val
yield A
def quicksort(A, start, end):
if start >= end:
return
pivot = A[end]
pivotIdx = start
for i in range(start, end):
if A[i] < pivot:
swap(A, i, pivotIdx)
pivotIdx += 1
yield A
swap(A, end, pivotIdx)
yield A
yield from quicksort(A, start, pivotIdx - 1)
yield from quicksort(A, pivotIdx + 1, end)
def selectionsort(A):
if len(A) == 1:
return
for i in range(len(A)):
minVal = A[i]
minIdx = i
for j in range(i, len(A)):
if A[j] < minVal:
minVal = A[j]
minIdx = j
yield A
swap(A, i, minIdx)
yield A
if __name__ == "__main__":
N = int(input("Enter number of integers: "))
method_msg = "Enter sorting method:\n(b)ubble\n(i)nsertion\n(m)erge \
\n(q)uick\n(s)election\n"
method = input(method_msg)
A = [x + 1 for x in range(N)]
random.seed(time.time())
random.shuffle(A)
if method == "b":
title = "Bubble sort"
generator = bubblesort(A)
elif method == "i":
title = "Insertion sort"
generator = insertionsort(A)
elif method == "m":
title = "Merge sort"
generator = mergesort(A, 0, N - 1)
elif method == "q":
title = "Quicksort"
generator = quicksort(A, 0, N - 1)
else:
title = "Selection sort"
generator = selectionsort(A)
fig, ax = plt.subplots()
ax.set_title(title)
bar_rects = ax.bar(range(len(A)), A, align="edge")
ax.set_xlim(0, N)
ax.set_ylim(0, int(1.07 * N))
text = ax.text(0.02, 0.95, "", transform=ax.transAxes)
iteration = [0]
def update_fig(A, rects, iteration):
for rect, val in zip(rects, A):
rect.set_height(val)
iteration[0] += 1
text.set_text("# of operations: {}".format(iteration[0]))
anim = animate.FuncAnimation(fig, func=update_fig,
fargs=(bar_rects, iteration), frames=generator, interval=1, repeat=False)
plt.show()