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.
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?
Solution 1:
类似radix sort, 先扫描一次得知所有的值出现的次数,再依次setup它们即可
ref: http://blog.csdn.net/fightforyourdream/article/details/15025713
http://en.wikipedia.org/wiki/Radix_sort
Radix sort
Class | Sorting algorithm |
---|---|
Data structure | Array |
Worst case performance | |
Worst case space complexity |
In computer science, radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. Radix sort dates back as far as 1887 to the work of Herman Hollerith on tabulating machines.[1]
1 public void sortColors1(int[] A) { 2 if (A == null || A.length == 0) { 3 return; 4 } 5 6 int len = A.length; 7 8 int red = 0; 9 int white = 0; 10 int blue = 0; 11 12 for (int i = 0; i < len; i++) { 13 if (A[i] == 0) { 14 red++; 15 } else if (A[i] == 1) { 16 white++; 17 } else { 18 blue++; 19 } 20 } 21 22 for (int i = 0; i < len; i++) { 23 if (red > 0) { 24 A[i] = 0; 25 red--; 26 } else if (white > 0) { 27 A[i] = 1; 28 white--; 29 } else { 30 A[i] = 2; 31 } 32 } 33 }
Solution 2:
使用双指针指向左边排好的0和右边排好的1,再加一个指针cur扫描整个数组。一趟排序下来就完成了。相当快捷。
注意:与右边交换之后,cur不能移动,因为你有可能交换过来是1或是0,还需要与左边交换。而与左边交换后,cur就可以向右边移动了。
1 public void sortColors(int[] A) { 2 if (A == null || A.length == 0) { 3 return; 4 } 5 6 int len = A.length - 1; 7 int left = 0; 8 int right = len; 9 10 int cur = 0; 11 while (cur <= right) { 12 if (A[cur] == 2) { 13 // 换到右边,换过来的有可能是0,也有可能是1,所以cur要停留 14 15 swap(A, cur, right); 16 right--; 17 } else if (A[cur] == 0) { 18 // 从左边换过来的只可能是1,所以可以直接cur++ 19 // 因为所有的2全部换到右边去了。 20 21 swap(A, cur, left); 22 left++; 23 cur++; 24 } else { 25 cur++; 26 } 27 } 28 } 29 30 public void swap(int[] A, int n1, int n2) { 31 int tmp = A[n1]; 32 A[n1] = A[n2]; 33 A[n2] = tmp; 34 }
December 22nd, 2014 redo
补充一个例子如下:
注:非常要注意的是,我们要使用cur <= right作为边界值。因为right 指向的是未判断的值。所以当cur == right时,此值仍然需要继续判断。
1 public class Solution { 2 public void sortColors(int[] A) { 3 if (A == null || A.length == 0) { 4 return; 5 } 6 7 int left = 0; 8 9 // Bug 1: right is wrong. 10 int right = A.length - 1; 11 12 int cur = 0; 13 14 // left: the first one which is not 0 15 // right: the first one which is not 2 16 // So we should use <= because right may be not dealed with. 17 while (cur <= right) { 18 switch (A[cur]) { 19 case 0: 20 // Bug 0: Forget to add A. 21 swap(A, left, cur); 22 left++; 23 cur++; 24 break; 25 case 1: 26 cur++; 27 break; 28 case 2: 29 swap(A, cur, right); 30 right--; 31 // 这里不cur++的原因是,有可能从右边换过来有0,1还要继续处理 32 break; 33 34 default: 35 cur++; 36 break; 37 } 38 } 39 } 40 41 public void swap(int[] A, int n1, int n2) { 42 int tmp = A[n1]; 43 A[n1] = A[n2]; 44 A[n2] = tmp; 45 } 46 }
Follow up:
附上写得不错的一个博客:
http://blog.csdn.net/kenden23/article/details/17440519
以及本题的后续: Lintcode: Sort Colors II 解题报告
GitHub:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/sort/SortColors.java