-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.py
More file actions
29 lines (23 loc) · 996 Bytes
/
Copy pathbubbleSort.py
File metadata and controls
29 lines (23 loc) · 996 Bytes
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
# bubble sort
import time
def bubble_sort(array, drawArray, speed):
isSorted = False
lastUnsorted = len(array)-1
while (not isSorted):
isSorted = True
for i in range(lastUnsorted):
if array[i] > array[i+1]:
array[i], array[i+1] = array[i+1], array[i]
isSorted = False
# colour the array, green = being swapped, red = being checked against
colourArray = ['red' if x == i else ('turquoise' if x <= lastUnsorted else 'green') for x in range(len(array))]
drawArray(array, colourArray)
# time.sleep(speed)
lastUnsorted -= 1
# finished sorting
for i in range(len(array)):
colourArray = ['blue' if x == i else ('turquoise' if x < i else 'green') for x in range(len(array))]
drawArray(array, colourArray)
# time.sleep(speed/2)
# colour the array in turquoise
drawArray(array, ['turquoise']*len(array))