Hadron DaVinci

Bubble Sort Tutorial

16 Apr 2021 - Hadron DaVinci


Example

Sort the given array with Bubble Sort:

A = [5, 2, 4, 6, 1, 3]
bubble_sort(A)
assert(A) == [1, 2, 3, 4, 5, 6]


Method

  1. Bubble up the largest number in the unsorted space
    • adjacent numbers are swapped when left number
      is greater than right number
  2. Reduce the unsorted space
  3. repeat 1.and 2. until the unsorted space is empty

Complexity

Time Complexity: N^2
Space Complexity: 1
Stable

Code

method1.py