Question

In: Computer Science

Complete the class code below and include a method named partition which takes an integer array...

Complete the class code below and include a method named partition which takes an integer array and a partition integer as parameters and then partitions the original array into two new arrays where elements of the first array are all less than or equal to the partition integer and the second array are all greater than the partition integer.

The method should print the original array, followed by the lower partition and finally the upper partition. Nothing should be returned by the method.

For example, given int[ ] arr = {1, 45, 16, 7, 39, 6, 5, 11, 72, 31, 15} a method call to partition(arr, 11) should output:

   [1, 45, 16, 7, 39, 6, 5, 11, 72, 31, 15]

   [1, 5, 6, 7, 11]

   [15, 16, 31, 39, 45, 72]

public class Partition {

         public static void main(String[] args) {

              int[] arrayToPartition = {1, 45, 16, 7, 39, 6, 5, 11, 72, 31, 15};

              int partitionNumber = 11;

              partition(arrayToPartition, partitionNumber);

        }

             // your method code here

   } // end of Partition class

Solutions

Expert Solution

The explanation is done in the code itself. If you have any queries write a comment. If you have understood upvote thank you.

SOLUTION:

public class Partition {

public static void partition(int arr[],int num){
//declare two arrays to store the lower and higher elements
//here i have given its length equal to the length of the original
//array so that shere should not be any exceptions when the the given element is maximum//element in that case
int lowerArray[]=new int[arr.length];
int upperArray[]=new int[arr.length];
//pointer for index of the two arrays
int up=0,lp=0;
System.out.print("Original Array ");
//iterate through all the elements
for(int i=0;i<arr.length;i++){
//check for great and less condition and place them in the appropriate arrays and update their index
if(arr[i]<=num){
lowerArray[lp]=arr[i];
lp=lp+1;
}else{
upperArray[up]=arr[i];
up=up+1;
}
//i am printing the original array here itself
System.out.print(" "+arr[i]+" ");
  
}
//here print donot take new line while println takes it
//print all the data by checking the indexes of the higher and lower arrays
System.out.println("");
System.out.print("Lower array elements ");
for(int i=0;i<lp;i++){
System.out.print(" "+lowerArray[i]+" ");
}
System.out.println("");
System.out.print("Upper Array ");
for(int i=0;i<up;i++){
System.out.print(" "+upperArray[i]+" ");
}
}
public static void main(String[] args) {

int[] arrayToPartition = {1, 45, 16, 7, 39, 6, 5, 11, 72, 31, 15};

int partitionNumber = 11;

partition(arrayToPartition, partitionNumber);

}

} // end of Partition class
CODE AND OUTPUT:


Related Solutions

Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
Write a complete static method named addUp that accepts, in order, an integer number and an...
Write a complete static method named addUp that accepts, in order, an integer number and an integer thisMany as parameters and that prints a complete line of output reporting all the numbers that result from adding number to itself thisMany number of times (starting at 1 and ending at thisMany). For example, the following calls: addUp(3, 5); addUp(7, 3); should produce this output: Adding up 3 for 5 times gives: 3, 6, 9, 12, 15 Adding up 7 for 3...
1. Implement a public method named initialize. It takes a twodimensional square array of integers...
1. Implement a public method named initialize. It takes a two dimensional square array of integers namedarray as a parameter. It initializes all of the elements of the array to the sum of their indices except for themajor diagonal (upper left to lower right) where each element is initialized to -1. (For testing use a 4X4 or5X5 array and have the application print out the array in 2 dimension format.2. Implement a method named totals that takes a two dimensional...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Which of the following operations for an array takes the mosttime to complete?Setting the...
Which of the following operations for an array takes the most time to complete?Setting the value of an element at a given indexinserting an element at a given indexReturning the size of the arrayReturning the value of an element at a given index
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to...
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method uses 2 ref parameters: feet, inches left of type int and a parameter that is not ref of type int to which you pass inchesinches. For example, 67 inches is 5 feet 7 inches.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT