Bubble sort V1
This algorithm parse through the list one time and sorts the largest value ot the end, bubbling the item to the end.
scratch
https://scratch.mit.edu/projects/1190660429/
Python
https://trinket.io/python/7c0897d2fef4
pseudocode
https://docs.google.com/drawings/d/1UQPL0UdhpBzwoFq4q7yz8oQ-osxl9hhZZHFSKm6LlXE/edit?usp=sharing
Bubble sort V2
This repeats the swap function as many times as the length of the list-1, another words each items is swap chgecked to make sure that all items are in oprder and all pairs have been checked. Inefficient though functional.
Scratch
https://scratch.mit.edu/projects/1192685978
Python
https://trinket.io/python/7750ee428d50
pseudocode
https://docs.google.com/drawings/d/1SljasNG0YvjBnn8tOp_qkwb0Xqecz0B9bKQvp-le8ds/edit?usp=sharing
Bubble sort V3
This algorithm will repeat as long as a 'swap' happens, which changes the swap variable to True, allowing the conditional iteration to repeat the swap parse again. This is more efficient and functional
Scratch
https://scratch.mit.edu/projects/1192689940
Python
https://trinket.io/python/fd6d80fb45da
number_list = [101, 200, 89, 53, 21, 32, 59, 41, 52, 91]
switch = True
parse = 0
# repeat code 9 times - 1 PARSE through the list, resulting in the largest value, bubbling to the end of the list
def swap():
global switch
global parse
swap = 0
parse = parse + 1
for i in range(9):
if number_list[i] > number_list[i+1]:
swap = number_list[i]
number_list[i] = number_list[i+1]
number_list[i+1] = swap
switch = True
# calling the swap parse function as many times as there are items in the list, -1. This is inefficient though functional.
def bubblesortV2():
length = len(number_list)
for i in range(length-1):
swap()
# Calling the swap parse function only if swap parse through one time without switching. THis is more efficeint and functional.
def bubblesortV3():
global switch
while switch:
switch = False
swap()
bubblesortV3()
print(number_list)
print("This repeated, " + str(parse) + " number of times.")