Elementary Sort, The slowest and simplest Sort algorithm, time complexity is O(n^2)
Bubble Sort
As Its name, the bigger element will be like a heavy item, Sinking to the bottom of the water, and the small one will swim up to the surface
Here’s the algorithm animation: Bubble Sort
javascript code:
1 | function bubbleSort(a) { |
Insertion Sort
loop through elements, every time counter new element Insert it into already ordered element in front of it.
javascript code:
1 | function insertionSort(a, low = 0, high = a.length - 1) { |
Selection Sort
Loop through elements, choose the smallest one, and put it into the front of elements every round.
javascript code:
1 | function selectionSort(a) { |
Conclusion
There may be other optimizations for these three algorithms, but I just want to talk about the basic version in this post, other optimizations can be discussed in new posts.
Next, I want to talk about Shell Sort, I treat it as Advanced Selection Sort, It’s really amazing in real-world production, tiny easy code but effective