-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort012.java
33 lines (31 loc) · 912 Bytes
/
Sort012.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
import java.util.Arrays;
class Sort012{
public static void sort012(int arr[]){
int left=0,right=arr.length-1;
int current=0,temp;
while(current<=right){
if(arr[current]==0){
temp=arr[left];
arr[left]=arr[current];
arr[current]=temp;
left++;
current++;
}
else if(arr[current]==2){
temp=arr[right];
arr[right]=arr[current];
arr[current]=temp;
right--;
}
else{
current++;
}
}
}
public static void main(String[] args) {
int arr[]={2,2,0,1,0,2,0,0,0,1,1,2,1,2,0};
System.out.println(Arrays.toString(arr));
sort012(arr);
System.out.println(Arrays.toString(arr));
}
}