-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.py
29 lines (22 loc) · 902 Bytes
/
bubblesort.py
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
import time
max_time = 0.250
# compares neighbouring elements in the array
def bubbleSort(arr,displayArray,speedInput,pauseBool):
swapCount = 0
for i in range(len(arr)):
swapped = False
for j in range(len(arr)-1):
if arr[j] > arr[j+1]:
arr[j],arr[j+1] = arr[j+1],arr[j]
swapCount+=1
colorArray = ['blue' if x==j or x==j+1 else 'red' for x in range(len(arr))]
for clr in range(1,i+1):
colorArray[-clr] = 'green'
displayArray(arr,colorArray,swapCount)
time.sleep(max_time - (speedInput*max_time/100))
swapped = True
if swapped == False:
break
colorArray = ['green' for i in range(len(arr))]
displayArray(arr,colorArray,swapCount)
print("Sorted arr : ",arr)