This homework deals with the problem of partitioning an arra

This homework deals with the problem of partitioning an array. We\'ll define partitioning to mean arranging the elements of an array around a certain \"pivot, \" element such that all elements to the left of the pivot are strictly less than the pivot, and all elements to the right of the pivot are greater than or equal to the pivot. To simplify things a little bit, we\'ll assume that the pivot element is always the first element (index 0) of the original array (i.e., before any rearranging of element is performed). For example, suppose you have an array containing the elements {10, 7, 45, -44, 33, 1}. Here the pivot would be the original first element, which is 10. If I asked you to partition this array, here are some possible results: {7, 1, -44, 10, 45, 33} {-44, 1, 7, 10, 45, 33} {1, 7, -44, 10, 35, 45} Note that there\'s more than one valid way to partition - the only requirement is that all elements to the left of the pivot are less than the pivot, and all elements to the right of the pivot are greater than or equal to the pivot. Also note that partitioning is NOT the same as sorting the array - sorting means to arrange the array elements such that they are all in order from least to greatest. As you can see from the example above, partitioning alone does not guarantee this. However, partitioning is an important first step in some sorting algorithms! Consider this algorithm for partitioning an array a: a. Create two new arrays called lesser and greater. These will be used to store the elements of a that are less than or greater than the pivot element, respectively. b. Loop through all elements of a besides the pivot. If the elements is less than the pivot, copy it into the lesser array. If the element is greater than or equal to the pivot, copy it into the greater array. c. Create a new array called result, of the same length as a. d. Copy the elements of lesser into result. e. Copy the pivot element itself into result. f. Copy the elements of greater into result. g. Return the partitioned array result. Write a method public static double [] partion (double[]a) that implements this algorithm.

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

--------------------------------------------------------------

OUTPUT:

Result Array : [7.0, -44.0, 1.0, 10.0, 45.0, 33.0]

 This homework deals with the problem of partitioning an array. We\'ll define partitioning to mean arranging the elements of an array around a certain \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site