简体   繁体   English

荷兰国旗 pr0blem 和排序之间的区别

[英]Difference between dutch flag pr0blem and sorting

I'm reading the Dutch national flag problem and I don't get it: why is this different from a simple sorting problem?我正在阅读荷兰国旗问题,但我不明白:为什么这与简单的排序问题不同?

I mean: if we assign 0 to the red color, 1 to white and 2 to blue and do a standard quicksort or whatever.. why should I not get the right answer?我的意思是:如果我们将 0 分配给红色,将 1 分配给白色,将 2 分配给蓝色并进行标准的快速排序或其他什么......为什么我不能得到正确的答案?

Am I missing something?我错过了什么吗?

The Dutch National Flag Problem is just a special case of the more general sorting problem where the only permissible elements are 0, 1, and 2. You can absolutely solve it using a standard sorting algorithm.荷兰国旗问题只是更一般的排序问题的一个特例,其中唯一允许的元素是 0、1 和 2。您绝对可以使用标准排序算法来解决它。

The problem is interesting in of itself partially for historical reasons but largely because it's challenging to find solutions that are stable, time-efficient (O(n)), and space-efficient (O(1)).这个问题本身很有趣,部分原因是历史原因,但主要是因为找到稳定、省时 (O(n)) 和节省空间 (O(1)) 的解决方案具有挑战性。 It's easy to get solutions with two of these three properties, but getting all three is (I believe) an open problem.很容易获得具有这三个属性中的两个的解决方案,但是获得所有三个属性是(我相信)一个悬而未决的问题。 It's also interesting as a venue for developing algorithms for quick sort and partitioning with duplicate keys;作为开发快速排序和使用重复键分区的算法的场所,它也很有趣; you can think of 0, 1, and 2 as less than the pivot, equal to the pivot, and greater than the pivot and you basically have quick sort with repeated elements.您可以将 0、1 和 2 视为小于枢轴、等于枢轴且大于枢轴,并且您基本上可以对重复元素进行快速排序。

Hope this helps!希望这可以帮助!

In this problem the range of values of items to be sorted is small.在这个问题中,要排序的项目值的范围很小。 In such cases it's possible to use non-comparison sort (eg, such as counting sort) to sort the array.在这种情况下,可以使用非比较排序(例如,计数排序)对数组进行排序。 Non comparison sort algorithms can sort in O(n) that is, faster than regular comparison sort algorithms such as quicksort.非比较排序算法可以在 O(n) 中排序,即比快速排序等常规比较排序算法更快。

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

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