Question

I just finished the old "Sorting Detective" homework (where you are given a few black box sorting algorithms and have to determine what example each one is by the results), and I noticed that Insertion Sort always took N-1 comparisons on a sorted list. Since I won't be able to look at my instructor's code until everyone has turned in their assignments, nor am I allowed to ask questions in class that could tip off the other students as to how to proceed to solve the problem, this left me with a question I can't get the answer to this question for at least a week.

Is it always the case in the real world that a textbook example of Insertion Sort will do N-1 comparisons on a sorted list, or is it quirk of my instructor's/textbook's version of Insertion Sort?

After searching Google and the Wikipedia, I couldn't find the answer to this, which means either I'm asking the wrong question or they just don't have it. Any ideas?

Was it helpful?

Solution

Yes, a “textbook” insertion sort will perform N-1 comparisons if the input is already sorted. See for example Knuth's The Art of Computer Programming, Volume 3, §5.2.1, Algorithm S (Straight insertion sort). For each record Ri (i > 1), the algorithm compares it against elements Ri-1 down to R1, in that order, until it finds an element smaller than Ri. If the input is already sorted, then Ri-1 is always smaller than Ri, so the algorithm only performs one comparison per record (and no comparisons for record 1).

Of course, you could write insertion sort to perform the comparisons in order from R1 to Ri-1 instead, but that would perform N*(N-1)/2 comparisons if the input list were already sorted. So don't do that. :)

You could also use a binary search to find the location at which to insert each record. This is called binary insertion, and Knuth also discusses it in §5.2.1. Again, it will perform more comparisons on an already-sorted input than textbook straight insertion (but fewer on random input).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top