它是更快地插入排序项目或将其添加到排序列表之后列表列表、将其、它是、更快

2023-09-10 23:47:34 作者:与时间为敌

如果我有一个排序的列表(比如快速排序排序),如果我有很多值增加的,是它更好地暂停排序,并将其添加到年底,然后进行排序,或者使用二进制印章放置正确的项目,同时增加他们。是否有所作为,如果该物品是随机的,或者已经或多或少的订单?

If I have a sorted list (say quicksort to sort), if I have a lot of values to add, is it better to suspend sorting, and add them to the end, then sort, or use binary chop to place the items correctly while adding them. Does it make a difference if the items are random, or already more or less in order?

推荐答案

如果您添加足够的项目,你有效地构建名单从头开始,你应该能够通过事后排序列表,以获得更好的性能。

If you add enough items that you're effectively building the list from scratch, you should be able to get better performance by sorting the list afterwards.

如果项目大都是为了,您可以调整两个增量更新,并定期整理,以利用这一点,但坦率地说,它通常是不值得的麻烦。 (你还需要小心之类的东西,确保一些意外的排序不能让你的算法需要太多的长的,QV天真快速排序)

If items are mostly in order, you can tweak both incremental update and regular sorting to take advantage of that, but frankly, it usually isn't worth the trouble. (You also need to be careful of things like making sure some unexpected ordering can't make your algorithm take much longer, q.v. naive quicksort)

这两个增量更新和常规列表排序为O(N日志N),但你可以得到一个更好的常数因子排序后的一切(我这里假设你已经有了一些辅助数据结构使你的增量更新可以访问列表项目比O(N)...)更快。一般来说,整理一次全部拥有更多的设计自由度比保持顺序递增,因为增量更新必须时刻保持一个完整的订单,而是一种在一次性批量排序不。

Both incremental update and regular list sort are O(N log N) but you can get a better constant factor sorting everything afterward (I'm assuming here that you've got some auxiliary datastructure so your incremental update can access list items faster than O(N)...). Generally speaking, sorting all at once has a lot more design freedom than maintaining the ordering incrementally, since incremental update has to maintain a complete order at all times, but an all-at-once bulk sort does not.

如果不出意外,请记住,有很多高度优化的批量各种可用。

If nothing else, remember that there are lots of highly-optimized bulk sorts available.

 
精彩推荐
图片推荐