Question: If a list is already sorted, how many comparisons will the insertionSort method perform? / * * The Insertion Sort method * / public static

If a list is already sorted, how many comparisons will the insertionSort method perform?
/** The Insertion Sort method */
public static void insertionSort(int[] list){
for (int i =1; i < list.length; i++){
/** insert list[i] into a sorted sublist list[0..i-1] so that list[0..i] is sorted. */
int currentElement = list[i];
int k;
for (k = i -1; k >=0 && list[k]> currentElement; k--){
list[k +1]= list[k];
}
// Insert the current element into list[k+1]
list[k +1]= currentElement;
}
}
a.
n/2 times.
b.
n +1 times.
c.
n -1 times.
d.
n times.
e.
2n times.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!