can anyone write a java program to sort alternative positive and negative numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can anyone write a java program to sort alternative positive and negative numbers

the give input has to be in array and the output has to be stored in another array .

15th Jul 2018, 8:25 AM
Bharathi Priya
Bharathi Priya - avatar
8 Answers
+ 4
-2 is greater than -8 so, you are trying to sort using absolute value, right? And another question: is there always the no.of positive ints == no.of negative ints ? for eg. in the example you wrote 2 +ve ints and 2 -ve ints.
15th Jul 2018, 12:58 PM
Tanay
Tanay - avatar
15th Jul 2018, 1:21 PM
D_Stark
D_Stark - avatar
+ 2
I don't get what you are trying to do. can you post the sample for input and output?
15th Jul 2018, 12:17 PM
Tanay
Tanay - avatar
+ 2
Bharathi Priya finally got some time to write a code for this problem. check this out: https://code.sololearn.com/cz5qYg48PVLl/?ref=app
15th Jul 2018, 7:12 PM
Tanay
Tanay - avatar
+ 1
Can you please post your attempt(s) at coding this?
15th Jul 2018, 8:35 AM
Janningā­
Janningā­ - avatar
+ 1
yes
15th Jul 2018, 8:35 AM
Bharathi Priya
Bharathi Priya - avatar
+ 1
import java.util.Scanner; class nonrpt{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[100]; int c=0; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<n;i++) { if(a[i]<0) { c=1; } else { c=0; break; } } } if(c==1) { for (i=0;i<n;i=i+2) System.out.println("nos:"+a[i]); } if(c==1) { for (i=1;i<n;i=i+2) System.out.println("nos:"+a[i]); } } } }
15th Jul 2018, 8:42 AM
Bharathi Priya
Bharathi Priya - avatar
+ 1
same input input []={1,6,-2,-8} output []={-2,1,-8,6}
15th Jul 2018, 12:41 PM
Bharathi Priya
Bharathi Priya - avatar