Question

link

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:
You are not suppose to use the library's sort function for this problem.

Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.

Could you come up with an one-pass algorithm using only constant space?

Stats

Frequency 2
Difficulty 4
Adjusted Difficulty 4
Time to use --------

Ratings/Color = 1(white) 2(lime) 3(yellow) 4/5(red)

Analysis

This is a extremely interesting question, with very tricky solutions. But the 3rd piece of code is actually the standard solution (by making use of the idea from partition array).

Code

First solution came from this blog.

public void sortColors(int[] A) {
    int a = -1, b = -1, c = -1;
    for (int i = 0; i < A.length; i ++) {
        if (A[i] == 0) {
            A[++ c] = 2;
            A[++ b] = 1;
            A[++ a] = 0;
        } else if (A[i] == 1) {
            A[++ c] = 2;
            A[++ b] = 1;
        } else {
            A[++ c] = 2;
        }
    }
}

Second solution, 2 pointer & swap which is written here.

public void sortColors(int[] A) {
    int len = A.length;
    int i = 0, x = 0, y = len - 1;
    while (i <= y) {
        if (A[i] == 0)
            swap(A, i ++, x ++);
        else if (A[i] == 2)
            swap(A, i, y --);
        else i ++;
    }
}

private void swap(int[] A, int a, int b) {
    int temp = A[a];
    A[a] = A[b];
    A[b] = temp;
}

Updated on July 4th, 2014: Use of solution of Partition Array to partition colors twice:

  1. first time move all 0 to left.
  2. second time move all 1 to left, following the 0s.

Code :

public void sortColors(int[] A) {
    if (A == null || A.length == 0) {
        return;
    }
    int len = A.length;
    partition(A, 0, len - 1, 0);
    int p = 0;
    while (p < len && A[p] == 0) {
        p++;
    }
    partition(A, p, len - 1, 1);
}

private void partition(int[] A, int start, int end, int target) {
    // find the target and put it on the left side of the array
    while (start < end) {
        while (start < A.length && A[start] == target) {
            start++;
        }
        while (end >= 0 && A[end] != target) {
            end--;
        }
        if (start > end) {
            break;
        } else {
            int temp = A[start];
            A[start] = A[end];
            A[end] = temp;
        }
    }
}