forked from RishuRajan/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting.java
71 lines (57 loc) · 1.21 KB
/
Sorting.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.*;
import java.util.*;
public class Main {
public static void sort012(int[] arr){
//write your code here
int i= 0;
int j= 0;
int k=arr.length-1;
while(i<=k){
if(arr[i]==1 ){
i++;
}
else if(arr[i]==0){
swap(arr,i,j);
i++;
j++;
}
else{
swap(arr,i,k);
k--;
}
}
// i=j;
// while(i<arr.length){
// if(arr[i]==2){
// i++;
// }
// else{
// swap(arr,i,j);
// i++;
// j++;
// }
// }
}
// 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);
}
}