Question: Answer the following question, please. Question 16 Which sorting method is implemented below? public int[] array; public int[] tempArr; public int length; public void sort(int[]
Answer the following question, please.
Question 16
Which sorting method is implemented below?
public int[] array; public int[] tempArr; public int length; public void sort(int[] inputArr) { array = inputArr; length = inputArr.length; tempArr = new int[length]; doSort(0, length - 1); } private void doSort(int lowerIndex, int higherIndex) { if (lowerIndex < higherIndex) { int middle = lowerIndex + (higherIndex - lowerIndex) / 2; doSort(lowerIndex, middle); doSort(middle + 1, higherIndex); doSomething(lowerIndex, middle, higherIndex); } } private void doSomething(int lowerIndex, int middle, int higherIndex) { for (int i = lowerIndex; i <= higherIndex; i++) { tempArr[i] = array[i]; } int i = lowerIndex; int j = middle + 1; int k = lowerIndex; while (i <= middle && j <= higherIndex) { if (tempArr[i] <= tempArr[j]) { array[k] = tempArr[i]; i++; } else { array[k] = tempArr[j]; j++; } k++; } while (i <= middle) { array[k] = tempArr[i]; k++; i++; } } Question 16 options:
| Selection sort | |
| Insertion sort | |
| Binary search | |
| Mergesort |
Save
Question 17
Consider the following implementation of binary search:
public static int binarySearch(int[] array, int key) { int n = array.length; int first = 0; int last = n - 1; int middle = (first + last)/2; System.out.println(array[middle]); while( first <= last ) { if ( array[middle] < key ) { first = middle + 1; } else if ( array[middle] == key ) { return middle; } else { last = middle - 1; } middle = (first + last)/2; System.out.println(array[middle]);
} return -1; } 1) What is the output of the print statements with the following arguments:
array = [0 2 4 6 8 10 12 14] key = 15
2) Give the arguments above, what does the above search algorithm return?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
