-
Notifications
You must be signed in to change notification settings - Fork 21
/
Sort Colors II.java
executable file
·119 lines (96 loc) · 3.11 KB
/
Sort Colors II.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
M
1531086337
tags: Sort, Two Pointers, Partition, Quick Sort
Sort Color的普通版, sort all k colors in colors array.
Details 参见: https://github.com/awangdev/LintCode/blob/master/Java/Sort%20Color.java
#### Quick Sort
- O(nk)
```
/*
LintCode
Given an array of n objects with k different colors (numbered from 1 to k),
sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.
Example
GIven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4].
Note
You are not suppose to use the library's sort function for this problem.
Challenge
A rather straight forward solution is a two-pass algorithm using counting sort. That will cost O(k) extra memory.
Can you do it without using extra memory?
Tags Expand
Two Pointers Sort
*/
/*
Thoughts (Need to revist and think about this, very interesting)
Doing quick sort partition for K -1 times.
1. Use K - 1 value as pivot
2. Starting from 0, whenever low<high && less or equal to pivot, low++
3. starting from end, whenever high >0, and greater than pivot, high--
4. Result: only swap when low and high have disagreement on the pivot value.
*/
class Solution {
public void sortColors2(int[] colors, int k) {
if (colors == null || colors.length == 0 || k <= 0) return;
int start = 0;
for (int i = 0; i < k - 1; i++) {
start = partition(colors, start, colors.length - 1, i);
}
/*
int end = colors.length - 1;
for (int i = 0; i < k - 1; i++) {
end = helper(colors, 0, end, k - i - 1);
}
*/
}
// quick sort partion function template
private int partition(int[] nums, int start, int end, int pivot) {
int low = start, high = end;
while (low <= high) {
while(low < high && nums[low] <= pivot) low++;
while(high > 0 && nums[high] > pivot) high--;
if (low <= high) swap(nums, low++, high--);
}
return low - 1;
}
private void swap(int[] nums, int x, int y){
int temp = nums[x];
nums[x] = nums[y];
nums[y] = temp;
}
}
// Previous version
class Solution {
public void sortColors2(int[] colors, int k) {
if (colors == null || colors.length == 0 || k <= 0) {
return;
}
int end = colors.length - 1;
for (int i = 0; i < k - 1; i++) {
end = helper(colors, 0, end, k - i - 1);
}
}
public void swap(int[] colors, int x, int y){
int temp = colors[x];
colors[x] = colors[y];
colors[y] = temp;
}
public int helper(int[] colors, int start, int end, int pivot) {
int low = start;
int high = end;
while (low <= high) {
while(low < high && colors[low] <= pivot) {
low++;
}
while(high > 0 && colors[high] > pivot) {
high--;
}
if (low <= high) {
swap(colors, low, high);
low++;
high--;
}
}
return low - 1;
}
}
```