Computer Science, asked by Maniac6644, 1 year ago

Write a program by using Scanner class to accept a set of positive and negative numbers randomly.Print all the negative numbers before the positive numbers without changing the order of the numbers.

Sample input:1,-3,6,-9,5,-6,-7,4
Sample output:-9,-7,-6,-3,1,4,5,6


Anonymous: sort it in ascending order and it will be done!
Maniac6644: do the program.....
Anonymous: easy see the copy's sorting programs!
Anonymous: see my last answer!
Anonymous: nice question !

Answers

Answered by Anonymous
10

Here is your answer

______________________________________________________________________

import java.util.*;

class selection_sort

{

   public void main()

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter the number of elements in an array");

       int n=sc.nextInt();

       int a[]=new int[n];

       System.out.println("Enter the array values randomly ! I will sort it ! I know magic");

       for(int i=0;i<n;i++)

       {

           a[i]=sc.nextInt();

       }

       int min=0;

       int temp=0;

       for(int i=0;i<(n-1);i++)

       {

           min=i;

           for(int j=i+1;j<n;j++)

           {

               if(a[j]<a[min])

               {

                   min=j;

               }

           }

           temp=a[i];

           a[i]=a[min];

           a[min]=temp;

       }

       System.out.println("Sorted array in ascending order gives all negative first then positive hehe");

       for(int i=0;i<n;i++)

       {

           System.out.println(a[i]);

       }

   }

}

If you randomly enter any integers , the output will be:

First negatives will be printed (if any ) then positives will be printed.

Output example

Input :

a[0] = 1

a[1]=9

a[2]=-5

Output:

-5

1

9

Hope it helps you

_____________________________________________________________________

Similar questions