-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDutchNationalFlagProblem.java
More file actions
49 lines (44 loc) · 1023 Bytes
/
DutchNationalFlagProblem.java
File metadata and controls
49 lines (44 loc) · 1023 Bytes
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
package algorithm.search;
import java.util.Arrays;
/**
* The type Dutch national flag problem.
*/
public class DutchNationalFlagProblem {
/**
* Swap int [ ].
*
* @param arr the arr
* @return the int [ ]
*/
public static int[] swap(int[] arr){
int l =arr.length;
int i =0,j=l-1,mid=0;
int temp;
while(mid<=j){
if(arr[mid]==0){
temp=arr[mid];
arr[mid++]=arr[i];
arr[i++]=temp;
}
else if(arr[mid]==2){
temp=arr[mid];
arr[mid]=arr[j];
arr[j--]=temp;
}
else
mid++;
}
return arr;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[])
{
int[] arr = {0,2, 0, 0, 1, 2, 1};
swap(arr);
System.out.println(Arrays.toString(arr)); // import java.util.Arrays; for this functionality
}
}