Quick sort

Computer Science · Algorithms

Question 1 of 10

0

    Quick sort (ascending) on [28, 20, 6, 24, 35]. Lomuto partition, last item as pivot: scan left to right; an item that is less than or equal to the pivot is swapped into the block at the left. The pivot is then swapped into the boundary position. A swap is counted only when its two positions differ. In this first partition, which value is the pivot?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Quick sort explained

    Quick sort is a divide and conquer sorting algorithm. It picks one item in the list as the pivot, moves every smaller item to its left and every larger item to its right, and then sorts the two sides in the same way.

    Splitting the list around the pivot is called partitioning. After one partition, the pivot is in its final sorted position, and the two sides are smaller problems that can be solved independently. A list with zero or one item is already sorted, which is the base case. Quick sort is usually very fast, with an average time of O(nlog⁡n)O(n \log n), but its worst case is O(n2)O(n^2), which happens when the pivot is always the smallest or largest item.

    A common mistake is to assume the pivot must end up in the middle of the list. It goes wherever its correct sorted position is, which may be at either end.

    Worked example

    Partition [5, 3, 8, 4, 2] using the last item as the pivot.

    1. The pivot is 2. Compare each other item with it. None is smaller than 2, so nothing needs to move to the left of the pivot.
    2. Swap the pivot with the first item: [2, 3, 8, 4, 5].
    3. The pivot 2 is now in its final place at index 0. The right side, [3, 8, 4, 5], is sorted in the same way next.

    The finished list is [2, 3, 4, 5, 8].

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      Quick sort (ascending) on [12, 19, 2, 27]. Lomuto partition, last item as pivot: scan left to right; an item that is less than or equal to the pivot is swapped into the block at the left. The pivot is then swapped into the boundary position. A swap is counted only when its two positions differ. In this first partition, which value is the pivot?

      • 27
      • 12
      • 19
      • 2
    • Medium

      Quick sort (ascending) on [25, 7, 38, 21, 31, 8]. Lomuto partition, last item as pivot: scan left to right; an item that is less than or equal to the pivot is swapped into the block at the left. The pivot is then swapped into the boundary position. A swap is counted only when its two positions differ. After this first partition of the whole list, what is the value at index 1 (counting from 0)?

    • Hard

      Quick sort (ascending) on [11, 36, 38, 37, 30, 21]. Lomuto partition, last item as pivot: scan left to right; an item that is less than or equal to the pivot is swapped into the block at the left. The pivot is then swapped into the boundary position. A swap is counted only when its two positions differ. In this first partition of the whole list, how many swaps are made?