Solved

Consider the Following Implementation of Insertion Sort

Question 6

Multiple Choice

Consider the following implementation of insertion sort:
Public static void insertionSort(int [ ] array) {
Int unsortedValue;// The first unsorted value
Int scan;// Used to scan the array
// The outer loop steps the index variable through
// each subscript in the array,starting at 1.This
// is because element 0 is considered already sorted.
For (int index = 1;index < array.length;index++) {
// The first element outside the sorted segment is
// array[index].Store the value of this element
// in unsortedValue
UnsortedValue = array[index];
// Start scan at the subscript of the first element
// outside the sorted segment.
Scan = index;
// Move the first element outside the sorted segment
// into its proper position within the sorted segment.
While (scan > 0 && array[scan-1] > unsortedValue) {
Array[scan] = array[scan - 1];
Scan --;
}
// Insert the unsorted value in its proper position
// within the sorted segment.
Array[scan] = unsortedValue;
}
}
This method uses the < and > operators to compare array subscripts,as when index is compared against the length of the array,a.length.The method also uses these operators to compare array elements against each other,for example,in an expression such as a[scan-1] >unSortedValue.What would happen if we change every < operator to >,and change every > operator to < ?


A) Instead of sorting in ascending order,the method would sort in descending order
B) The method would throw an array index out of bounds exception
C) The method would return,leaving the array unmodified
D) The method would modify the array,but the array would likely not be sorted correctly

Correct Answer:

verifed

Verified

Unlock this answer now
Get Access to more Verified Answers free of charge

Related Questions

Unlock this Answer For Free Now!

View this answer and more for free by performing one of the following actions

qr-code

Scan the QR code to install the App and get 2 free unlocks

upload documents

Unlock quizzes for free by uploading documents