forked from mayankchaudhary26/HacktoberFest-Practice-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort_012.java
More file actions
43 lines (38 loc) Β· 974 Bytes
/
Sort_012.java
File metadata and controls
43 lines (38 loc) Β· 974 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
import java.util.*;
public class Sort012 {
// O(N)
public static void sort012(int[] arr){
int p1 = -1,p2 = arr.length-1,itr=0;
while(itr <= p2){
if(arr[itr] == 0)
swap(arr,itr++,++p1);
else if(arr[itr] == 2)
swap(arr,itr,p2--);
else
itr++;
}
}
// used for swapping ith and jth elements of array
public static void swap(int[] arr, int i, int j) {
System.out.println("Swapping index " + i + " and index " + j);
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr){
for(int i = 0 ; i < arr.length; i++){
System.out.println(arr[i]);
}
}
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0 ;i < n; i++){
arr[i] = scn.nextInt();
}
sort012(arr);
print(arr);
scn.close();
}
}