-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSortAlgorithm.py
More file actions
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
import time
import sortingUtils
from sortingUtils import isSorted, wait
def bubbleSort(array, delay):
startTime = time.perf_counter()
while not isSorted(array) and sortingUtils.sorting:
swapped = False
for i in range(len(array)):
for j in range(0, len(array) - i - 1):
sortingUtils.comparisons += 1
sortingUtils.selectedIndices.clear()
sortingUtils.comparedIndices.clear()
sortingUtils.selectedIndices.append(j)
if array[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)
if not swapped:
break
def bubbleSortNoVisible(array):
timeArray = array.copy()
startTime = time.perf_counter()
while not isSorted(timeArray):
swapped = False
for i in range(len(timeArray)):
for j in range(0, len(timeArray) - i - 1):
if timeArray[j] > timeArray[j + 1]:
timeArray[j], timeArray[j + 1] = timeArray[j + 1], timeArray[j]
swapped = True
if not swapped:
break
sortingUtils.sortTime = time.perf_counter() - startTime