|
1 | 1 | package com.arrays;
|
2 | 2 |
|
3 |
| -/* |
4 |
| - Dutch National Flag Algorithm, or 3-way Partitioning |
| 3 | +/** |
| 4 | +Dutch National Flag Algorithm, or 3-way Partitioning |
5 | 5 |
|
6 | 6 | Sort an array of 0s, 1s and 2s
|
7 | 7 | Given an array A[] consisting 0s, 1s and 2s.
|
|
21 | 21 | Count the number of 0’s, 1’s and 2’s. After Counting, put all 0’s first, then 1’s and lastly 2’s in the array.
|
22 | 22 | We traverse the array two times. Time complexity will be O(n).
|
23 | 23 |
|
24 |
| -Best Solution |
25 |
| -https://algorithmsandme.com/dutch-national-flag-problem/ |
26 |
| - */ |
27 |
| - |
28 |
| -public class SortArrayOf0s1s2s_DutchNationalFlagAlgo { |
29 |
| -/* |
30 | 24 | Dutch national flag problem : algorithm
|
31 | 25 | Start with three pointers : reader, low and high.
|
32 | 26 | reader and low are initialized as 0 and high is initialized as last element of array as size-1.
|
33 | 27 | reader will be used to scan the array while low and high will be used to swap elements to their desired positions.
|
34 | 28 | Starting with current position of reader, follow below steps, keep in mind we need 0 at start of array
|
35 | 29 | If element at index reader is 0, swap element at reader with element at low and increment low and reader by 1.
|
36 | 30 | If element at reader is 1, do not swap and increment reader by 1.
|
37 |
| - If element at reader is 2, swap element at reader with element at high and decrease high by 1*/ |
| 31 | + If element at reader is 2, swap element at reader with element at high and decrease high by 1 |
38 | 32 |
|
| 33 | + Actually, three pointers divide array into four parts. Red, white, unknown and Blue. |
| 34 | + Every element is taken from unknown part and put into its right place. |
| 35 | + So all three other parts expand while unknown part shrinks. |
39 | 36 |
|
40 |
| - public void dutchNationalFlagAlgorithm(){ |
| 37 | +Best Solution |
| 38 | +https://algorithmsandme.com/dutch-national-flag-problem/ |
| 39 | +*/ |
41 | 40 |
|
42 |
| - } |
43 | 41 |
|
44 |
| - public static void sort012(int arr[], int arr_size) { |
45 |
| - int countZero = 0; |
46 |
| - int countOne = 0; |
47 |
| - int countTwo = 0; |
48 |
| - for (int a : arr) { |
49 |
| - if (a == 0) { |
50 |
| - countZero++; |
51 |
| - } else if (a == 1) { |
52 |
| - countOne++; |
53 |
| - } else if (a == 2) { |
54 |
| - countTwo++; |
55 |
| - } |
56 |
| - } |
| 42 | +public class SortArrayOf0s1s2s_DutchNationalFlagAlgo { |
57 | 43 |
|
58 |
| - int i = 0; |
59 |
| - while (countZero > 0) { |
60 |
| - arr[i] = 0; |
61 |
| - countZero--; |
62 |
| - i++; |
| 44 | + public static void swap(int[] input, int i, int j){ |
| 45 | + int temp = input[i]; |
| 46 | + input[i] = input[j]; |
| 47 | + input[j] = temp; |
63 | 48 | }
|
64 | 49 |
|
65 |
| - while (countOne > 0) { |
66 |
| - arr[i] = 1; |
67 |
| - countOne--; |
68 |
| - i++; |
69 |
| - } |
| 50 | + public static void dutchNationalFalgAlgorithm(int [] input){ |
| 51 | + |
| 52 | + //initialize all variables |
| 53 | + int reader = 0; |
| 54 | + int low = 0; |
| 55 | + int high = input.length - 1; |
| 56 | + |
| 57 | + while(reader <= high){ |
| 58 | + /* |
| 59 | + input always holds a permutation of the |
| 60 | + original data with input(0..(lo-1)) =0, |
| 61 | + input(lo..(reader-1))=1, input(reader..hi) is |
| 62 | + untouched, and input((hi+1)..(input.length-1)) = 2 |
| 63 | + */ |
| 64 | + if(input[reader] == 0){ |
| 65 | + /*When element at reader is 0, swap |
| 66 | + element at reader with element at index |
| 67 | + low and increment reader and low*/ |
| 68 | + swap(input, reader, low); |
| 69 | + reader++; |
| 70 | + low++; |
| 71 | + } |
| 72 | + else if(input[reader] == 1){ |
| 73 | + /* if element at reader is just |
| 74 | + increment reader by 1 */ |
| 75 | + reader++; |
| 76 | + } |
| 77 | + else if(input[reader] == 2){ |
| 78 | + /* If element at reader is 2, swap |
| 79 | + element at reader with element at |
| 80 | + high and decrease high by 1 */ |
| 81 | + swap(input, reader, high); |
| 82 | + high--; |
| 83 | + } |
| 84 | + else{ |
| 85 | + System.out.println("Bad input"); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
70 | 89 |
|
71 |
| - while (countTwo > 0) { |
72 |
| - arr[i] = 2; |
73 |
| - countTwo--; |
74 |
| - i++; |
75 | 90 | }
|
| 91 | + public static void main(String[] args) { |
| 92 | + int[] input = {2,2,1,1,0,0,0,1,1,2}; |
76 | 93 |
|
77 |
| - } |
78 |
| - |
| 94 | + dutchNationalFalgAlgorithm(input); |
79 | 95 |
|
80 |
| - /* Utility function to print array arr[] */ |
81 |
| - static void printArray(int arr[], int arr_size) { |
82 |
| - int i; |
83 |
| - for (i = 0; i < arr_size; i++) |
84 |
| - System.out.print(arr[i] + " "); |
85 |
| - System.out.println(""); |
| 96 | + for(int i=0; i<input.length; i++){ |
| 97 | + System.out.print(" " + input[i]); |
| 98 | + } |
| 99 | + } |
86 | 100 | }
|
87 | 101 |
|
88 |
| - /*Driver function to check for above functions*/ |
89 |
| - public static void main(String[] args) { |
90 |
| - int arr[] = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}; |
91 |
| - int arr_size = arr.length; |
92 |
| - sort012(arr, arr_size); |
93 |
| - System.out.println("Array after seggregation "); |
94 |
| - printArray(arr, arr_size); |
95 |
| - } |
96 |
| -} |
|
0 commit comments