?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Correcting an Insertion Sort Algorithm

APCSA-4FP4LT

A programmer has attempted to code an insertion sort algorithm as follows.

public static void sorter( double[] nums)
{
     for (int i=1; i < nums.length; i++)
     {
          int curr = (int) nums[i];
          int j=i;
          while(j >0 && curr < nums[j-1])
          {
               nums[j] = nums[j-1];
               j--;
          }
          nums[j] = nums[curr];
     }
}

However, it is not quite working. What needs to be changed to make this a successful insertion sort algorithm?

I. Change the while statement as follows:

while(j>0 && curr < nums[j])

II. Change the declaration of variable curr as follows:

double curr = nums[i];

III. Change the last statement in the for loop as follows:

nums[j] = curr;
A

I only.

B

II only.

C

III only.

D

I and II only.

E

II and III only.