-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting.java
50 lines (37 loc) · 1 KB
/
Sorting.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
// Sort objects so that objects of the same color are adjacent, with the colors in the order red, white and blue.
public class Solution {
public void sortColors(int[] nums) {
if(nums == null || nums.length < 2)
{
return;
}
int countarr[] = new int[3];
int n = nums.length;
int bsort[] = new int[n];
int i,j;
for(i=0;i<3;i++)
{
countarr[i] = 0;
}
for(j=0;j<n;j++)
{
countarr[nums[j]] = countarr[nums[j]] + 1;
}
//Pass 2
j =0;
int numsindex = 0;
while(j<=2)
{
if(countarr[j] != 0)
{
nums[numsindex] = j;
numsindex = numsindex + 1;
countarr[j] = countarr[j] - 1;
}
else
{
j++;
}
}
}
}