algorithm - 关于快速排序 killer

标签 algorithm code-analysis quicksort

你们中的一些人可能偶然发现了这篇可爱的文章 - http://igoro.com/archive/quicksort-killer/\

真正有趣的是他如何修复快速排序以在 O(N log N) 内针对定义的对手执行。

the quicksort might choose the median element as the pivot at each step, thus always geting a perfect split of the input sequence into two halves. Median can be found deterministically in O(N) running time, and so the total running time is always O(N log N).

我的问题是线性时间中值查找算法最终不会使用相同的比较函数并在 O(N^2) 而不是 O(N) 中执行吗?

编辑:

准确地说:我质疑基于分区的中值选择算法的复杂性,该算法使用类似于快速排序的策略,并且将使用与快速排序相同的比较函数。它如何在 O(N) 中与这个对手一起工作?

最佳答案

won't the linear time median-finding algorithm end up using the same compare function and perform in O(N^2) instead of O(N)?

不,通过添加一个 O(N) 函数来找到中位数,复杂度变为

O((N+N) log N) == O(2N log N) == O(N log N)

但是,正如该文章所述,增加的常量使它没有吸引力。

标准技术称为 median-of-3,完整的中值搜索不会真正改善它。

如果最坏的情况很重要,那就不要使用快速排序。 Shellsort 有更好的上限。

关于algorithm - 关于快速排序 killer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4299576/

相关文章:

c# - 基于第一个数组对多个数组进行排序

Java - 将数组值乘以另一个数组

algorithm - 生成连接的凸多边形图

c# - 有什么理由不在生产版本中启用 CODE_ANALYSIS 吗?

C# 调用图生成工具

c# - 替代嵌套类型的类型 Expression<Func<T>>

algorithm - 双轴快速排序的缺点是什么?

c++ - 如何返回另一个 float 的最接近的可表示 float 倍数?

image - 如何缩放特殊范围[0.9 1.1]内的数据?

Java Quicksort分区方法