Java Program to Arrange the Elements of a Given Array of Integers Where All Negative Integers Appear Before All the Positive Integers

In the previous article, we have seen Java Program to Cyclically Rotate a Given Array Clockwise by One

In this article we are going to see how to arrange the elements of a given array of integers where all negative integers appear before all the positive integers using Java programming language.

Java Program to Arrange the Elements of a Given Array of Integers Where All Negative Integers Appear Before All the Positive Integers

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to arrange the elements of a given array of integers where all negative integers appear before all the positive integers.

Method-1: Java Program to Arrange the Elements of a Given Array of Integers Where All Negative Integers Appear Before All the Positive Integers By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Initialize two pointers, i=0, j=arr.length–1.
  • While i<=j, if the element at i is negative, increment i.
  • If the element at j is positive, decrement j.
  • Now at index i, there is a positive element and at index j, there is negative element, so swap these two.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] arr = { -1, 2, -3, 4, -5, 6, 7, -8, 9, -10 };
        System.out.println("The array is : ");
        //calling printArray() method
        printArray(arr);
        
        // calling the method
        modifyMethod(arr);
        
        // printing the array
       System.out.println("The modified array is : ");
       //calling printArray() method
        printArray(arr);

    }

    //modifyMethod() method to bring all negative numbers first 
    //then positive elements in array
    static void modifyMethod(int[] arr) 
    {
        // initialize two pointers
        int i = 0;
        int j = arr.length - 1;
        while (i <= j) {
            // if the element at i is negative, increment i
            if (arr[i] < 0 )
            i++;
            // if the element at j is positive, decrement j
            if (arr[j] > 0)
                j--;
            // swap the elements
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    //printArray() method to print the array 
    static void printArray(int[] arr) 
    { 
        // printing array 
        for (int i=0; i<arr.length; i++) 
        { 
            System.out.print(arr[i] + " "); 
        } 
        System.out.println("");
    }
}

Output:

The array is : 
-1 2 -3 4 -5 6 7 -8 9 -10 
The modified array is : 
-1 -10 -3 -8 6 -5 7 4 9 2

Method-2: Java Program to Arrange the Elements of a Given Array of Integers Where All Negative Integers Appear Before All the Positive Integers By Dynamic Initialization of Array Elements

Approach:

  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Initialize two pointers, i=0, j=arr.length–1.
  • While i<=j, if the element at i is negative, increment i.
  • If the element at j is positive, decrement j.
  • Now at index i, there is a positive element and at index j, there is negative element, so swap these two.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        // asking user to enter the number of elements
        System.out.println("Enter number of elements in the array: ");
        int n = sc.nextInt();
        // initializing the array
        int[] arr = new int[n];
        // asking user to enter the elements
        System.out.println("Enter elements of the array: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
         System.out.println("The array is : ");
         printArray(arr);
        
        
         // calling the method
         modifyMethod(arr);
        
         System.out.println("The modified array is : ");
         printArray(arr);

    }

    static void modifyMethod(int[] arr) 
    {
        // initialize two pointers
        int i = 0;
        int j = arr.length - 1;
        while (i <= j) {
            // if the element at i is negative, increment i
            if (arr[i] < 0 )
            i++;
            // if the element at j is positive, increment j
            if (arr[j] > 0)
                j--;
            // swap the elements
            if (i <= j) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
        }
    }
    
    //printArray() method to print the array 
    static void printArray(int[] arr) 
    { 
        // printing array 
        for (int i=0; i<arr.length; i++) 
        { 
            System.out.print(arr[i] + " "); 
        } 
        System.out.println("");
    }
}
Output:

Enter number of elements in the array: 5
Enter elements of the array: 2 3 -1 -8 -4
The array is : 
2 3 -1 -8 -4 
The modified array is : 
-4 -8 -1 3 2

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: