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.
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.
Where am I wrong?
lowandhighare the values you have defined to do the three-way partition i.e. to do a three-way partition you only need two values:In the C++ program what you are moving are the positions where the partitions occurred. A step-by-step example:
As the algorithm says you:
p + 1) because everything below the bottom has been already checked, orq - 1) because everything above the top has been already checked, orYou get
[3, 1, 0, 2],[6, 4]and[9, 8, 9]as bottom, middle and top partitions respectively.