Codele / Algorithms
Bubble sort
Bubbles in a glass rise until the biggest ones reach the top. Bubble sort does the same with numbers: the biggest bar floats to the end, pass after pass.
- Bubble sort compares two neighbours and swaps them if the left one is bigger.
- One walk along the row is a pass. After each pass the biggest bar left sits at the end for good.
- If a whole pass makes no swaps, the row is sorted and we stop early.
- It is easy to understand but slow on long rows: about n times n steps, written O(n²).
Loading the playable lesson…