-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortAnArray.java
58 lines (44 loc) · 1.23 KB
/
SortAnArray.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
package Assignment2;
import java.io.*;
class SortAnArray {
static void sort012(int a[], int arr_size) {
int lo = 0;
int hi = arr_size - 1;
int mid = 0, temp = 0;
while (mid <= hi) {
switch (a[mid]) {
case 0: {
temp = a[lo];
a[lo] = a[mid];
a[mid] = temp;
lo++;
mid++;
break;
}
case 1:
mid++;
break;
case 2: {
temp = a[mid];
a[mid] = a[hi];
a[hi] = temp;
hi--;
break;
}
}
}
}
static void printArray(int arr[], int arr_size) {
int i;
for (i = 0; i < arr_size; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}
public static void main(String[] args) {
int arr[] = {1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1};
int arr_size = arr.length;
sort012(arr, arr_size);
System.out.println("Array after seggregation ");
printArray(arr, arr_size);
}
}