I am currently implementing the Quickselect Algorithm to get the best n elements in a list. The "best" element is in this case the largest.
我目前正在实现Quickselect算法以获得列表中最好的n个元素。在这种情况下,“最佳”元素是最大的。
My problem is the following:
I discovered that this gives poor performance in case of many duplicate elements, because at the end the values that are not sorted will all be the same and at that point the left border will only be incremented by 1 at each iteration regardless of the pivot index.
我的问题如下:我发现这在许多重复元素的情况下表现不佳,因为在最后,未排序的值将全部相同,并且在该点,左边界将仅在每个增加1迭代,无论枢轴索引如何。
Is there any way to fix this and abort in this case?
有没有办法解决这个问题并在这种情况下中止?
1 个解决方案
#1
Quickselect is based on the Quicksort-Algorithm.
Quickselect基于Quicksort算法。
According to Quicksort complexity when all the elements are same? , Quicksort can hit its worst-case time-complexity when all the elements are equal.
根据Quicksort的复杂性,当所有元素都相同时?当所有元素相等时,Quicksort可以达到最坏情况下的时间复杂度。
The link also provides a solution [1] how to speed up Quicksort. Since Quickselect is based on Quicksort, a similar solution might help you.
该链接还提供了解决方案[1]如何加速Quicksort。由于Quickselect基于Quicksort,类似的解决方案可能对您有所帮助。
-
[1] The solution mentiones a partitioning-idea. CS.SE has some question that does include code and compares 2 partitioning algorithms for Quicksort, namely the approach suggested by Lomuto and the one by Hoare: CS.SE: Quicksort Partitioning Hoare vs Lomuto.
The marked answer also states that Hoare's variant is more efficient, while Lomuto's is easier to implement correctly. So depending on your situation, the latter might be sufficient.[1]解决方案提到了分区的想法。 CS.SE有一些问题包括代码并比较Quicksort的2个分区算法,即Lomuto提出的方法和Hoare的方法:CS.SE:Quicksort Partitioning Hoare vs Lomuto。明确的答案还指出Hoare的变体更有效,而Lomuto的更容易正确实现。所以根据你的情况,后者可能就足够了。
-
[2] QuickSort and Hoare Partition
[2] QuickSort和Hoare分区
#1
Quickselect is based on the Quicksort-Algorithm.
Quickselect基于Quicksort算法。
According to Quicksort complexity when all the elements are same? , Quicksort can hit its worst-case time-complexity when all the elements are equal.
根据Quicksort的复杂性,当所有元素都相同时?当所有元素相等时,Quicksort可以达到最坏情况下的时间复杂度。
The link also provides a solution [1] how to speed up Quicksort. Since Quickselect is based on Quicksort, a similar solution might help you.
该链接还提供了解决方案[1]如何加速Quicksort。由于Quickselect基于Quicksort,类似的解决方案可能对您有所帮助。
-
[1] The solution mentiones a partitioning-idea. CS.SE has some question that does include code and compares 2 partitioning algorithms for Quicksort, namely the approach suggested by Lomuto and the one by Hoare: CS.SE: Quicksort Partitioning Hoare vs Lomuto.
The marked answer also states that Hoare's variant is more efficient, while Lomuto's is easier to implement correctly. So depending on your situation, the latter might be sufficient.[1]解决方案提到了分区的想法。 CS.SE有一些问题包括代码并比较Quicksort的2个分区算法,即Lomuto提出的方法和Hoare的方法:CS.SE:Quicksort Partitioning Hoare vs Lomuto。明确的答案还指出Hoare的变体更有效,而Lomuto的更容易正确实现。所以根据你的情况,后者可能就足够了。
-
[2] QuickSort and Hoare Partition
[2] QuickSort和Hoare分区