简体   繁体   中英

Why there is no shell sort with binary insertion?

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)

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 (ie with a low constant factor) by moving the whole section of memory, eg 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 h th 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM