Bubble Sort
Last updated
Was this helpful?
Last updated
Was this helpful?
is a simple yet straightforward swap sorting primitive and its principle is the same as what you might expect from a bubble surfacing up and down in the water.
A naive implementation would be as follow to bubble up the biggest number to the rightmost position in each run for computing the ascending sequence and bubble up the smallest number to the rightmost position in each run for computing the descending sequence:
To improve upon that, a sentinel is introduced so as to resolve useless iterations and comparisons in partially sorted inputs:
is elementary yet simple to evaluate; it yields a scenario of Ο(n) for partially sorted arrays and of Ο(n2)