Sorting Algorithms: Bubble Sort
Date: 2013-10-09 |
Bubble Sort
Bubble sort moves through the data set, comparing each pair of adjacent elements. If they are in the wrong order, it swaps them. It iterates through the data “bubbling” the largest piece of data to the top.
****In-Place: yes
Online: No
Stable: Yes
Big O: Best O(n), Worst O(n^2)
Example:
5 3 13 7 9 2 6
3 5 13 7 9 2 6
3 5 13 7 9 2 6
3 5 7 13 9 2 6
3 5 7 9 13 2 6
3 5 7 9 2 13 6
3 5 7 9 2 6 | 13
3 5 7 9 2 6 | 13
3 5 7 2 9 6 | 13
3 5 2 7 9 6 | 13
3 5 2 7 9 6 | 13
3 5 2 7 6 | 9 13
3 5 2 7 6 | 9 13
3 2 5 7 6 | 9 13
3 2 5 7 6 | 9 13
3 2 5 6 | 7 9 13
2 3 5 6 | 7 9 13
2 3 5 6 | 7 9 13
2 3 5 | 6 7 9 13
2 3 5 | 6 7 9 13
2 3 | 5 6 7 9 13
2 3 5 6 7 9 13
Want more like this?
The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.