-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathSortColors75.java
189 lines (161 loc) · 4.81 KB
/
SortColors75.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* 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?
*/
import java.util.Arrays;
public class SortColors75 {
public void sortColors(int[] nums) {
int L = nums.length;
if (L == 0 || L == 1) {
return;
}
helper(0, L - 1, nums);
}
private void helper(int left, int right, int[] nums) {
if (left == right) {
return;
}
int mid = (left + right) / 2;
helper(left, mid, nums);
helper(mid + 1, right, nums);
merge(left, mid, right, nums);
}
private void merge(int left, int mid, int right, int[] nums) {
int l = 0;
int r = 0;
int i = left;
int[] la = Arrays.copyOfRange(nums, left, mid+1);
int[] ra = Arrays.copyOfRange(nums, mid+1, right+1);
while (l <= mid - left && r <= right - mid - 1) {
if (la[l] <= ra[r]) {
nums[i] = la[l];
l++;
} else {
nums[i] = ra[r];
r++;
}
i++;
}
while (l <= mid - left) {
nums[i] = la[l];
l++;
i++;
}
}
/**
* https://discuss.leetcode.com/topic/6968/four-different-solutions
*/
// two pass O(m+n) space
public void sortColors2(int A[]) {
int num0 = 0, num1 = 0, num2 = 0;
for(int i = 0; i < A.length; i++) {
if (A[i] == 0) ++num0;
else if (A[i] == 1) ++num1;
else if (A[i] == 2) ++num2;
}
for(int i = 0; i < num0; ++i) A[i] = 0;
for(int i = 0; i < num1; ++i) A[num0+i] = 1;
for(int i = 0; i < num2; ++i) A[num0+num1+i] = 2;
}
// one pass in place solution
public void sortColors3(int A[]) {
int n0 = -1, n1 = -1, n2 = -1;
for (int i = 0; i < A.length; ++i) {
if (A[i] == 0)
{
A[++n2] = 2; A[++n1] = 1; A[++n0] = 0;
}
else if (A[i] == 1)
{
A[++n2] = 2; A[++n1] = 1;
}
else if (A[i] == 2)
{
A[++n2] = 2;
}
}
}
// one pass in place solution
public void sortColors4(int A[]) {
int j = 0, k = A.length - 1;
for (int i = 0; i <= k; ++i){
if (A[i] == 0 && i != j)
swap(A, i--, j++);
else if (A[i] == 2 && i != k)
swap(A, i--, k--);
}
}
// one pass in place solution
public void sortColors5(int A[], int n) {
int j = 0, k = n-1;
for (int i=0; i <= k; i++) {
if (A[i] == 0)
swap(A, i, j++);
else if (A[i] == 2)
swap(A, i--, k--);
}
}
// bucket sort
public void sortColors6(int[] nums) {
int[] buckets = new int[3];
for (int i=0; i<nums.length; i++) {
buckets[nums[i]]++;
}
int idx = 0;
for (int i=0; i<3; i++) {
int count = buckets[i];
for (int j=0; j<count; j++) {
nums[idx++] = i;
}
}
}
public void sortColors7(int[] nums) {
if (nums == null || nums.length <= 1) return;
int left = 0;
int right = nums.length - 1;
int i = left;
while (i <= right) {
if (nums[i] == 0) {
swap(nums, i++, left++);
} else if (nums[i] == 2) {
swap(nums, i, right--);
} else {
i++;
}
}
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
public static void main(String[] args) {
SortColors75 sc = new SortColors75();
int[] nums = new int[]{
5, 8, 1, 3, 7, 2, 9, 4, 1, 1, 0
};
System.out.println(Arrays.toString(nums));
sc.sortColors(nums);
System.out.println(Arrays.toString(nums));
nums = new int[]{
1, 1, 0, 2, 0, 1, 2, 0, 1, 1, 1, 2
};
System.out.println(Arrays.toString(nums));
sc.sortColors(nums);
System.out.println(Arrays.toString(nums));
}
}