forked from Krushna-Prasad-Sahoo/JavaCode-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArraySort.java
47 lines (42 loc) · 1.08 KB
/
ArraySort.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
import java.util.*;
public class Main {
public static void main(String[] args) {
int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
// int arr[] = {0, 1, 2, 0, 1, 2};
int size = arr.length;
System.out.print("Array Before Sorting.");
for(int x : arr){
System.out.print(x + " ");
}
System.out.print("Array After Sorting.");
arr = array_sort(arr, arr_size);
for(int x : arr){
System.out.print(x + " ");
}
}
public static int[] array_sort(int a[], int size)
{
int l = 0;
int h = size - 1;
int m = 0, temp = 0;
while (m <= h) {
if(a[m] == 0){
temp = a[l];
a[l] = a[m];
a[m] = temp;
l++;
m++;
}
else if(a[m] == 1){
m++;
}
else {
temp = a[m];
a[m] = a[h];
a[h] = temp;
h--;
}
}
return a;
}
}