levo

Algorithms

Quick sort

  • 2 things to know
  • Component 02

Overview

Recall questions on how quick sort works, pivots and partitioning, tracing a quick sort, best, average and worst-case complexity, choosing pivots to avoid the worst case, space complexity, recursion depth, and comparing quick sort with merge sort.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Trace a quick sort on the list 7, 2, 9, 4, 1, taking the first item as the pivot.

Key term

Trace what happens when quick sort with a first-item pivot is applied to the already-sorted list 1, 2, 3, 4.

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Describe how a quick sort works, step by step.

    Model answer

    Choose one item of the list as the pivot. Partition the rest of the list so that every item smaller than the pivot is placed on its left and every item larger is placed on its right; the pivot is then in its final correct position. Apply the same procedure recursively to the sublist on the left and the sublist on the right. A sublist of zero or one item is already sorted and ends the recursion.

  2. 2

    What is a pivot?

    Model answer

    The item chosen at each stage against which every other item in the current sublist is compared, in order to split that sublist into those below it and those above it. Once the partition is complete the pivot itself is in the position it will occupy in the final sorted list and never needs to move again.

  3. 3

    Trace a quick sort on the list 7, 2, 9, 4, 1, taking the first item as the pivot.

    Model answer

    The pivot is 7. Items smaller are 2, 4, 1 and items larger are 9, giving 2, 4, 1 then 7 then 9, with 7 now fixed. Sort the left sublist 2, 4, 1 with pivot 2: smaller is 1, larger is 4, giving 1, 2, 4, and each single-item sublist is done. The right sublist 9 is a single item. Reassembling gives 1, 2, 4, 7, 9.

  4. 4

    After a single partition, what do you know for certain?

    Model answer

    That the pivot is in its final sorted position, and that every item to its left is smaller than it while every item to its right is larger. You know nothing about the order within either of those two groups, which is why each must be sorted in turn.

  5. 5

    State quick sort's best and average time complexity, and say what has to be true for them.

    Model answer

    Both are O(n log n), and both depend on the pivot splitting the sublist into two reasonably balanced halves. Each level of partitioning costs a pass over the data, and if the data halves each time there are about log n levels.

  6. 6

    State quick sort's worst-case complexity and describe exactly what input causes it.

    Model answer

    O(n squared). It occurs when the pivot is repeatedly the smallest or largest item in the sublist, so the partition produces one empty sublist and one containing everything else. If the pivot is always the first item, then a list already in sorted order, or in exactly reverse order, does this: each partition removes only one item, so there are n levels of recursion each costing a full pass, giving n multiplied by n.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

See a huge jump in your memorisation

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It trains answer structure

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Algorithms, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.