-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.py
More file actions
33 lines (26 loc) · 1.09 KB
/
Copy pathBubbleSort.py
File metadata and controls
33 lines (26 loc) · 1.09 KB
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
def sort_with_bubbles(lst):
# Set swap_occurred to True to guarantee the loop runs once
swap_occurred = True
# Run the loop as long as a swap occurred the previous time
while swap_occurred:
# Start off assuming a swap did not occur
swap_occurred = False
# For every item in the list except the last one...
for i in range(len(lst) - 1):
# If the item should swap with the next item...
if lst[i] > lst[i + 1]:
# Then, swap them! But these lines aren't
# quite right: fix this code!
temp = lst[i]
lst[i] = lst[i + 1]
lst[i + 1] = temp
# One more line is needed here; add it!
swap_occurred = True
return lst
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: [1, 2, 3, 4, 5]
print(sort_with_bubbles([5, 3, 1, 2, 4]))