了解荷兰国旗计划荷兰、国旗、计划

由网友(頹廢、懂不了的伤╯℡)分享简介:我读荷兰国旗问题,但不明白什么低和高参数是在C threeWayPartition 函数++实现。 I was reading the Dutch national flag problem, but couldn't understand what the low and high arguments are i...

我读荷兰国旗问题,但不明白什么参数是在C threeWayPartition 函数++实现。

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.

如果我认为他们作为分和数组的最大元素进行排序,那么如果否则如果语句不作出任何意义,因为(数据[1] - 低)(数据[1] - GT;的高点)总是返回零。

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.

我在哪里错了?

推荐答案

的值,你定义做了三路分区,即做一个三方的分区,你只需要两个值:

low and high are the values you have defined to do the three-way partition i.e. to do a three-way partition you only need two values:

[bottom] <= low  < [middle] < high <= [top]

在C ++程序你正在为那里的分区发生的位置。一个一步一步的例子:

In the C++ program what you are moving are the positions where the partitions occurred. 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 ),因为底部之下的一切已被检查,或 交换元素的的在的的顶部(即① - 1 )因为一切都高于顶已被检查,或 的 退出的它是因为它属于中间的元素。 Swap the element above the bottom (i.e. p + 1) because everything below the bottom has been already checked, or Swap the element below the top (i.e. q - 1) because everything above the top has been already checked, or Leave the element where it is because it belongs to middle.

您获得 [3,1,0,2] [6,4] [9,8,9] 的底部,中部和顶部分别分区。

You get [3, 1, 0, 2], [6, 4] and [9, 8, 9] as bottom, middle and top partitions respectively.

阅读全文

相关推荐

最新文章