[英]Understanding Dutch National flag Program
I was reading the Dutch national flag problem , but couldn't understand what the low
and high
arguments are in the threeWayPartition
function in the C++ implementation. 我正在阅读荷兰国旗问题 ,但无法理解C ++实现中
threeWayPartition
函数中的low
和high
参数是什么。
If I assume them as min and max elements of the array to be sorted, then the if
and else if
statements doesn't makes any sense since (data[i] < low)
and (data[i] > high)
always returns zero. 如果我将它们视为要排序的数组的最小和最大元素,那么
if
和else if
语句没有任何意义,因为(data[i] < low)
和(data[i] > high)
总是返回零。
Where am I wrong? 我哪里错了?
low
and high
are the values you have defined to do the three-way partition ie to do a three-way partition you only need two values: low
和high
是您定义为执行三向分区的值,即执行三向分区,您只需要两个值:
[bottom] <= low < [middle] < high <= [top]
In the C++ program what you are moving are the positions where the partitions occurred. 在C ++程序中,您要移动的是分区发生的位置。 A step-by-step example:
一步一步的例子:
data = [ 3, 1, 4, 9, 8, 2, 6, 9, 0 ]
low = 4
high = 8
[ 3 , 1 , 4 , 9 , 8 , 2 , 6 , 9 , 0 ]
p^ i^ q^
[ 3 , 1 , 4 , 9 , 8 , 2 , 6 , 9 , 0 ]
p^ i^ q^
[ 3 , 1 , 4 , 9 , 8 , 2 , 6 , 9 , 0 ]
p^ i^ q^
[ 3 , 1 , 4 , 9 , 8 , 2 , 6 , 9 , 0 ]
p^ i^ q^
[ 3 , 1 , 4 , 0 , 8 , 2 , 6 , 9 , 9 ]
p^ i^ q^
[ 3 , 1 , 0 , 4 , 8 , 2 , 6 , 9 , 9 ]
p^ i^ q^
[ 3 , 1 , 0 , 4 , 9 , 2 , 6 , 8 , 9 ]
p^ i^ q^
[ 3 , 1 , 0 , 4 , 6 , 2 , 9 , 8 , 9 ]
p^ i^ q^
[ 3 , 1 , 0 , 4 , 6 , 2 , 9 , 8 , 9 ]
p^ i^ q^
[ 3 , 1 , 0 , 2 , 6 , 4 , 9 , 8 , 9 ]
p^ iq^
As the algorithm says you: 正如算法所说:
p + 1
) because everything below the bottom has been already checked, or p + 1
),因为底部下方的所有内容都已经过检查,或者 q - 1
) because everything above the top has been already checked, or q - 1
),因为所有上述顶部已经被选中,或 You get [3, 1, 0, 2]
, [6, 4]
and [9, 8, 9]
as bottom, middle and top partitions respectively. 您分别得到
[3, 1, 0, 2]
, [6, 4]
和[9, 8, 9]
作为底部,中部和顶部分区。
} else if (data[i] >= high) {
表明您看到的内容可能不是文章中所写的内容。
Think of low
and high
as the half-open range [low,high)
for values in the middle partition. 将
low
和high
视为中间分区中的值的半开范围[low,high)
。 All values less than low
end up in the left partition. 低于
low
所有值都在左侧分区中结束。 The middle partition will contain the values from low
up to, but not including, high
. 中间分区将包含从
low
到high
但不包括high
。 Finally, all values greater than or equal to high
end up in the right partition. 最后,所有大于或等于
high
值都会在右侧分区中结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.