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.

 1 public class Solution {
 2     public void sortColors(int[] A) {
 3         int i=0,start=0,end =A.length-1;
 4         while(i<=end){
 5             if(A[i]==0){
 6                 int temp = A[i];
 7                 A[i] = A[start];
 8                 A[start] = temp;
 9                 i++;
10                 start++;
11                 continue;
12             }
13             else if(A[i]==2){
14                 int temp = A[i];
15                 A[i] = A[end];
16                 A[end] = temp;
17                 end--;
18                 continue;
19             }
20             else{
21                 i++;
22             }
23         }
24     }
25 }
View Code

 

posted @ 2014-02-12 03:56  krunning  阅读(145)  评论(0编辑  收藏  举报