关于复杂性(如果使用基于比较的排序算法)

发布于 2024-12-28 14:09:24 字数 218 浏览 1 评论 0 原文

众所周知,任何基于比较模型的排序算法都有nlogn的下界,即Omega(nlogn)。 这可以用数学证明。

但众所周知,荷兰国旗问题可以在 O(n) 时间内对 3 个不同的元素(重复使用)进行排序。它也是基于比较模型,但可以在 O(n) 时间内进行排序。 那么我们如何证明基于比较模型的排序下限是合理的,因为荷兰国旗问题有点违反了这一点。

请帮助我理解差异......

谢谢

as we all know that any sorting algorithm based on comparison model has lower bound of nlogn i.e Omega(nlogn).
which can be proved mathematically.

but as we all know dutch flag problem can sort 3 distinct elements (used repeatedly) in O(n) time.It is also based on comparison model but can sort in O(n) time.
so how can we justify lower bound of sorting based on comparison model , because dutch flag problem kinda violates that.

please help me understanding the difference.....

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终难愈 2025-01-04 14:09:24

在我看来,这不是下界的相关“矛盾”,因为它是一种特殊情况(可能的值范围小于元素总数,实际上它是一个常数,3),可以是用于找到比 O(N*logN) 更快的算法,O(N*logN) 是一般基于比较的排序算法的下限(即,您没有关于序列内容的信息)。

通常在N个元素在0..K范围内并且K<1的情况下。 N 您可以有效地利用非比较排序(例如计数排序)来解决 O(N) 的问题。

In my opinion, this is not a relevant "contradiction" of the lower bound, because it is a particular case (the possible range of values is smaller than the total number of elements, in fact it is a constant, 3) which can be exploited to find an algorithm faster than O(N*logN), which is the lower bound for a general comparison-based sorting algorithm (i.e. where you have no information about the content of the sequence).

Generally in the case where N elements are in the range 0..K with K < N you can efficiently exploit a non-comparison sort like counting sort to solve the problem in O(N).

去了角落 2025-01-04 14:09:24

边界Omega(n log n) 给出了基于比较的任意元素排序的下限。

荷兰国旗问题考虑的只是没有任意元素,但每个元素只有三个选项的情况。

因此,边界Omega(n log n) 一般情况下适用于该问题,无需额外约束。但是,如果您施加其他约束(例如,每个元素只有三个选项),您很可能能够找到比此限制更好的算法,因为您随后考虑了一个特定情况,其中您也许可以应用其他启发法等。

The bound Omega(n log n) gives a lower bound for comparison-based sorting of arbitrary elements.

What the dutch flag problem considers is just a case where you do not have arbitrary elements, but just three options for each element.

Thus, the bound Omega(n log n) holds for the problem in general, without additional constraints. However, if you impose other constraints (for example, that there are just three options for each element), you might well be able to find algorithms better than this bound simply because you then consider a particular case where you might be able to apply other heuristics, etc.

老街孤人 2025-01-04 14:09:24

要点是:荷兰国旗的设置并不是完全有序的。有许多相等的元素,事实上,当您计算不同的对象时,它是一组(最多)大小为 3 的集合。

Omega(n log n) 边界可能仅在对象时才困难>ab 要么 a 要么 b>a 成立(又名:所有对象都是不同的

事实上,我可以排序O(0),此时所有对象都必须为 null

假设至少有两个不同的对象,我相信正确的界限类似于 Omega(n log m),其中 n 是对象的数量,m 不同对象的数量(排序不相等)。简单来说,log m是找到输出桶所需的决策数量。一般情况下,如果相等对象的数量较少,则 O(log m)=O(log n)。在荷兰国旗问题中,m=3

基数排序以不同的方式利用了这一点。它也被认为是线性的。但是,它需要 log m 传递数据,其中 m 是可能不同元素的数量。所以实际上,基数排序也是Omega(n log m),其中m是它可以识别的对象数量。

The point is: the dutch flag set is not totally ordered. There are many equal elements, in fact when you count distinct objects, it is a set of (at most) size 3.

The Omega(n log n) bound is probably only hard when for objects a and b either a<b or b>a holds (aka: all objects are distinct)

In fact, I can sort in O(0), when all objects must be null.

Assuming that there are at least two distinct objects, I believe the proper bound is something like Omega(n log m) where n is the number of objects and m is the number of distinct objects (that do not sort equal). Simply speaking, log m is the number of decisions needed to find the output bucket. In the general case, O(log m)=O(log n), if the amount of equal objects is low. In the dutch flag problem, m=3.

Radix sort exploits this in a different way. It is also considered to be linear. However, it requires log m passes over the data, where m is the number of possibly distinct elements. So actually, Radix sort also is Omega(n log m), where m is the number of objects it can discern.

逆光下的微笑 2025-01-04 14:09:24

荷兰国旗问题比正常排序有一些更基本的数据,它知道存在三个不同的值,如果您查看维基百科页面的算法,它是:

void threeWayPartition(int data[], int size, int low, int high) {
  int p = -1;
  int q = size;
  for (int i = 0; i < q;) {
    if (data[i] < low) {
      swap(data[i], data[++p]);
      ++i;
    } else if (data[i] >= high) {
      swap(data[i], data[--q]);
    } else {
      ++i;
    }
  }
}

事实上,它不使用元素对之间的比较,它只是使用较低/之间的比较中/上界和元素,与普通排序中使用的其他比较方法无关,可以与计数排序进行比较。

Dutch flag problem has some more basic data than normal sort, it knows there is three different value and if you look at wikipedia page for algorithm it's:

void threeWayPartition(int data[], int size, int low, int high) {
  int p = -1;
  int q = size;
  for (int i = 0; i < q;) {
    if (data[i] < low) {
      swap(data[i], data[++p]);
      ++i;
    } else if (data[i] >= high) {
      swap(data[i], data[--q]);
    } else {
      ++i;
    }
  }
}

In fact it doesn't used comparison between pair of elements, it just used comparison between lower/medium/upper bound and elements, which is irrelevant to other comparison methods which used in normal sorting, you can compare it with Counting Sort.

空城仅有旧梦在 2025-01-04 14:09:24

荷兰国旗问题更多的是一个划分算法。

这只是排序的第一步。
您可以使用它进行排序(通过一遍又一遍地将其应用于分区),但我猜您最终会得到 Omega(nlogn)。

知道不同值的数量(对于旗帜来说是 3 个)及其值(蓝色、白色、红色)的情况非常罕见。

Dutch Flag problem is more of a partitioning algorithm.

It's only a first step to sorting.
You could use it for sorting (by applying it to the partitions over and over again), but i guess you end up with Omega(nlogn).

Knowing the number of different values (3 in case of the flag) and their values (blue, white, red) is a very rare case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文