From what I have understood reading posts on quicksort
is that the choice of the pivot element hugely influences whether it runs with worst case performance for a given input or not.
根据我的理解,关于快速排序的阅读帖子是,枢轴元素的选择极大地影响它是否在给定输入的最坏情况下运行。
I ask myself
我问我自己
-
does a
randomized
pivot element simply minimize (but not exclude) the chance of running inn^2
一个随机的枢轴元素只是最小化(但不排除)在n ^ 2中运行的机会
-
What condition(s) must the
input
meet given adeterministic pivot element
like for example, always picking the first element as pivot so thatn^2
becomes reality.在给定确定性枢轴元素的情况下,输入必须符合什么条件,例如,始终将第一个元素选为枢轴,以使n ^ 2成为现实。
-
How would I input so that worst-case performance becomes reality?
我将如何输入以使最坏情况的性能成为现实?
Others have given the example of an already sorted array be it in ascending or descending order as an extreme case.
其他人已经给出了已经排序过的数组的例子,它是一个极端情况的升序或降序。
I assume that this has to do with the splitting procedure
, the pointers for element < pivot
(and vice versa) and the way an unfavorable pivot element makes the splitting process more costly.
我假设这与分裂过程,元素
Could possibly someone show on a simple example like an array
with [1,2,3]
and pivot [0]
how worst-case performance is met so that I can see how all this relates to one another.
可能有人会在一个简单的例子上显示像[1,2,3]和pivot [0]这样的最坏情况下的表现,以便我可以看到这一切是如何相互关联的。
1 个解决方案
#1
-
Yes, a randomized pivot element simply makes the worst case unlikely.
是的,一个随机的枢轴元素只是使最坏的情况不太可能。
-
A sufficient condition would be that the choice of the pivot, given an array of length n, always splits it into two arrays with one having length O(1).
一个充分的条件是,给定长度为n的数组,枢轴的选择总是将其分成两个阵列,其中一个长度为O(1)。
-
Assume you have some rule that at call i, the algorithm chooses the element of the array
values
at position v(n, i) as the pivot element (the example you gave is that v(n, i) = 0 always, i.e., the algorithm always looks at the first element). Then set:假设你有一些规则,在调用i时,算法选择位置v(n,i)处的数组值的元素作为pivot元素(你给出的例子是v(n,i)= 0总是,即,算法总是看第一个元素)。然后设置:
values_0[v(n, 0)] = 0
values_0 [v(n,0)] = 0
values_1[v(n, 1)] = 1
values_1 [v(n,1)] = 1
values_2(v(n, 2)] = 2
values_2(v(n,2)] = 2
....
where values_i is the array formed from the original array by omitting the elements at v(n, j) for j < i.
其中values_i是通过省略j(i)的v(n,j)处的元素而从原始数组形成的数组。
Regarding your example of [1, 2, 3] and pivot 0, to the best of my understanding, it is not well defined. You cannot obtain a full worst-case example using a fixed pivot element, as the recursion will never end.
关于你的[1,2,3]和0的例子,据我所知,它没有很好地定义。您无法使用固定的pivot元素获得完整的最坏情况示例,因为递归永远不会结束。
#1
-
Yes, a randomized pivot element simply makes the worst case unlikely.
是的,一个随机的枢轴元素只是使最坏的情况不太可能。
-
A sufficient condition would be that the choice of the pivot, given an array of length n, always splits it into two arrays with one having length O(1).
一个充分的条件是,给定长度为n的数组,枢轴的选择总是将其分成两个阵列,其中一个长度为O(1)。
-
Assume you have some rule that at call i, the algorithm chooses the element of the array
values
at position v(n, i) as the pivot element (the example you gave is that v(n, i) = 0 always, i.e., the algorithm always looks at the first element). Then set:假设你有一些规则,在调用i时,算法选择位置v(n,i)处的数组值的元素作为pivot元素(你给出的例子是v(n,i)= 0总是,即,算法总是看第一个元素)。然后设置:
values_0[v(n, 0)] = 0
values_0 [v(n,0)] = 0
values_1[v(n, 1)] = 1
values_1 [v(n,1)] = 1
values_2(v(n, 2)] = 2
values_2(v(n,2)] = 2
....
where values_i is the array formed from the original array by omitting the elements at v(n, j) for j < i.
其中values_i是通过省略j(i)的v(n,j)处的元素而从原始数组形成的数组。
Regarding your example of [1, 2, 3] and pivot 0, to the best of my understanding, it is not well defined. You cannot obtain a full worst-case example using a fixed pivot element, as the recursion will never end.
关于你的[1,2,3]和0的例子,据我所知,它没有很好地定义。您无法使用固定的pivot元素获得完整的最坏情况示例,因为递归永远不会结束。