It’s a well-known isssue with Quicksort that when the data set is in or almost in sort order, performance degrades horribly. In this case, Insertion Sort, which is normally very slow, is easily the best choice. The question is knowing when to use which.
Is there an algorithm available to run through a data set, apply a comparison factor, and return a report on how close the data set is to being in sort order? I prefer Delphi/Pascal, but I can read other languages if the example isn’t overly complex.
As you’d expect quite a lot of thought goes into this. The median-of-three technique means that quicksort’s worst case behaviour doesn’t occur for sorted data, but instead for less obvious cases.
Introsort is quite exciting, since it avoids quicksort’s quadratic worst case altogether. Instead of your natural question, “how do I detect that the data is nearly-sorted”, it in effect asks itself as it’s going along, “is this taking too long?”. If the answer is yes, it switches from quicksort to heapsort.
Timsort combines merge sort with insertion sort, and performs very well on sorted or reverse-sorted data, and on data that includes sorted or reverse-sorted subsets.
So probably the answer to your question is, “you don’t need a pre-pass analysis, you need an adaptive sort algorithm”.