Algorithm - Selection Sort
, the same as Bubble Sort, but in practice Selection Sort is roughly 2x faster.
![](
Refer to the comments in the code. You can understand why it's faster by counting the worst-case operations.
![](
For an array of 5 elements, there are 4 passes.
The number of comparisons decreases by 1 each pass.
So it performs 4, 3, 2, 1 comparisons respectively.
That's 10 total comparisons.
Plus, there's a process of swapping the minimum to the front once per pass, resulting in 4 swaps.
So the total is 14 steps.
![](
For reference, Bubble Sort with 5 elements takes 20 steps.
With 20 elements each:
Selection Sort: 199 steps
Bubble Sort: 380 steps
You can see roughly a 2x difference.