Sorting Algorithms MCQ Practice

Question 1 of 10

Harbor buoys store depth readings in meters. selectionSort takes the smallest remaining value and swaps it into the next left slot. What are the contents of depths after the first outer-loop pass?

public static void selectionSort(int[] data)
{
    for (int i = 0; i < data.length - 1; i++)
    {
        int minIndex = i;
        for (int j = i + 1; j < data.length; j++)
        {
            if (data[j] < data[minIndex])
            {
                minIndex = j;
            }
        }
        int temp = data[i];
        data[i] = data[minIndex];
        data[minIndex] = temp;
    }
}

int[] depths = {8, 3, 6, 1, 4};
selectionSort(depths);
  1. A. {1, 3, 6, 8, 4}
  2. B. {1, 3, 4, 6, 8}
  3. C. {3, 6, 8, 1, 4}
  4. D. {8, 3, 6, 4, 1}

Question 2 of 10

A marigold stall keeps leftover seed counts in an ArrayList. insertionSort takes the next unsorted value and shifts the sorted prefix to insert it. What are the contents of packs after the first outer-loop pass?

public static void insertionSort(ArrayList<Integer> data)
{
    for (int i = 1; i < data.size(); i++)
    {
        int next = data.get(i);
        int j = i - 1;
        while (j >= 0 && data.get(j) > next)
        {
            data.set(j + 1, data.get(j));
            j--;
        }
        data.set(j + 1, next);
    }
}

ArrayList<Integer> packs = new ArrayList<Integer>();
packs.add(5);
packs.add(2);
packs.add(9);
packs.add(4);
insertionSort(packs);
  1. A. [5, 2, 9, 4]
  2. B. [2, 5, 9, 4]
  3. C. [2, 4, 5, 9]
  4. D. [2, 9, 4, 5]

Question 3 of 10

Lantern-festival scores are ranked from largest to smallest. selectionSortMaxFront repeatedly takes the largest remaining value and swaps it into the next left slot. What are the contents of scores after the first outer-loop pass?

public static void selectionSortMaxFront(int[] data)
{
    for (int i = 0; i < data.length - 1; i++)
    {
        int maxIndex = i;
        for (int j = i + 1; j < data.length; j++)
        {
            if (data[j] > data[maxIndex])
            {
                maxIndex = j;
            }
        }
        int temp = data[i];
        data[i] = data[maxIndex];
        data[maxIndex] = temp;
    }
}

int[] scores = {4, 9, 1, 6};
selectionSortMaxFront(scores);
  1. A. {9, 4, 1, 6}
  2. B. {4, 1, 6, 9}
  3. C. {9, 6, 4, 1}
  4. D. {1, 4, 9, 6}

Question 4 of 10

Quilt remnant widths are stored in remnant. After two outer-loop passes of min-based selectionSort, what are the contents of remnant?

public static void selectionSort(int[] data)
{
    for (int i = 0; i < data.length - 1; i++)
    {
        int minIndex = i;
        for (int j = i + 1; j < data.length; j++)
        {
            if (data[j] < data[minIndex])
            {
                minIndex = j;
            }
        }
        int temp = data[i];
        data[i] = data[minIndex];
        data[minIndex] = temp;
    }
}

int[] remnant = {7, 2, 9, 4};
selectionSort(remnant);
  1. A. {2, 7, 9, 4}
  2. B. {2, 4, 7, 9}
  3. C. {2, 4, 9, 7}
  4. D. {7, 2, 4, 9}

Question 5 of 10

Cocoa tasting ranks sit in ranks. After two outer-loop passes of insertionSort, what are the contents of ranks?

public static void insertionSort(int[] data)
{
    for (int i = 1; i < data.length; i++)
    {
        int next = data[i];
        int j = i - 1;
        while (j >= 0 && data[j] > next)
        {
            data[j + 1] = data[j];
            j--;
        }
        data[j + 1] = next;
    }
}

int[] ranks = {6, 1, 8, 3, 5};
insertionSort(ranks);
  1. A. {1, 3, 6, 8, 5}
  2. B. {1, 3, 5, 6, 8}
  3. C. {1, 6, 3, 8, 5}
  4. D. {1, 6, 8, 3, 5}

Question 6 of 10

An observatory logs asteroid ID numbers. After the outer-loop pass that writes a final sorted value into index 2, what are the contents of ids?

public static void selectionSort(int[] data)
{
    for (int i = 0; i < data.length - 1; i++)
    {
        int minIndex = i;
        for (int j = i + 1; j < data.length; j++)
        {
            if (data[j] < data[minIndex])
            {
                minIndex = j;
            }
        }
        int temp = data[i];
        data[i] = data[minIndex];
        data[minIndex] = temp;
    }
}

int[] ids = {12, 5, 9, 3, 7};
selectionSort(ids);
  1. A. {3, 5, 9, 12, 7}
  2. B. {3, 5, 7, 12, 9}
  3. C. {3, 5, 7, 9, 12}
  4. D. {3, 5, 9, 7, 12}

Question 7 of 10

Ferry ticket prices are stored in an ArrayList. selectionSortMaxEnd takes the largest remaining value and swaps it into the next right-hand final slot. What are the contents of prices after the first outer-loop pass?

public static void selectionSortMaxEnd(ArrayList<Integer> data)
{
    for (int i = data.size() - 1; i > 0; i--)
    {
        int maxIndex = 0;
        for (int j = 1; j <= i; j++)
        {
            if (data.get(j) > data.get(maxIndex))
            {
                maxIndex = j;
            }
        }
        int temp = data.get(i);
        data.set(i, data.get(maxIndex));
        data.set(maxIndex, temp);
    }
}

ArrayList<Integer> prices = new ArrayList<Integer>();
prices.add(5);
prices.add(9);
prices.add(2);
prices.add(7);
selectionSortMaxEnd(prices);
  1. A. [9, 5, 2, 7]
  2. B. [2, 5, 7, 9]
  3. C. [5, 7, 2, 9]
  4. D. [5, 9, 2, 7]

Question 8 of 10

Origami crease counts start as {4, 2, 3, 1}. After the first insertionSort pass the array is {2, 4, 3, 1}. Which statement is true?

public static void insertionSort(int[] data)
{
    for (int i = 1; i < data.length; i++)
    {
        int next = data[i];
        int j = i - 1;
        while (j >= 0 && data[j] > next)
        {
            data[j + 1] = data[j];
            j--;
        }
        data[j + 1] = next;
    }
}
  1. A. 2 is already in its final sorted index for the finished array
  2. B. 2 is in the correct place among the current sorted prefix, but a later pass can still move it
  3. C. The array is fully sorted after this single pass
  4. D. The pass swapped 4 with 1 to finish the sort

Question 9 of 10

Greenhouse tray IDs are sorted with min-based selectionSort. The table shows incomplete after-pass rows. What are the contents after pass 3?

int[] trays = {8, 5, 11, 2, 6, 4};
Moment Array
Start {8, 5, 11, 2, 6, 4}
After pass 1 {2, 5, 11, 8, 6, 4}
After pass 2 {2, 4, 11, 8, 6, 5}
After pass 3 ?
public static void selectionSort(int[] data)
{
    for (int i = 0; i < data.length - 1; i++)
    {
        int minIndex = i;
        for (int j = i + 1; j < data.length; j++)
        {
            if (data[j] < data[minIndex])
            {
                minIndex = j;
            }
        }
        int temp = data[i];
        data[i] = data[minIndex];
        data[minIndex] = temp;
    }
}
  1. A. {2, 4, 5, 8, 6, 11}
  2. B. {2, 4, 11, 8, 6, 5}
  3. C. {2, 4, 5, 6, 8, 11}
  4. D. {2, 5, 4, 8, 6, 11}

Question 10 of 10

Driftwood lengths start as {6, 9, 2, 4}. One clerk runs one pass of min-based selectionSort. Another runs one pass of insertionSort. Which pair of after-pass arrays is correct?

public static void selectionSort(int[] data)
{
    for (int i = 0; i < data.length - 1; i++)
    {
        int minIndex = i;
        for (int j = i + 1; j < data.length; j++)
        {
            if (data[j] < data[minIndex])
            {
                minIndex = j;
            }
        }
        int temp = data[i];
        data[i] = data[minIndex];
        data[minIndex] = temp;
    }
}

public static void insertionSort(int[] data)
{
    for (int i = 1; i < data.length; i++)
    {
        int next = data[i];
        int j = i - 1;
        while (j >= 0 && data[j] > next)
        {
            data[j + 1] = data[j];
            j--;
        }
        data[j + 1] = next;
    }
}
  1. A. selection {2, 4, 6, 9}; insertion {2, 6, 9, 4}
  2. B. selection {6, 9, 2, 4}; insertion {2, 9, 6, 4}
  3. C. both algorithms yield {2, 4, 6, 9}
  4. D. selection {2, 9, 6, 4}; insertion {6, 9, 2, 4}