In Quick-sort , you choose a random pivot that delimits the array to two half's, most of the chances that one may be smaller,
e.g. Array size 100, pivot delimits the array to 40 / 60, the 40 is the the smaller size.
Lets assume that you decide on your threshold size to use the insertion sort to be 10,
you need to continue recursively split the array by pivot , whenever one of the half's become smaller or equal to 10, you may use the insertion sort that behaves like O(n) on small size arrays.
Take into account that insertion sort will behave badly if your array is sorted reversely (worst case).
As regards to the recursion stuff, you only need to modify the stop case of the quick-sort recursion -> array size <= 10 stop recursion and sort the all the array (which is much smaller in this recursion step) using insertion sort.
By tail recursion , they mean do everything you need with the first half, and then invoke insertion sort for the smaller half as a last method , it is used to save space.
Quick-sort()
choose a pivot
move the smaller elements from left
move the bigger elements from right
quick-sort on the bigger half of the array
if half is less then X
only then do an insertion sort on the other half <- this is a tail recursion insertion sort
else
quick sort on this half also
As far as i see , the second optimization suggest not to use insertion sort for every recursion step, but remember the indexes for which the constraint is made, then to invoke insertion sort in one batch concatenating the items from all the slices, this will insure improve the cache use , but is is slightly more difficult to implement,
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…