algorithm:
i) initialize i and j to 0 and k to n-1 Where n is number of balls
ii) while j <= k do
if pointer j point to red ball than
swap a[i] and a[j];
and perform i++; j++
if pointer j point to white ball than
just perform j++
if pointer j point to blue ball than
swap a[j] and a[k];
and perform k--;
This problem is also known as Dutch National Flag Problem. Check out how Dutch flag looks like:
i) initialize i and j to 0 and k to n-1 Where n is number of balls
ii) while j <= k do
if pointer j point to red ball than
swap a[i] and a[j];
and perform i++; j++
if pointer j point to white ball than
just perform j++
if pointer j point to blue ball than
swap a[j] and a[k];
and perform k--;
This problem is also known as Dutch National Flag Problem. Check out how Dutch flag looks like: