-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.py
63 lines (54 loc) · 2.25 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# naive implementation
def bubble_sort(data: list) -> None:
"""Sorts a list in place"""
n = len(data)
comparison_count = 0
#compare all the way up til youre comparing penultimate thing to last thing (n-1 comparisons to be made)
for i in range(n-1):
for j in range(n-1):
comparison_count += 1
if data[j] > data[j+1]:
data[j], data[j + 1] = data[j +1], data[j]
print(f"End of pass {i}. `data` is now {data}")
print(f"comparison_count is {comparison_count}")
def bubble_sort_opt(data: list) -> None:
"""Sorts a list in place"""
n = len(data)
comparison_count = 0
for i in range(n-1):
# reduce the amount of times it goes around, no more pointless comparisons
for j in range(n-1-i):
comparison_count += 1
if data[j] > data[j+1]:
data[j], data[j + 1] = data[j +1], data[j]
print(f"End of pass {i}. `data` is now {data}")
print(f"comparison_count is {comparison_count}")
def bubble_sort_opt_more(data: list) -> None:
"""Sorts a list in place"""
n = len(data)
comparison_count = 0
for i in range(n-1):
swapped = False
# reduce the amount of times it goes around, no more pointless comparisons
for j in range(n-1-i):
comparison_count += 1
if data[j] > data[j+1]:
data[j], data[j + 1] = data[j +1], data[j]
swapped = True
print(f"End of pass {i}. `data` is now {data}")
if not swapped:
# the last pass had no swaps, so early exit as data is now sorted
break
# could while not swapped: version of loop using while instead of for loop
print(f"comparison_count is {comparison_count}")
# best worst and average case for algo
# worst case is the data in reverse order, so everything must be sorted backward
# best case - already sorted
#numbers = [1,2,3,4,5,6,7] # when running this we're making n-1 comparisons, best case
numbers = [7, 6, 5, 4, 3, 2, 1] # worst case we make 21 comparisons, worst case
#numbers = [3, 2, 4, 1, 5, 7, 6]
#numbers = [1,2,3,4,6,5,7]
#numbers = list(range(70, 0, 1))
print(f"Sorting {numbers}")
bubble_sort_opt_more(numbers)
print(f"The sorted data is {numbers}")