题目链接: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.

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?

这道题的要求是对只有0,1和2的数组进行排序。

1. 计数排序

直接思路,计数排序。先遍历一遍数组,统计0,1和2的数量。接着再遍历1变数组,按照之前统计的数量重新对数组赋值。

不过则需要遍历两遍数组,下面的两种方法只需要遍历一遍数组。

时间复杂度:O(n)

空间复杂度:O(m)

 1 class Solution
 2 {
 3 public:
 4     void sortColors(int A[], int n)
 5     {
 6         int cnt[3] = {0, 0, 0};
 7         for(int i = 0; i < n; ++ i)
 8             ++ cnt[A[i]];
 9         
10         for(int i = 0, j = 0; i < 3; ++ i)
11             while(cnt[i] --)
12                 A[j ++] = i;
13     }
14 };

2. 左右指针

由于要排序的只有3种数字,所以只要把0放到左边,2放到右边,剩下的1放到中间即可。因此采用2个指针标记左边0的右侧位置和右边1的左侧位置,然后不断交换元素。

时间复杂度:O(n)

空间复杂度:O(1)

 1 class Solution
 2 {
 3 public:
 4     void sortColors(int A[], int n)
 5     {
 6         int zero = 0, two = n - 1;
 7         for(int i = 0; i <= two; ++ i)
 8         {
 9             while(A[i] == 2 && i < two)
10                 swap(A[i], A[two --]);
11             while(A[i] == 0 && i > zero)
12                 swap(A[i], A[zero ++]);
13         }
14     }
15 };

3. 利用索引

用3个变量zero、one、two分别标记已排好序的0,1和2的末端索引位置。当读取到0时候,3个变量均后移一位,同是置位;当读到1的时候,one和two后移一位并置位;当读到2的时候,只有two后移一位并置位。

时间复杂度:O(n)

空间复杂度:O(1)

 1 class Solution
 2 {
 3 public:
 4     void sortColors(int A[], int n)
 5     {
 6         int zero = 0, one = 0, two = 0;
 7         for(int i = 0; i < n; ++ i)
 8         {
 9             if(A[i] == 0)
10             {
11                 A[two ++] = 2;
12                 A[one ++] = 1;
13                 A[zero ++] = 0;
14             }
15             else if(A[i] == 1)
16             {
17                 A[two ++] = 2;
18                 A[one ++] = 1;
19             }
20             else
21                 A[two ++] = 2;
22         }
23     }
24 };