简体   繁体   English

为什么没有 shell 二进制插入排序?

[英]Why there is no shell sort with binary insertion?

Shell sort in general is improved insertion sort . Shell sort总的来说是改进的insertion sort Another improvement of insertion sort is binary insertion sort . insertion sort的另一个改进是binary insertion sort

Why there is no shell sort with binary insertion?为什么没有 shell 二进制插入排序?

It can be done and it should not be very difficult to code.可以做到,编码起来应该也不是很困难。
(I agree getting indexes right can take 2-3 days) (我同意让索引正确可能需要 2-3 天)

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.插入排序可以从使用二分查找查找插入点中受益匪浅,因为数组的排序部分在 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.请注意,虽然二分查找允许您在 O(log n ) 时间内找到插入点,但仍需要 O( n ) 时间将元素移动一个空格以执行插入。 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.但是,如果子数组是连续的,则可以通过移动 memory 的整个部分(例如,使用memmove系统调用)非常快速地完成此移位(即,常数因子较低)。 Moving contiguous sections of memory in bulk is fast because it's a common operation which the hardware is optimised for.批量移动 memory 的连续部分很快,因为它是硬件优化的常见操作。

In contrast, shell sort's "sorted portions" are not contiguous;相比之下,shell 排序的“已排序部分”并不连续; they are "every h th element" of the array, so you cannot perform an insertion using a bulk memory move.它们是数组的“每第h个元素”,因此您不能使用批量 memory 移动执行插入。 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 .在这种情况下,通过二进制搜索找到插入点意味着您可以进行 O(log n ) 次比较,但您仍然需要进行 O( n ) 次写入,并且您没有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.您可以尝试自己实施它,看看它在实践中有多大的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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