?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Identifying the Sort Algorithm and Number of Passes 1

APCSA-DVGYEN

Consider the following sort method.

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

What type of sort algorithm is this method using and how many passes through the for loop will it take to sort the following array?

double[] nums = {52.3, 20.25, 5.52, 20.5, 67.45, 10.81};
sorter(nums);
A

Merge sort which takes 6 passes to sort.

B

Insertion sort which takes 6 passes to sort.

C

Merge sort which takes 5 passes to sort.

D

Selection sort which takes 6 passes to sort.

E

Insertion sort which takes 5 passes to sort.