-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.py
executable file
·146 lines (116 loc) · 2.83 KB
/
viz.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
#!/usr/bin/env python3
import random
import cv2
import numpy as np
import time
random.seed(time.time())
listLength = int(input('length of list to generate: '))
winx, winy = listLength, listLength
white = (255,255,255)
red = (0, 0, 255)
nums = list(range(listLength))
random.shuffle(nums)
# create black background object
def genblackbg(x = winx, y = winy):
return np.zeros((x, y, 3))
# set lines
def makelines(imagetoinsert, data, redline=None):
for n in data:
cv2.line(imagetoinsert, (data.index(n), winy), (data.index(n), n), white, 1)
if n == redline:
cv2.line(imagetoinsert, (data.index(n), winy), (data.index(n), n), red, 1)
# display starting positions (commented out for now)
# startimg = genblackbg()
# makelines(startimg)
# cv2.imshow('startimg', startimg)
time.sleep(1.5)
"""
# bubble sort
for num in range(len(nums)-1,0,-1):
loopimg = genblackbg()
makelines(loopimg, nums)
cv2.imshow('loopimg', loopimg)
cv2.waitKey(1)
for i in range(num):
if nums[i]>nums[i+1]:
temp = nums[i]
nums[i] = nums[i+1]
nums[i+1] = temp
"""
"""
# insertion sort
for index in range(1, len(nums)):
while index > 0 and nums[index - 1] > nums[index]:
nums[index], nums[index - 1] = nums[index - 1], nums[index]
index -= 1
loopimg = genblackbg()
makelines(loopimg, nums, index)
cv2.imshow('loopimg', loopimg)
cv2.waitKey(1)
"""
# radix sort
lst = nums
RADIX = 10
maxLength = False
tmp, placement = -1, 1
while not maxLength:
maxLength = True
# declare and initialize buckets
buckets = [list() for _ in range( RADIX )]
# split lst between lists
for i in lst:
tmp = int((i / placement) % RADIX)
buckets[tmp].append(i)
if maxLength and tmp > 0:
maxLength = False
# empty lists into lst array
a = 0
for b in range( RADIX ):
buck = buckets[b]
for i in buck:
lst[a] = i
a += 1
loopimg = genblackbg()
makelines(loopimg, nums, i)
cv2.imshow('loopimg', loopimg)
cv2.waitKey(1)
# move to next
placement *= RADIX
"""
# merge sort
def merge_sort(collection):
length = len(collection)
if length > 1:
midpoint = length // 2
left_half = merge_sort(collection[:midpoint])
right_half = merge_sort(collection[midpoint:])
i = 0
j = 0
k = 0
loopimg = genblackbg()
makelines(loopimg, collection)
cv2.imshow('loopimg', loopimg)
cv2.waitKey(1)
left_length = len(left_half)
right_length = len(right_half)
while i < left_length and j < right_length:
if left_half[i] < right_half[j]:
collection[k] = left_half[i]
i += 1
else:
collection[k] = right_half[j]
j += 1
k += 1
while i < left_length:
collection[k] = left_half[i]
i += 1
k += 1
while j < right_length:
collection[k] = right_half[j]
j += 1
k += 1
return collection
merge_sort(nums)
"""
cv2.waitKey(0)
cv2.destroyAllWindows()