The instructions to answer my question are below in bulletpo

The instructions to answer my question are below in bulletpoints, and write the code in Java, thank you.

Simple Partition The array thearray contains 20 unsorted numbers index 0-19 Write a while loop to partition them using the pivot value 104 Simple less than 104 to the left, greater than to the right No median of three, no extra swap at the end. 104 is not in the array Do not declare any variables or initialize the array or write swap() that is done for you. Implement the instructions above by placing your Code here: left = 0; leftptr = -1;//assumes a ++PREFIX to actually start at 0 right = 19; rightptr = 20;//assumes a --PREFIX to actually start at 19 while (true) {//requires a break to end the loop while ();//large number on left while ();//small number on right if() {//test to end the loop} else {swap (, );//pass swap the 2 array indexes/subscripts you want to swap}}//end while

Solution

Please follow hte code and comments for description :

CODE :

import java.util.Arrays; // required imports for the code

public class PartitionArray { // class to run the code

private static final int[] thearray = {37, 36, 27, 110, 15, 120, 34, 209, 4, 10, 84, 65, 87, 58, 98, 32, 74, 1, 144, 201}; // instance array

public static void main(String args[]) { // driver method

int pivot = 104; // pivot value

System.out.println(\"The Pivot Value is = \" + pivot); // print the pivot to the console

int i = -1, j = 20; // local variables

while (true) { // iterate till the loop is breaked

++i; // initial increment of the i
while (thearray[i] <= pivot) { // check for the pivot and the elements from the left
i++; // increment the value
}

--j; // get the initial vlaue
while (j > 0 && thearray[j] > pivot) { // check for the condition
j--; // decrement the indexex
}

if (i >= j) { // check for the condition to break the loop
break;
} else {
swap(i, j); // swap the values
}
}
System.out.println(\"The Resultant Array is : \" + Arrays.toString(thearray)); // print the data
}

private static void swap(int i, int j) { // method to swap the indexes
int temp = thearray[i]; // move the values
thearray[i] = thearray[j];
thearray[j] = temp;
}
}


OUTPUT :

The Pivot Value is = 104
Array is : [37, 36, 27, 1, 15, 74, 34, 32, 4, 10, 84, 65, 87, 58, 98, 209, 120, 110, 144, 201]


Hope this is helpful.

The instructions to answer my question are below in bulletpoints, and write the code in Java, thank you. Simple Partition The array thearray contains 20 unsorte
The instructions to answer my question are below in bulletpoints, and write the code in Java, thank you. Simple Partition The array thearray contains 20 unsorte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site