-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortColors.java
More file actions
38 lines (31 loc) · 752 Bytes
/
SortColors.java
File metadata and controls
38 lines (31 loc) · 752 Bytes
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
/*Sort array of 0,1,2 without using library function */
/*Used count sort algorithm, count of each element and sorting*/
class SortColors {
public void sortColors(int[] nums) {
if(nums==null||nums.length==0){
return;
}
int c0=0;
int c1=0;
int c2=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]==0){
c0++;
}
if(nums[i]==1)
c1++;
else
c2++;
}
for(int j=0;j<c0;j++){
nums[j]=0;
}
for(int j=c0;j<c0+c1;j++){
nums[j]=1;
}
for(int j=c0+c1;j<nums.length;j++){
nums[j]=2;
}
}
}