Sort Colors

2013.12.22 04:39

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.

click to show follow up.

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?

Solution1:
  First solution is the plain counting sort. Count the numbers first and rewrite the array next. This algorithm requires two passes of the array.
  Time complexity is O(n), space compelxity is O(1).
Accepted code:
 1 // 1WA, 1AC
 2 class Solution {
 3 public:
 4     void sortColors(int A[], int n) {
 5         // IMPORTANT: Please reset any member data you declared, as
 6         // the same Solution instance will be reused for each test case.
 7         if(n <= 0 || A == nullptr){
 8             return;
 9         }
10         
11         int n0, n1, n2;
12         int i, j;
13         
14         // 1WA here, wrong sentence: n0 = n1 = n2;
15         n0 = n1 = n2 = 0;
16         for(i = 0; i < n; ++i){
17             if(A[i] == 0){
18                 ++n0;
19             }else if(A[i] == 1){
20                 ++n1;
21             }else{
22                 ++n2;
23             }
24         }
25         
26         j = 0;
27         for(i = 0; i < n0; ++i){
28             A[j++] = 0;
29         }
30         for(i = 0; i < n1; ++i){
31             A[j++] = 1;
32         }
33         for(i = 0; i < n2; ++i){
34             A[j++] = 2;
35         }
36     }
37 };
Solution2:
  The one-pass solution for this problem can be done with three iterators, corresponding to 0, 1 and 2. Put 0 on the front, 2 at the back and 1 in the middle.
  It seems this algorithm is O(n) in time, but it is actually inefficient O(n^2). The process of inserting the "1"s into middle part requires a lot of swap operation. I don't think this is a good choice, because it's no faster than insertion sort.
  Time complexity is O(n^2), space complexity is O(1).
Accepted code:
 1 // 1TLE, 1WA, 1AC, one-pass but inefficient solution
 2 class Solution {
 3 public:
 4     void sortColors(int A[], int n) {
 5         if(n <= 0 || A == nullptr){
 6             return;
 7         }
 8         
 9         int i0, i1, i2;
10         int tmp;
11         
12         i0 = 0;
13         i1 = n - 1;
14         i2 = n - 1;
15         // 1WA here, it's "<=" instead of "<".
16         while(i0 <= i1){
17             if(A[i0] == 1){
18                 tmp = A[i0];
19                 A[i0] = A[i1];
20                 A[i1] = tmp;
21                 --i1;
22             }else if(A[i0] == 2){
23                 tmp = A[i0];
24                 A[i0] = A[i2];
25                 A[i2] = tmp;
26                 --i2;
27                 if(i2 < i1){
28                     //  1TLE here, it's "i1 = i2" instead of "i2 = i1".
29                     i1 = i2;
30                 }
31             }else{
32                 ++i0;
33             }
34         }
35     }
36 };

 

 posted on 2013-12-22 04:42  zhuli19901106  阅读(193)  评论(0编辑  收藏  举报