CS Grade 9-12

CS: Sorting Algorithms: Bubble, Selection, and Insertion

Tracing, comparing, and choosing basic sorting algorithms

View Answer Key
Name:
Date:
Score: / 12

Tracing, comparing, and choosing basic sorting algorithms

CS - Grade 9-12

Instructions: Read each problem carefully. Show your work in the space provided. Assume all sorts arrange numbers from smallest to largest unless stated otherwise.
  1. 1
    Diagram of a bubble sort pass with adjacent bars being compared and swapped left to right.

    Bubble sort compares adjacent values and swaps them when they are out of order. Starting with the list [5, 1, 4, 2, 8], write the list after one full left-to-right pass of bubble sort.

  2. 2
    Selection sort diagram showing the smallest bars moved into the first two sorted positions.

    Selection sort finds the smallest value in the unsorted part and swaps it into the next sorted position. Starting with [29, 10, 14, 37, 13], write the list after the first two passes of selection sort.

  3. 3
    Insertion sort diagram showing one highlighted bar inserted into a sorted front section.

    Insertion sort builds a sorted section at the front of the list. The current list is [2, 5, 9, 4, 7], where [2, 5, 9] is already sorted. Insert 4 into the sorted section and write the new list.

  4. 4

    Complete this comparison: Bubble sort, selection sort, and insertion sort all have worst-case time complexity O(n^2). Which one usually has the best best-case time complexity on an already sorted list if implemented with an early stop or natural stopping condition, and why?

  5. 5

    A stable sorting algorithm keeps equal values in their original relative order. In a typical implementation that swaps only when the left value is greater than the right value, is bubble sort stable? Explain your answer.

  6. 6
    Diagram showing selection sort moving a smaller item forward and reversing two equal-valued items.

    Selection sort is often not stable. Consider the list [(4, A), (4, B), (3, C)], where the number is the sort key and the letter is the item label. Explain how selection sort can change the order of equal 4 values.

  7. 7
    Bubble sort trace showing swaps during one pass as the largest bar moves to the right.

    Count the number of swaps made during one full pass of bubble sort on the list [6, 3, 5, 2]. Also write the list after that pass.

  8. 8

    A list is almost sorted: [1, 2, 3, 5, 4, 6, 7]. Choose bubble sort, selection sort, or insertion sort as the best simple algorithm for this case, and explain your choice.

  9. 9
    Selection sort comparison model showing shrinking unsorted sections for six items.

    Selection sort runs on a list with 6 items. How many total comparisons are made to find the minimum values across all passes? Assume the final one-item section does not need a pass.

  10. 10
    Already sorted bars with one left-to-right bubble sort pass and no swaps.

    An optimized bubble sort stops if a full pass makes no swaps. Starting with [1, 2, 3, 4], how many passes are needed before the algorithm stops, and why?

  11. 11
    Insertion sort trace showing a growing sorted section over several insertions.

    Trace insertion sort on [8, 4, 6, 2]. Write the list after inserting 4, after inserting 6, and after inserting 2.

  12. 12

    You need to sort 1,000,000 random numbers in a real application. Explain why bubble sort, selection sort, and insertion sort are usually poor choices for this task, and name one faster type of sorting algorithm.

LivePhysics™.com CS - Grade 9-12

More CS Worksheets

See all CS worksheets

More Grade 9-12 Worksheets

See all Grade 9-12 worksheets