How can we use our beloved bubble sort in Javascript?
We have seen examples of how to sort an array and sorting with multiple attributes.
The easiest way is to sort an array is to just use Array.sort()
. But that alone will not show you the complexity that you want to show in your program (for e.g. for your next college thesis work that requires you to write minimum 100 pages of code).
Ergo, the need for a bubble sort algorithm.
|
|
The code is quite simple -
- We take a given array and iterate through the array in two loops
- We just use a modern Javascript notation to replace elements in
j
andj-1
positions wheneverelement at j
<element at j-1