Bubble Sort

Bubble sort is an $$O(n^2)$$ algorithm for sorting arrays with $$O(1)$$ memory. it works by "bubbling" each element up to the top. It is stable and adaptive.

Explanation
Bubble sort makes $$n$$ passes through the array, exchanging consecutive elements if they are in the wrong order. It will also stop when no swaps are made, meaning the array is sorted.

Example
Bubble sorting a list of 5 elements: [5 3 4 1 2] [5 3 4 1 2] └─┘ Compare and swap. [3 5 4 1 2]   └─┘ Keep comparing until you reach the end of the list. [3 4 5 1 2]     └─┘ [3 4 1 5 2]        └─┘ [3 4 1 2 5] Repeat until n passes are made or the array is sorted. [3 1 2 4 5] [1 2 3 4 5]

Pseudocode
procedure bubble_sort(array arr) do for i = length(arr)-1 to 0 do for j = 0 to i do if arr[j] > arr[j + 1] do swap arr[j] and arr[j + 1] end if end for end for end