Answer the given question with a proper explanation and step-by-step solution. Part 1: Implement the quickselect median algorithm Implement the median algorithm from test 1, which is called the "quickselect" algorithm (due to it's relationship to quicksort). Note, it can be used to find the i-th smallest element for any value of i, but we'll just use it to find the median (i = length/2) The algorithm uses the partition helper method (provided) to find the median. The partition method moves small elements, to the left side, and big elements to the right side. Finding the median requires moving all elements smaller than the median to the left half, and all elements greater to the right half. We can call repeatedly partition in a binary-search-like manner to quickly put the median in the right place. Implement the driver method public static int median(int[] arr) which returns the median value, and modifies the input array so all small elements are in the left half, and all big elements are in the right half. Write a private, recursive method static int median(int[] arr, int begin, int end) to do the bulk of the work. package exammakeup; import java.util.Collections; import java.util.Random; /** * Method for computing the median of an int[], along with some other helpers **/ public class Median { //TODO: implement recursive median method and the public driver /** * Driver for quicksort * @param arr the array to sort */ public static void quickSort(int[] arr){ quickSort(arr, 0, arr.length); } /** * Recursive quicksort method * @param arr * @param begin inclusive index * @param end exclusive index */ private static void quickSort(int[] arr, int begin, int end){ if((end - begin) < 2) return; var pivot = partition(arr, begin, end, getPivotIndex(begin, end)); quickSort(arr, begin, pivot); quickSort(arr, pivot + 1, end); } /** * Partition the sub-array so all elements left of the pivot are less than or equal to it * and all elements to its right are greater than it * @param arr the array to partition * @param begin first element in the range to be partitioned * @param end one-past the end of the range to be partitioned (ie end is EXclusive) * @param pivotIndex index of element to use as the pivot * @return index of the pivot element after partitioning */ public static int partition(int[] arr, int begin, int end, int pivotIndex){ int left = begin; int right = end -1; var pivot = arr[pivotIndex]; //move pivot out of the way swap(arr, pivotIndex, right); right--; while(left <= right){ while(left < end -1 && arr[left] <= pivot){ left++; } while(right >= begin && arr[right] > pivot){ right--;} if(left < right) { swap(arr, left, right); left++; right--; } } swap(arr, left, end -1); return left; } private static void swap(int[] arr, int i, int j){ var tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } public static void shuffle(int[] arr){ Random r = new Random(); for(var i = arr.length - 1; i > 0; i--){ var j = r.nextInt(i + 1); swap(arr, i, j); } } /// use the middle element, which will usually be fine. private static int getPivotIndex(int begin, int end){ return (begin + end)/2; } } With Java
Answer the given question with a proper explanation and step-by-step solution.
Part 1: Implement the quickselect median
Implement the median algorithm from test 1, which is called the "quickselect" algorithm (due to it's relationship to quicksort). Note, it can be used to find the i-th smallest element for any value of i, but we'll just use it to find the median (i = length/2)
The algorithm uses the partition helper method (provided) to find the median. The partition method moves small elements, to the left side, and big elements to the right side. Finding the median requires moving all elements smaller than the median to the left half, and all elements greater to the right half. We can call repeatedly partition in a binary-search-like manner to quickly put the median in the right place.
Implement the driver method public static int median(int[] arr) which returns the median value, and modifies the input array so all small elements are in the left half, and all big elements are in the right half. Write a private, recursive method static int median(int[] arr, int begin, int end) to do the bulk of the work.
package exammakeup;
import java.util.Collections;
import java.util.Random;
/**
* Method for computing the median of an int[], along with some other helpers
**/
public class Median {
//TODO: implement recursive median method and the public driver
/**
* Driver for quicksort
* @param arr the array to sort
*/
public static void quickSort(int[] arr){
quickSort(arr, 0, arr.length);
}
/**
* Recursive quicksort method
* @param arr
* @param begin inclusive index
* @param end exclusive index
*/
private static void quickSort(int[] arr, int begin, int end){
if((end - begin) < 2) return;
var pivot = partition(arr, begin, end, getPivotIndex(begin, end));
quickSort(arr, begin, pivot);
quickSort(arr, pivot + 1, end);
}
/**
* Partition the sub-array so all elements left of the pivot are less than or
equal to it
* and all elements to its right are greater than it
* @param arr the array to partition
* @param begin first element in the range to be partitioned
* @param end one-past the end of the range to be partitioned (ie end is
EXclusive)
* @param pivotIndex index of element to use as the pivot
* @return index of the pivot element after partitioning
*/
public static int partition(int[] arr, int begin, int end, int pivotIndex){
int left = begin;
int right = end -1;
var pivot = arr[pivotIndex];
//move pivot out of the way
swap(arr, pivotIndex, right);
right--;
while(left <= right){
while(left < end -1 && arr[left] <= pivot){ left++; }
while(right >= begin && arr[right] > pivot){ right--;}
if(left < right) {
swap(arr, left, right);
left++;
right--;
}
}
swap(arr, left, end -1);
return left;
}
private static void swap(int[] arr, int i, int j){
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
public static void shuffle(int[] arr){
Random r = new Random();
for(var i = arr.length - 1; i > 0; i--){
var j = r.nextInt(i + 1);
swap(arr, i, j);
}
}
/// use the middle element, which will usually be fine.
private static int getPivotIndex(int begin, int end){
return (begin + end)/2;
}
}
With Java
Step by step
Solved in 5 steps with 2 images