-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSorting 1
More file actions
30 lines (28 loc) · 784 Bytes
/
Sorting 1
File metadata and controls
30 lines (28 loc) · 784 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
/*
Move all negative numbers to beginning and positive to end with constant extra space.
An array contains both positive and negative numbers in random order.
Rearrange the array elements so that all negative numbers appear before all positive numbers.
*/
import java.util.*;
class Test2{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n=sc.nextInt(), i,k, temp;
System.out.print("Enter the elements in the array: ");
int a[]=new int [n];
for(i=0; i<n; i++)
a[i]=sc.nextInt();
for(i=0; i<n-1; i++){
for(k=i+1;k<n; k++){
if(a[i]>a[k]){
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
for(i=0; i<n; i++)
System.out.print(a[i]+" ");
}
}