English 中文(简体)
快速应急循环器需求
原标题:quick-sorts iterator requirements

tl;dr: 能否在一份双倍链接的清单上迅速执行? 在思考之前,我的理解是,它不是

另一天,我有机会审议基本分类算法的代用要求。 基本的O(N2)系统相当简单。

  • Bubble sort - 2 forward iterators will do nicely, one dragging after the other.
  • Insertion sort - 2 bidirectional iterators will do. One for the out-of-order element, one for the insertion point.
  • Selection sort - A little bit trickier but forward iterators can do the trick.

<><>质量>

st:ort(如核心标准图书馆/单版)/硅图(1996年)要求随机进入。

__introsort_loop(_RandomAccessIterator __first,
         _RandomAccessIterator __last,
         _Size __depth_limit, _Compare __comp)

正如我所期望的那样。

更仔细的检查 我找不到要求这样做的真正原因。 唯一明确要求随机抽取的用户是<代码>d:__median,要求计算中间要素。 普通面包车速does not计算中值。

分割包括检查

 if (!(__first < __last))
    return __first;

对双向检查并不真正有用。 然而,人们应当能够用先前的分班旅行(从左边到右边/左边)的简单条件来取代这种旅行。

if ( __first == __last ) this_partitioning_is_done = true;

只有双向导师才能公平有效地实施快速通航? 宽度仍然可以保持。

NB. 我尚未尝试实际执行。

最佳回答

tl;dr: 是

As you say, the problem is to find the pivot element, which is the element in the middle, finding this with random access takes O(1), finding it with bidirectional iterators takes O(n) (n/2 operations, to be precise). However, in each step you have to create to sub containers, the left and the right containing smaller and bigger numbers respectively. This is where the main work of quick sort takes place, right?

现在,在建设次集装箱(再入侵步骤)时,我的做法是建立一个显示各自前沿成分的“<代码>h。 现在,每当你选择一个子集装箱的下一个元素时,就只能每隔一次提前<代码>h。 当你准备放弃新的再入侵步骤时,这将有<代码>h。

You only have to find the first pivot which does not matter really, because O(n log n + n/2) = O(n log n).

Actually this is just a runtime optimisation, but has no impact on the complexity, because whether you iterate over the list once (to put each value in the respective sub container) or twice (to find the pivot and then put each value in the respective sub container) is all the same: O(2n) = O(n).
It s simply a question of execution time (not complexity).

问题回答

你们需要随机接触器,因为你通常想从名单上的中间点取一纸空子。 如果你选择第一种或最后一项内容,则双向探测器就足够了,但随后Quicksort degenerates to O(n^2)的预选清单。

There s absolutely no problem with implementing quick-sort strategy on a doubly-linked list. (I think it can also be easily adapted to a singly-linked list as well). The only place in the traditional quick-sort algorithm that depends on the random-access requirement is the setup phase that uses something "tricky" to select the pivot element. In reality all these "tricks" are nothing more than just heuristics that can be replaced with pretty much equally effective sequential methods.

此前,我已迅速执行相关名单。 没有什么特殊意义,你只是需要密切关注重新连接的适当因素。 如你可能理解的那样,清单排他性算法的大部分价值来自以下事实:你可以将内容重新排序到relinking,而不是明确的价值交换。 不仅可以更快,而且(而且常常——更重要)也保留了可能附在清单内容的外部参考资料的价值有效性。

P.S. However, I d say that for linked lists the merge-sort algorithm results in a significantly more elegant implementation, which has equally good performance (unless you are dealing with some cases that perform better with quick-sort specifically).





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签