CS: Sorting Algorithms: Bubble, Selection, and Insertion
Tracing, comparing, and choosing basic sorting algorithms
Tracing, comparing, and choosing basic sorting algorithms
CS - Grade 9-12
- 1
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
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
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
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
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
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
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
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
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
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
Trace insertion sort on [8, 4, 6, 2]. Write the list after inserting 4, after inserting 6, and after inserting 2.
- 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.
Related Cheat Sheets
More CS Worksheets
CS: Algorithms and Flowcharts
Grade 6-8 · 12 problems
CS: Arrays and Lists
Grade 9-12 · 12 problems
CS: Big-O Notation and Algorithm Efficiency
Grade 9-12 · 12 problems
CS: Binary Numbers and Number Systems
Grade 6-8 · 12 problems
More Grade 9-12 Worksheets
Linear Equations
Math · 8 problems
Cell Biology
Biology · 8 problems
Reading Comprehension
Language Arts · 8 problems
Historical Thinking & Evidence
Social Studies · 8 problems