1. In an insertion sort elements are split into two parts and each part is recursively sorted. An array of one item is sorted (base case).
2. Analyse the code below. What type of sort has been coded?
3. Under what condition will a selection sort execute faster?
4. The following method should sort the numbers in the passed array into ascending order. But, it does not work. Which of the following lines is wrong?
5. Refer to the question and code above. What is wrong with the line you identified?
6. The following code demonstrates an insertion sort.
7. If the data is already sorted in the correct order you don't need to move any values and an insertion sort would execute faster.
8. In an insertion sort, if the data is already sorted in descending order, it would result in _________________.
9. Assuming this method should sort the numbers in the passed array into ascending order, what is wrong with line 1?
10. Selection sort and Insertion sort both have nested loops that run through the data of size n approximately n squared times.
11. Considering the above, Selection sort and insertion sort perform differently on some data.
12. Merge sort is basically a Divide and Conquer algorithm to sort a given set of elements _____________ and then merge them.
13. Analyse the following code. What is happening on the line below the comment x? (refer to line 30 in the following code)
public class MyMergeSort {
private int[] array;
private int[] tempMergArr;
private int length;
public static void main(String a[]){
int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
MyMergeSort mms = new MyMergeSort();
mms.sort(inputArr);
for(int i:inputArr){
System.out.print(i);
System.out.print(" ");
}
}
public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// COMMENT X: The below step __________________?!??
doMergeSort(lowerIndex, middle);
doMergeSort(middle + 1, higherIndex);
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}
None of these answers are correct
....merges both sides of the array
...sorts the right side of the array
...sorts the left side of the array
14. The following code demonstrates:
15. The idea behind _________________is dividing the array into the sorted and unsorted subarrays.