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