Home > OS >  Why there is no shell sort with binary insertion?
Why there is no shell sort with binary insertion?

Time:01-07

Shell sort in general is improved insertion sort. Another improvement of insertion sort is binary insertion sort.

Why there is no shell sort with binary insertion?

It can be done and it should not be very difficult to code.
(I agree getting indexes right can take 2-3 days)

CodePudding user response:

Insertion sort can benefit significantly from using a binary search to find the insertion point because the sorted portion of the array is contiguous in memory.

Note that while binary search allows you to find the insertion point in O(log n) time, it still takes O(n) time to shift the elements along by one space in order to do the insertion. However, if the subarray is contiguous, this shifting can be done very quickly (i.e. with a low constant factor) by moving the whole section of memory, e.g. using a memmove system call. Moving contiguous sections of memory in bulk is fast because it's a common operation which the hardware is optimised for.

In contrast, shell sort's "sorted portions" are not contiguous; they are "every hth element" of the array, so you cannot perform an insertion using a bulk memory move. In this case a binary search to find the insertion point means you can do O(log n) comparisons, but you still need to do O(n) writes, and you don't have the benefit of a low constant factor for that n.

All of that said, this is a theoretical argument; you could try implementing it yourself, to see how much of a difference it makes in practice.

  • Related