-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.py
37 lines (32 loc) · 902 Bytes
/
bubble_sort.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
30
31
32
33
34
35
36
37
'''
Bubble Sort
'''
from sort_check import sort_check, sort_check1
# Optimizations:
# i. Alternating directions
# ii. Sorted Boundaries
def bubble_sort(l):
start = 0
stop = len(l) - 1
sorted_left, sorted_right = start, stop
incr = +1
last_swap = -1
while last_swap:
while start != stop:
if ( incr * (l[start] - l[start + incr]) ) > 0:
l[start], l[start + incr] = l[start + incr], l[start]
last_swap = start
start += incr
if last_swap == -1:
return
elif incr == 1:
sorted_right = last_swap
start, stop = sorted_right, sorted_left
incr = -1
else:
sorted_left = last_swap
start, stop = sorted_left, sorted_right
incr = + 1
last_swap = -1
sort_check1(bubble_sort, 0b111)
sort_check(bubble_sort)