-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathDutchNationalFlagAlgo.java
More file actions
46 lines (45 loc) · 1.01 KB
/
DutchNationalFlagAlgo.java
File metadata and controls
46 lines (45 loc) · 1.01 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
import java.util.Scanner;
public class DutchNationalFlagAlgo
{
public static void sortColors(int[] nums) {
int mid=0;
int low=0;
int high=nums.length - 1;
while(mid<=high)
{
if(nums[mid]==0)
{
int temp = nums[low];
nums[low] = nums[mid];
nums[mid] = temp;
mid++;
low++;
}
else if (nums[mid]==1)
{
mid++;
}
else
{
int temp = nums[high];
nums[high] = nums[mid];
nums[mid] = temp;
high--;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
sortColors(arr);
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
}
}