Practice Sorting in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

Rearranging items in a collection into a defined order, such as smallest to largest or alphabetical. Sorting is one of the most studied problems in computer science, with algorithms ranging from simple (bubble sort, O(n2)O(n^2)) to efficient (merge sort, O(nlogn)O(n \log n)).

Putting things in order—alphabetical, numerical, by date—so they are easier to find and use.

Showing a random 20 of 50 problems.

Example 1

easy
Sort the letters [z,a,m][\text{z},\text{a},\text{m}] alphabetically.

Example 2

challenge
A list of 88 items is already sorted. Compare the number of comparisons for optimized bubble sort vs merge sort.

Example 3

challenge
Three sorted lists of length 5 each are merged. Minimum number of comparisons in the worst case?

Example 4

challenge
You sort 1 billion 64-bit integers. Argue why a stable O(nlogn)O(n \log n) sort with O(n)O(n) extra memory may still be the wrong choice, and name what to check.

Example 5

medium
Merge two sorted lists [1,4,7][1,4,7] and [2,3,8][2,3,8]. Give the result.

Example 6

medium
How many adjacent comparisons does one full pass of bubble sort make on a list of 66 elements?

Example 7

medium
Counting sort works in O(n+k)O(n+k) where kk is the value range. For what kind of input is it a poor choice?

Example 8

hard
Compare bubble sort and merge sort in terms of time complexity. When would you choose one over the other?

Example 9

medium
A merge sort splits a list of 8 in half repeatedly. How deep does the recursion go?

Example 10

medium
Sort [4, 1, 3] using insertion sort. Show each step.

Example 11

medium
You have a list of exam scores that is already sorted except one new score was added at the end. Would insertion sort or bubble sort be a better choice, and why?

Example 12

medium
Sort the list [5, 2, 8, 1, 9] using bubble sort. Show the first two passes.

Example 13

medium
How many comparisons does insertion sort perform in the WORST case on a list of size 4?

Example 14

hard
On a list of size 16, how many comparisons does merge sort do in the worst case (approx)?

Example 15

medium
A naive bubble sort runs all n1n-1 passes even when sorted by pass 2. Name one optimization.

Example 16

medium
Why might you want to sort even though searching unsorted lists is O(n)O(n) already?

Example 17

medium
Sort [3,1,4,1,5][3, 1, 4, 1, 5] ascending and report the value at index 22 (0-indexed).

Example 18

easy
Apply one pass of selection sort to [4,2,6,1][4,2,6,1]: find the minimum and swap it to position 0.

Example 19

challenge
Argue why timsort (hybrid merge + insertion) often beats pure merge sort on real-world data.

Example 20

medium
A program sorts then must keep records with equal scores in submission order. Which property must the sort have?