?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Identifying the Sort Algorithm and Number of Passes 2

APCSA-F4PPXF

Consider the following sort method.

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

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 = {40, 20.1, 5, 20.5, 10};
sorter(nums);
A

Selection sort which takes 5 passes to sort.

B

Insertion sort which takes 5 passes to sort.

C

Merge sort which takes 5 passes to sort.

D

Selection sort which takes 4 passes to sort.

E

Insertion sort which takes 4 passes to sort.