Skip to content

Commit d33f0f4

Browse files
committed
added Sort Colors (medium)
1 parent 2261e86 commit d33f0f4

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Medium/SortColors/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Sort Colors
3+
[Leetcode Link](https://leetcode.com/problems/sort-colors/)
4+
5+
## Problem:
6+
7+
Given an array with `n` objects colored red, white or blue, sort them **in-place** so that objects of the same color are adjacent, with the colors in the order red, white and blue.
8+
9+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
10+
11+
Note: You are not suppose to use the library's sort function for this problem.
12+
13+
## Example:
14+
15+
```
16+
Input: [2,0,2,1,1,0]
17+
Output: [0,0,1,1,2,2]
18+
```
19+
20+
## Follow up:
21+
22+
- A rather straight forward solution is a two-pass algorithm using counting sort.
23+
24+
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.
25+
- Could you come up with a one-pass algorithm using only constant space?

Medium/SortColors/Solution.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package Medium.SortColors;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Use 'low' to keep track of entry of next '0',
7+
* use 'middle' to iterate over color array
8+
* use 'high' to keep track of entry of next '2'
9+
*/
10+
public class Solution {
11+
12+
public void sortColors(int[] nums) {
13+
int l = 0,
14+
m = 0,
15+
h = nums.length-1;
16+
while (m <= h) {
17+
switch (nums[m]) {
18+
case 0:
19+
swap(nums, l, m);
20+
l++;
21+
// no break equivalent to
22+
// m++; break; (because it flows to case 1)
23+
case 1:
24+
m++;
25+
break;
26+
case 2:
27+
swap(nums, m, h);
28+
h--;
29+
break;
30+
}
31+
}
32+
}
33+
34+
// helper swap function
35+
private void swap(int[] nums, int a, int b) {
36+
int temp = nums[a];
37+
nums[a] = nums[b];
38+
nums[b] = temp;
39+
}
40+
41+
// test driver
42+
public static void main(String[] args) {
43+
Solution sol = new Solution();
44+
int[] nums = {2,0,2,1,1,0};
45+
System.out.println("Input: " + Arrays.toString(nums));
46+
sol.sortColors(nums);
47+
System.out.println("Output: " + Arrays.toString(nums));
48+
}
49+
50+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Languages used: Java and Python
7474
- [Find Minimum in Rotated Sorted Array](Medium/FindMinInRotatedSortedArray)
7575
- [Maximum Swap](Medium/MaximumSwap)
7676
- [Number of Operations to Make Network Connected](Medium/NumToMakeNetworkConnected)
77+
- [Sort Colors](Medium/SortColors)
7778
- Hard
7879
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
7980
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)