Question: Shown below is the code for the insertion sort consisting of two recursive methods that replace the two nested loops that would be used in
Shown below is the code for the insertion sort consisting of two recursive methods that replace the two nested loops that would be used in its iterative counterpart: void insertionSort(int array[])
{
insert(array, 1);
}
void insert(int[] array, int i)
{
if (i < array.length)
{ int value = array[i];
int j = shift(array, value, i);
array[j] = value;
insert(array, i + 1);
}
} i
nt shift(int[] array, int value, int i)
{
int insert = i;
if (i > 0 && array[i - 1] > value)
{
array[i] = array[i - 1];
insert = shift(array, value, i - 1);
}
return insert;
} Draw the recursion tree for insertionSort when it is called for an array of length 5 with data that represents the worst case. Show the activations of insertionSort, insert and shift in the tree. Explain how the recursion tree would be different in the best case.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
