I am analysing sorting algorithms, and i have been told that insertion sort works amazing if the number of elements is less than 20.
I considered swapping costly and added some delay there, but it turns out even for less than 20 elements, selection sort is better. Then i made it print number of swaps, and in selection sort they are quite less.
So, is the comparison part that is considered to be costly?
Insertion sort is often implemented without swaps. Insertion sort is very quick if the elements are already sorted or almost sorted and can even beat more advanced algorithms such as Quick sort in such cases.