Question: Problem #5 Recursive Selection Sort Convert the regular iterative selection sort method ( IntSelectionSorter.java Download IntSelectionSorter.java) into a recursive one. In order to achieve that,
Problem #5 Recursive Selection Sort
Convert the regular iterative selection sort method (IntSelectionSorter.javaDownload IntSelectionSorter.java) into arecursiveone. In order to achieve that, replace the outer loop in the solution by recursive calls and the second (inner) loop with the recursive recFindMin() method calls.
The signature of the recursive method will be recSelectionSort(int[] array, int startIndex)
Writea "wrapper" method selectionSort(int[] a) that calls on the recursive selection sort method. The only purpose of it is to provide the expected Selection Sort method signature and hide additional parameters of the recursive method.
Requirement:Your method must not use any global variables or fields. Only local variables can be used in the method implementation.
/** The IntSelectionSorter class provides a public static method for performing a selection sort on an int array. */ public class IntSelectionSorter { /** The selectionSort method performs a selection sort on an int array. The array is sorted in ascending order. @param array The array to sort. */ public static void selectionSort(int[] array) { int startScan; // Starting position of the scan int index; // To hold a subscript value int minIndex; // Element with smallest value in the scan int minValue; // The smallest value found in the scan // The outer loop iterates once for each element in the // array. The startScan variable marks the position where // the scan should begin. for (startScan = 0; startScan < (array.length-1); startScan++) { // Assume the first element in the scannable area // is the smallest value. minIndex = startScan; minValue = array[startScan]; // Scan the array, starting at the 2nd element in // the scannable area. We are looking for the smallest // value in the scannable area. for(index = startScan + 1; index < array.length; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } // Swap the element with the smallest value // with the first element in the scannable area. array[minIndex] = array[startScan]; array[startScan] = minValue; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
