levo

Algorithms

Quick sort

  • 2 things to know
  • Component 02
Start revising · £5/month

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.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

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

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

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.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

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

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

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