-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort01.java
33 lines (31 loc) · 940 Bytes
/
Sort01.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
import java.util.*;
public class Sort01{
public static void PrintArray(int arr[]){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}
public static void Sort(int arr[]){
int start=0, end=arr.length-1;
while(start<end){
if (arr[start]==0 && start<end)
start++;
else if (arr[end]==1 && start<end)
end--;
else{ // arr[start]==0 && arr[end]==1 && start < end
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
}
}
PrintArray(arr);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int arr[]=new int[size];
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();
sc.close();
Sort(arr);
}
}