Skip to content

Commit 0c2bc16

Browse files
committed
edit dutch national flag
1 parent d940c16 commit 0c2bc16

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Dutch_national_flag.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.io.*;
2+
3+
class countzot {
4+
5+
// Sort the input array, the array is assumed to
6+
// have values in {0, 1, 2}
7+
static void sort012(int a[], int arr_size)
8+
{
9+
int lo = 0;
10+
int hi = arr_size - 1;
11+
int mid = 0, temp = 0;
12+
while (mid <= hi) {
13+
switch (a[mid]) {
14+
case 0: {
15+
temp = a[lo];
16+
a[lo] = a[mid];
17+
a[mid] = temp;
18+
lo++;
19+
mid++;
20+
break;
21+
}
22+
case 1:
23+
mid++;
24+
break;
25+
case 2: {
26+
temp = a[mid];
27+
a[mid] = a[hi];
28+
a[hi] = temp;
29+
hi--;
30+
break;
31+
}
32+
}
33+
}
34+
}
35+
36+
/* Utility function to print array arr[] */
37+
static void printArray(int arr[], int arr_size)
38+
{
39+
int i;
40+
for (i = 0; i < arr_size; i++)
41+
System.out.print(arr[i] + " ");
42+
System.out.println("");
43+
}
44+
45+
/*Driver function to check for above functions*/
46+
public static void main(String[] args)
47+
{
48+
int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
49+
int arr_size = arr.length;
50+
sort012(arr, arr_size);
51+
System.out.println("Array after seggregation ");
52+
printArray(arr, arr_size);
53+
}
54+
}

0 commit comments

Comments
 (0)