-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_Sort_0_1_2.java
More file actions
40 lines (33 loc) · 932 Bytes
/
Copy patharray_Sort_0_1_2.java
File metadata and controls
40 lines (33 loc) · 932 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
39
40
public class array_Sort_0_1_2 {
static void swap(int arr[], int l, int r) {
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
static void sort012(int arr[]) {
int n = arr.length;
int l = 0;
int m = 0;
int h = n - 1;
while (m < h) {
if (arr[m] == 0) {
swap(arr, l, m);
l++;
m++;
}
else if (arr[m] == 1)
m++;
else if (arr[m] == 2) {
swap(arr, m, h);
h--;
}
}
for (int i = 0; i < n; i++)
System.out.print(arr[i] + "\t");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int A[] = { 1, 1, 0, 1, 0, 2, 0, 2, 0, 1, 2, 1, 0, 1, 0, 2, 0, 1, 2, 2 };
sort012(A);
}
}