-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSortAlgorithm.py
More file actions
45 lines (35 loc) · 1.32 KB
/
Copy pathinsertionSortAlgorithm.py
File metadata and controls
45 lines (35 loc) · 1.32 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
import time
import sortingUtils
from sortingUtils import isSorted, wait
def insertionSort(array, delay):
startTime = time.perf_counter()
while not isSorted(array) and sortingUtils.sorting:
for i in range(1, len(array)):
k = array[i]
j = i - 1
sortingUtils.selectedIndices.clear()
sortingUtils.comparedIndices.clear()
sortingUtils.comparedIndices.append(i)
sortingUtils.comparisons += 1
while j >= 0 and k < array[j]:
sortingUtils.selectedIndices.clear()
sortingUtils.selectedIndices.append(j)
array[j + 1] = array[j]
j -= 1
sortingUtils.swaps += 1
sortingUtils.swapDataSound.play()
sortingUtils.sortTimeVisual = time.perf_counter() - startTime
array[j + 1] = k
wait(delay)
yield
def insertionSortNoVisible(array):
startTime = time.perf_counter()
while not isSorted(array) and sortingUtils.sorting:
for i in range(1, len(array)):
k = array[i]
j = i - 1
while j >= 0 and k < array[j]:
array[j + 1] = array[j]
j -= 1
array[j + 1] = k
sortingUtils.sortTime = time.perf_counter() - startTime