package
0.0.0-20240717210230-cb72c3092e0b
Repository: https://github.com/ashinzekene/algorithms.git
Documentation: pkg.go.dev
# README
Bubble Sort
Time Complexity
O(N2)
How it works
Bubble sort compares two values swapping them if the one on the left is smaller beginning from the first item in the array to the last item. For example. Given the array [2, 5, 3, 1, 4, 8, 7]
, bubble sort works thus:
- Compares
2
and5
, no sorting occurs[2, 5, 3, 1, 4, 8, 7]
- Compares
5
and3
, they will be swapped, hence we have[2, 3, 5, 1, 4, 8, 7]
- Compares
5
and1
, they will be swapped, hence we have[2, 3, 1, 5, 4, 8, 7]
- Next is
5
and4
, they will be swapped, hence we have[2, 3, 1, 4, 5, 8, 7]
- Next is
5
and8
, they are already sorted[2, 3, 1, 4, 5, 8, 7]
- Next is
8
and7
, they will be swapped, hence we have[2, 3, 1, 4, 5, 7, 8]
This continues till the end of the array is reached, then this process is repeated till the whole array is sorted