Java Program to Sort Elements of an Array in the Format Negative Numbers to Positive Numbers & Smallest to Highest

In the previous article, we have discussed about Java Program to Reverse Array Elements

In this article we are going to see how to sort elements of an array in the format negative to positive numbers and smallest to highest by using Java programming language.

Java Program to Sort Elements of an Array in the Format Negative Numbers to Positive Numbers & Smallest to Highest

As per the problem statement, there is an array having negative and positive elements, you have to sort the array so that first negative numbers then positive numbers will be there in smallest to highest order format.

For Example:

Let say there is an array arr[] = {-5, 6, -7, 3, -1, 3, 9}
Solution: {-7, -5, -1, 3, 3, 6, 9}

Let’s see different programs to understand it more clearly.

Method-1: Java Program to Sort Elements of an Array in the Format Negative Numbers to Positive Numbers & Smallest to Highest By Using Manual Sorting Approach

Approach:

  • Declare an array and take the array elements(both positive and negative numbers) as user input.
  • Then sort the array in ascending order by comparing each element by using for loop.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Array Size: ");
        int size=sc.nextInt();
        
        int[] arr=new int[size];
        
        //inserting elements
        System.out.println("Enter "+size+" elements into array:");
        for(int i=0;i<size;i++)
        {
            arr[i]=sc.nextInt();
        }
        
         System.out.println("Array elements after sort:");
         
        //ascending logic
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                if(arr[i]<arr[j])
                {
                    int temp=arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        //displaying elements
        for(int i=0;i<size;i++)
        {
            System.out.print(arr[i]+" ");
        }
    }
}

Output:

Enter the Array Size: 
6
Enter 6 elements into array:
-5 4 -3 2 -1 7
Array elements after sort:
-5 -3 -1 2 4 7

Method-2: Java Program to Sort Elements of an Array in the Format Negative Numbers to Positive Numbers & Smallest to Highest By Using Inbuilt Arrays.sort() Method

Approach:

  • Declare an array and take the array elements(both positive and negative numbers) as user input.
  • Then sort the array in ascending order by comparing each element by using for loop.
  • Print the result.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Array Size: ");
        int size=sc.nextInt();
        
        int[] arr=new int[size];
        
        //inserting elements
        System.out.println("Enter "+size+" elements into array:");
        for(int i=0;i<size;i++)
        {
            arr[i]=sc.nextInt();
        }
        
        System.out.println("Array elements after sort:");
         
        //Sorting the array in ascending logic by using inbuilt sort() method 
        Arrays.sort(arr);
        
        //displaying elements
        for(int i=0;i<size;i++)
        {
            System.out.print(arr[i]+" ");
        }
    }
}

Output:

Enter the Array Size: 
6
Enter 6 elements into array:
-5 4 -3 2 -1 7
Array elements after sort:
-5 -3 -1 2 4 7

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: