-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionsort.py
22 lines (18 loc) · 916 Bytes
/
insertionsort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import time
def insertion_sort(data, drawrectangle, delay):
for i in range(1,len(data)):
curr = data[i]
j = i - 1
drawrectangle(data, ['yellow' if x == i else 'lightblue' for x in range(len(data))] )
time.sleep(delay)
while data[j] > data[j+1] and j >= 0:
drawrectangle(data, ['yellow' if x == j or x == j+1 else 'lightblue' for x in range(len(data))] )
time.sleep(delay)
data[j], data[j+1] = data[j+1], data[j]
drawrectangle(data, ['green' if x == j or x == j + 1 else 'lightblue' for x in range(len(data))] )
time.sleep(delay)
j -= 1
# drawrectangle(data, ['yellow' if x == i else 'lightblue' for x in range(len(data))] )
# time.sleep(delay)
# data[j+1] = curr
drawrectangle(data, ['orange' for x in range(len(data))])