-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathDutchNationalFlagAlgo.java
47 lines (39 loc) · 1.11 KB
/
DutchNationalFlagAlgo.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
import java.util.Arrays;
public class DutchNationalFlagAlgo {
static void swap(int arr[], int n, int m) {
int temp = arr[n];
arr[n] = arr[m];
arr[m] = temp;
}
static void sortArray(int arr[]) {
int low = 0;
int mid = 0;
int high = arr.length - 1;
while (mid <= high) {
if (arr[mid] == 0) {
swap(arr, low, mid);
low = low +1;
mid = mid+1;
}
else if (arr[mid] == 1) {
mid = mid+1;
}
else {
swap(arr, mid, high);
high = high - 1;
}
}
System.out.println("Array after sorting "+ Arrays.toString(arr));
}
public static void main(String args[]) {
// Input array should be having 0,1,2
int arr1[] = {1,2,0,0,1,2,1};
sortArray(arr1);
int arr2[] = {0,1,0,2,0,1,2,2};
sortArray(arr2);
int arr3[] = {1,1,0,2,1,2,0};
sortArray(arr3);
int arr4[] = {2,0,1};
sortArray(arr4);
}
}