w3resource

Java: Move every positive number to the right and every negative number to the left of a given array of integers


Move Positives Right

Write a Java program to move every positive number to the right and every negative number to the left of a given array of integers.

Visual Presentation:

Java Basic Exercises: Move every positive number to the right and every negative number to the left of a given array of integers.


Sample Solution:

Java Code:

import java.util.*;

public class Solution {
    // Method to split and sort an array
    public static int[] split_sorting_array(int[] nums) {
        // Check if the input array is null
        if (nums == null) {
            throw new IllegalArgumentException("Null array......!"); // Throw an exception for null array
        }
        
        boolean flag = true; // Initialize flag to indicate array status
        while (flag) {
            flag = false; // Set flag to false initially
            
            // Iterate through the array to perform sorting
            for (int j = 0; j < nums.length - 1; j++) {
                if (nums[j] > nums[j + 1]) { // Check if the current element is greater than the next element
                    swap(nums, j, j + 1); // Swap the elements if they are in the wrong order
                    flag = true; // Set flag to true to indicate that swapping occurred
                }
            }
        }
        return nums; // Return the sorted array
    }
    
    // Method to swap elements in the array
    private static void swap(int[] nums, int left, int right) {
        int temp = nums[right]; // Store the value of the right index in a temporary variable
        nums[right] = nums[left]; // Assign the value of left index to the right index
        nums[left] = temp; // Assign the stored value to the left index
    }
    
    public static void main(String[] args) {
        int[] nums = {-2, 3, 4, -1, -3, 1, 2, -4, 0}; // Initialize the input array
        System.out.println("\nOriginal array: " + Arrays.toString(nums)); // Display the original array
        
        int[] result = split_sorting_array(nums); // Obtain the result of split and sorting
        System.out.println("\nResult: " + Arrays.toString(result)); // Display the result
    }
} 

Sample Output:

Original array: [-2, 3, 4, -1, -3, 1, 2, -4, 0]

Result: [-4, -3, -2, -1, 0, 1, 2, 3, 4]

Flowchart:

Flowchart: Java exercises: Move every positive number to the right and every negative number to the left of a given array of integers.



For more Practice: Solve these Related Problems:

  • Write a Java program to rearrange an array so that all even numbers precede odd numbers.
  • Write a Java program to stably partition an array into negative and positive numbers while preserving their order.
  • Write a Java program to rearrange an array so that negatives appear first, zeros in the middle, and positives at the end.
  • Write a Java program to reverse only the positive numbers in an array while leaving negative numbers in their original positions.

Go to:


PREV : Divide Using Subtraction.
NEXT : Integer to String Format.


Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.