forked from Annex5061/java-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNFSort.java
More file actions
Latest commit
49 lines (46 loc) · 1.38 KB
/
Copy pathDNFSort.java
File metadata and controls
49 lines (46 loc) · 1.38 KB
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
publicclassDNFSort {
// Sort the input array, the array is assumed to
// have values in {0, 1, 2}
staticvoidsort012(inta[], intarr_size) {
intlow = 0;
inthigh = arr_size - 1;
intmid = 0, temp = 0;
while (mid <= high) {
switch (a[mid]) {
case0: {
temp = a[low];
a[low] = a[mid];
a[mid] = temp;
low++;
mid++;
break;
}
case1:
mid++;
break;
case2: {
temp = a[mid];
a[mid] = a[high];
a[high] = temp;
high--;
break;
}
}
}
}
/* Utility function to print array arr[] */
staticvoidprintArray(intarr[], intarr_size) {
for (inti = 0; i < arr_size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
/*Driver function to check for above functions*/
publicstaticvoidmain(String[] args) {
intarr[] = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};
intarr_size = arr.length;
sort012(arr, arr_size);
System.out.println("Array after seggregation ");
printArray(arr, arr_size);
}
}