In [1]:
import ipytracer
from IPython.display import display
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
In [2]:
def bubble_sort(unsorted_list):
x = ipytracer.List1DTracer(unsorted_list)
display(x)
length = len(x)-1
for i in range(length):
for j in range(length-i):
if x[j] > x[j+1]:
x[j], x[j+1] = x[j+1], x[j]
return x.data
In [3]:
bubble_sort([6,4,7,9,3,5,1,8,2])
Out[3]:
In [4]:
def bubble_sort(unsorted_list):
x = ipytracer.ChartTracer(unsorted_list)
display(x)
length = len(x)-1
for i in range(length):
for j in range(length-i):
if x[j] > x[j+1]:
x[j], x[j+1] = x[j+1], x[j]
return x.data
In [5]:
bubble_sort([6,4,7,9,3,5,1,8,2])
Out[5]: