-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortColors.java
More file actions
45 lines (38 loc) · 1.19 KB
/
SortColors.java
File metadata and controls
45 lines (38 loc) · 1.19 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
/* *****************************************************************************
* Name: Jaya Mukherjee
* Last modified: Jan 1, 2022
**************************************************************************** */
import java.util.Scanner;
public class SortColors {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = scn.nextInt();
}
int low = 0, curr = 0, high = n - 1;
while (curr <= high) {
if (arr[curr] == 0) {
int temp = arr[curr];
arr[curr] = arr[low];
arr[low] = temp;
curr++;
low++;
}
else if (arr[curr] == 2) {
int temp = arr[curr];
arr[curr] = arr[high];
arr[high] = temp;
high--;
}
else {
curr++;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}