Question: //I need to get this function to work but cannot due to first if statement of function int [] sort, help appreciated: public class RecursiveSort
//I need to get this function to work but cannot due to first if statement of function int [] sort, help appreciated:
public class RecursiveSort { private static Scanner scanner;
public static int minIndex(int [] arr, int startIndex, int endIndex) { int sml= Integer.MAX_VALUE; int minIndex= 0; for (int i= startIndex; i < endIndex; i++) { if (sml> arr[i]) //we are going thru the array checking if largest integer is larger than arr[i] { sml= arr[i]; minIndex= i; } } return minIndex; } public static int [] sort(int [] arr, int startIndex, int endIndex) { if ( arr[startIndex] >= arr[endIndex] ) { //how would you get this to work return;} int MinIndex= minIndex(arr, startIndex, endIndex); //we have our MinIndex in this variable now sort int temp= arr[startIndex]; arr[startIndex]= arr[MinIndex]; arr[MinIndex]= temp; sort(arr, startIndex+1, endIndex); return arr; } public static void main(String[] args) { int [] ourArray= {6,1,2,3,5,11,12,0,1}; System.out.println("This is our array "); System.out.println(Arrays.toString(ourArray)); System.out.println("Please enter starting index here: \t"); scanner = new Scanner(System.in); int StartIndex= scanner.nextInt(); System.out.println("Please enter ending index here: \t");
int EndIndex= scanner.nextInt(); int minIndex= minIndex(ourArray, StartIndex, EndIndex); System.out.printf("For test purposes this is our minIndex: %d ", minIndex); System.out.println("Computing sort function now! "); int [] arr= sort(ourArray, StartIndex, EndIndex); System.out.println("DONE! "); System.out.println("Resulting List: "); System.out.println(Arrays.toString(arr)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
