Repository navigation
Expand file tree
/
Copy pathBinary Array Sorting
More file actions
70 lines (53 loc) · 1.58 KB
/
Copy pathBinary Array Sorting
File metadata and controls
70 lines (53 loc) · 1.58 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
import java.io.*;
import java.lang.*;
class Sorting
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String st[] = read.readLine().trim().split(" ");
//reading size of array
int n =Integer.parseInt(st[0]);
//creating a new array of size n
int arr[] = new int[n];
st = read.readLine().trim().split(" ");
//adding elements to the array
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(st[i]);
//calling the method binSort() of
//class BinarySort()
new BinarySort().binSort(arr, n);
//printing the elements of array
for(Integer x: arr)
System.out.print(x + " ");
System.out.println();
}
}
}
class BinarySort
{
static void binSort(int arr[], int n)
{
// add your code here
int i=-1,j=n;
while(true){
do{
i++;
}while(i<n && arr[i]==0);
do{
j--;
}while(j>=0 && arr[j]==1);
if(i>=j)
return;
else{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}