-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSortAlgorithm.py
More file actions
56 lines (40 loc) · 1.54 KB
/
Copy pathcountSortAlgorithm.py
File metadata and controls
56 lines (40 loc) · 1.54 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
import time
import sortingUtils
from sortingUtils import isSorted, wait
def countSort(array, delay):
while not isSorted(array):
startTime = time.perf_counter()
M = max(array)
count_array = [0] * (M + 1)
sortingUtils.selectedIndices.clear()
for num in array:
count_array[num] += 1
for i in range(1, M + 1):
count_array[i] += count_array[i - 1]
output_array = [0] * len(array)
for i in range(len(array) - 1, -1, -1):
output_array[count_array[array[i]] - 1] = array[i]
count_array[array[i]] -= 1
sortingUtils.swaps += 1
sortingUtils.swapDataSound.play()
sortingUtils.selectedIndices.append(i)
yield
wait(delay)
sortingUtils.sortTimeVisual = time.perf_counter() - startTime
def countSortNoVisible(array):
while not isSorted(array):
startTime = time.perf_counter()
M = max(array)
count_array = [0] * (M + 1)
for num in array:
count_array[num] += 1
for i in range(1, M + 1):
count_array[i] += count_array[i - 1]
output_array = [0] * len(array)
for i in range(len(array) - 1, -1, -1):
output_array[count_array[array[i]] - 1] = array[i]
count_array[array[i]] -= 1
sortingUtils.swaps += 1
sortingUtils.swapDataSound.play()
sortingUtils.selectedIndices.append(i)
sortingUtils.sortTime = time.perf_counter() - startTime