DutchNationalFlag.java not correct

#1

Trying to understand the code, I ran the Java code a couple of times. The last time I ran it, the answer I got was:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 2, 2, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 1]

Clearly not what was intended.

Working through it to find the issue.

0 Likes

#2

Hi javadev!

Below is my guess, please correct me if I’m wrong :grin:

Let’s back to the Problem Statement again:

Write a function that takes an array A and an index i into A, and rearrange the elements such that all elements less than A[i] appear first, followed by elements equal to A[i], followed by elements greater than A[i].

However, if you pick the smallest element’s index into A, what could happen? All the smallest (here, less and equal are same) elements will appear at the beginning followed by all the greater elements which are unsorted, because we don’t care about them.

So in your case, I think the wried yet reasonable result is only because 0 is rearranged well(satisfied the problem requirement), followed by 1 and 2 unsorted. You can print the A[pivotIndex] to see if it is the smallest.

0 Likes

#3

Hi Carlshen,

Yes, that was my belief also. I was stepping through this to see what happens if the pivot is set to either 0 or 2, because clearly those pivots affect the outcome.

Based on the problem description (which needs help), I would have expected an algorithm to produce 0’s, followed by 1’s, followed by 2’s. The index could be any number that provided a pivot of 1.

As far as the problem description is concerned, I think it should have been stated more clearly that the array is made up of a collection of 0, 1, 2.

0 Likes

#4

Hi javadev(really awesome name)!

Very glad it helps.

At beginning you may feel a little struggle, that’s normal, but I’ll bet keep working on the problems you will feel better!

If you think Dutch National Flag is not a clear case, let’s think about this situation:

You’re dealing with an array of strings of gamers’ genders, include ["m", "f", "u"] which stands for Male, Female and Unspecified respectively. Your task here is to sort them based on the boss’s preference. It could be ["m", "f", "u"] or ["f", "m", "u"], it changes timely because the boss changes his mind very often. As a smart developer, you don’t want to modify your function timely, instead you want to find a way that you don’t need to do the same sort work twice. Once and for all.

Input: [f, m, m, m, u, u] and [u, m, f]
Output: [u, u, m, m, m, f]

Input: [f, m, m, m, u, u] and [f, m, u]
Output: [f, m, m, m, u, u]

Maybe it is a bad example, however, if you want to solve it, you will find the pivotIndex is really reasonable.

0 Likes

#5

Thanks carlshen’s answer to this question :smile:

I think many people misunderstood this problem as a traditional DNF problem but we actually ask for a pivot index also, which is different from the traditional one. As a result, what javadev saw before are the result came from the pivot value = 0.

0 Likes