Sorting Algorithms MCQ Practice

Question 1 of 10

Kite-festival wind readings are stored in wind. After the first min-based selectionSort pass, what are the contents of wind?

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[] wind = {11, 4, 8, 15, 6};
selectionSort(wind);
  1. A. {4, 6, 8, 11, 15}
  2. B. {4, 11, 8, 15, 6}
  3. C. {11, 4, 8, 6, 15}
  4. D. {6, 4, 8, 15, 11}

Question 2 of 10

A pottery kiln log uses insertionSort on {180, 165, 200}. What are the contents after the first outer-loop pass?

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. {165, 180, 200}
  2. B. {180, 165, 200}
  3. C. {165, 200, 180}
  4. D. {200, 180, 165}

Question 3 of 10

Tide-pool crab counts are ranked largest-first with selectionSortMaxFront. What are the contents of crabs after the first 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[] crabs = {3, 8, 5, 2};
selectionSortMaxFront(crabs);
  1. A. {2, 3, 5, 8}
  2. B. {8, 5, 3, 2}
  3. C. {8, 3, 5, 2}
  4. D. {3, 5, 2, 8}

Question 4 of 10

Balloon loft heights sit in an ArrayList. After two min-based selectionSort passes, what are the contents of lofts?

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

ArrayList<Integer> lofts = new ArrayList<Integer>();
lofts.add(10);
lofts.add(7);
lofts.add(12);
lofts.add(3);
lofts.add(9);
selectionSort(lofts);
  1. A. [3, 7, 12, 10, 9]
  2. B. [3, 7, 9, 10, 12]
  3. C. [3, 10, 12, 7, 9]
  4. D. [10, 7, 12, 3, 9]

Question 5 of 10

Maple sap jar masses start as {4, 9, 1, 6}. After two insertionSort passes, what are the contents?

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. {4, 9, 1, 6}
  2. B. {1, 4, 6, 9}
  3. C. {1, 4, 9, 6}
  4. D. {4, 1, 9, 6}

Question 6 of 10

Boardwalk plank lengths are {8, 1, 5, 3}. Min-based selectionSort uses three outer-loop passes on this four-element array. What are the contents 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. {1, 8, 5, 3}
  2. B. {1, 3, 5, 8}
  3. C. {1, 3, 8, 5}
  4. D. {8, 1, 3, 5}

Question 7 of 10

A carnival booth stores prize-ticket totals in an ArrayList and uses selectionSortMaxEnd. What are the contents after the first 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> tickets = new ArrayList<Integer>();
tickets.add(4);
tickets.add(12);
tickets.add(7);
tickets.add(9);
selectionSortMaxEnd(tickets);
  1. A. [12, 4, 7, 9]
  2. B. [4, 7, 9, 12]
  3. C. [4, 9, 7, 12]
  4. D. [4, 12, 7, 9]

Question 8 of 10

Which statement correctly contrasts one pass of min-based selection sort with one pass of insertion sort on an array?

  1. A. Selection locks the next extreme into a final sorted index; insertion only orders a growing prefix, so those values may still move
  2. B. Insertion locks each inserted value into a final sorted index; selection never swaps
  3. C. Both algorithms always leave the entire array fully sorted after a single pass
  4. D. Selection shifts a hole through the prefix; insertion swaps the largest remaining value into index 0

Question 9 of 10

A ranger log of trail markers is sorted with insertionSort. Using the table, what are the contents after pass 2?

int[] markers = {5, 8, 2, 9, 1};
Pass Array after the pass
Start {5, 8, 2, 9, 1}
1 {5, 8, 2, 9, 1}
2 ?
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, 5, 8, 9, 1}
  2. B. {5, 8, 2, 9, 1}
  3. C. {1, 2, 5, 8, 9}
  4. D. {2, 5, 1, 8, 9}

Question 10 of 10

Cargo crate masses {6, 1, 9, 4, 3} are sorted with selectionSortMaxEnd. What are the contents after two outer-loop passes?

public static void selectionSortMaxEnd(int[] data)
{
    for (int i = data.length - 1; i > 0; i--)
    {
        int maxIndex = 0;
        for (int j = 1; j <= i; j++)
        {
            if (data[j] > data[maxIndex])
            {
                maxIndex = j;
            }
        }
        int temp = data[i];
        data[i] = data[maxIndex];
        data[maxIndex] = temp;
    }
}
  1. A. {6, 1, 3, 4, 9}
  2. B. {1, 3, 4, 6, 9}
  3. C. {6, 1, 9, 4, 3}
  4. D. {4, 1, 3, 6, 9}