(use the code below) Assume an array containing n integers, such that the integers in the array are in a complete random order. How can
(use the code below) Assume an array containing n integers, such that the integers in the array are in a complete random order. How can we rearrange the elements in the array such that all the negative integers come before all the zeros and all the positive integers appear at the end?
Note that we are not concerned with sorting the numbers in the array, but simply with reOrderthem in the order specified.
Example #1: The list of numbers:
7 | -3 | 0 | 0 | 8 | -2 |
would be rearranged as:
-2 | -3 | 0 | 0 | 8 | 7 |
Example #2: The list of numbers:
-6 | 6 | 7 | -7 | 0 | -5 | 8 | 5 |
would be rearranged as:
-6 | -5 | -7 | 0 | 7 | 8 | 5 | 6 |
For this problem write a program ReOrder.java which creates an array of random integers and calls the function reorder, as specified below, to rearange the integers in the array such that the array has three distinct regions (also called constraints) represented by:
< 0, == 0 and > 0.
public class ReOrder { /* * TODO: You are asked to complete the method * reorder. This method takes in as input an * array of ints and returns back nothing (void). * This method must call the method swap. * You cannot change the function * signature of this method. */ public static void reorder(int[] items) { } /* * TODO: You are asked to complete the method * swap. This method takes in as input two ints * and an array of ints. The int i and int j * are the index i and index j in the array items. * You are asked to swap the value at the index i in items * with the value at the index j. You cannot change the function * signature of this method. */ private static void swap(int i,int j,int[] items) { } public static void main(String[] args) { /* You can modify the main function in any way you like. * We will not mark your main function. */ int [] items={7,-3,0,0,8,-2}; /* * printing the values in the items before * calling the method reorder. */ for(int item:items) { System.out.println(item); } //calling the reorder method ReOrder.reorder(items); /* * printing the values in the items after * calling the method reorder */ for(int item:items) { System.out.println(item); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started