JS implementation of bubble sort with steps logged to console
Worst and Average Case Time Complexity: O(n*n). Worst case occurs when array is reverse sorted.
Auxiliary Space: O(1)
Boundary Cases: Bubble sort takes minimum time (Order of n) when elements are already sorted.
Sorting In Place: Yes
Stable: Yes
(Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list.)
From the start, we loop through the array and check adjacent elements. if data[i] > data[i+1], then we swap the larger element to the right.
That way, we slowly "bubble up" the largest element to the last spot.
2 < 4, OK [ 2, 4, 8, 10 ]
-- SORTED ARRAY -- [ 2, 4, 8, 10 ]