- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSortAlgorithm.py
More file actions
Latest commit
42 lines (37 loc) · 1.5 KB
/
Copy pathbubbleSortAlgorithm.py
File metadata and controls
42 lines (37 loc) · 1.5 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
importtime
importsortingUtils
fromsortingUtilsimportisSorted, wait
defbubbleSort(array, delay):
startTime=time.perf_counter()
whilenotisSorted(array) andsortingUtils.sorting:
swapped=False
foriinrange(len(array)):
forjinrange(0, len(array) -i-1):
sortingUtils.comparisons+=1
sortingUtils.selectedIndices.clear()
sortingUtils.comparedIndices.clear()
sortingUtils.selectedIndices.append(j)
ifarray[j] >array[j+1]:
sortingUtils.swaps+=1
sortingUtils.comparedIndices.append(j+1)
array[j], array[j+1] =array[j+1], array[j]
sortingUtils.swapDataSound.play()
swapped=True
yield
sortingUtils.sortTimeVisual=time.perf_counter() -startTime
wait(delay)
ifnotswapped:
break
defbubbleSortNoVisible(array):
timeArray=array.copy()
startTime=time.perf_counter()
whilenotisSorted(timeArray):
swapped=False
foriinrange(len(timeArray)):
forjinrange(0, len(timeArray) -i-1):
iftimeArray[j] >timeArray[j+1]:
timeArray[j], timeArray[j+1] =timeArray[j+1], timeArray[j]
swapped=True
ifnotswapped:
break
sortingUtils.sortTime=time.perf_counter() -startTime