in LintCode Array ~ read.

Sort Colors

LintCode-148.Sort Colors

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.

Notice: You are not suppose to use the library's sort function for this problem. You should do it in-place (sort numbers in the original array).

Given [1, 0, 1, 2], sort it in-place to [0, 1, 1, 2].

class Solution {  
    /**
     * @param nums: A list of integer which is 0, 1 or 2 
     * @return: nothing
     */
    public void sortColors(int[] a) {
        if (a == null || a.length <= 1) {
            return;
        }

        int left = 0;
        int right = a.length - 1;
        int i = 0;

        while (i <= right) {
            if (a[i] == 0) {
                swap(a, left, i);
                left++;
                i++;
            } else if(a[i] == 1) {
                i++;
            } else {
                swap(a, right, i);
                right--;
            }
        }
    }

    private void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

Hope this helps,
Michael