Is swapping costly or comparison?

Jan 19, 2012 at 2:36am
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?
Jan 19, 2012 at 3:03am
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.
Jan 19, 2012 at 3:03am
That would depend on what you are comparing. For built-in types, such as ints, it is not very costly.
Jan 19, 2012 at 8:56am
You want to use bogosort :D
Topic archived. No new replies allowed.