levo

Algorithms

Comparing algorithms and choosing one for a task

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

Overview

Recall questions on choosing between algorithms by complexity, why constants can let a slower-growing algorithm lose, hybrid sorts, memory and data size, sorting on embedded devices and servers, sorted versus unsorted searching, and Dijkstra's versus A*.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Compare searching a sorted array with searching an unsorted array of the same size.

Key term

Explain how the amount of data you expect changes which algorithm is suitable.

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Two algorithms solve the same problem, one O(n log n) and one O(n squared). Which would you choose for a list of a million items, and why?

    Model answer

    The O(n log n) one. At a million items log n is about 20, so the work is of the order of twenty million operations, whereas n squared is of the order of a million million. The gap widens as the data grows, so for large data the complexity class matters far more than any other consideration.

  2. 2

    Big O ignores constants. Explain why that means an O(n squared) algorithm can still beat an O(n log n) one.

    Model answer

    Big O describes how the cost grows, not how large it is. An O(n squared) algorithm with a very small constant cost per operation, such as insertion sort, can do less real work than an O(n log n) algorithm that has heavy overheads such as recursive calls and copying into temporary arrays. Below a crossover point of a few dozen items the simpler algorithm wins; above it the better complexity always takes over.

  3. 3

    Give a practical consequence of that crossover point.

    Model answer

    Real sorting implementations are often hybrids: they use merge sort or quick sort to break the data down, then switch to insertion sort once a sublist falls below a threshold of roughly ten to twenty items, because insertion sort finishes those small pieces with less overhead.

  4. 4

    Besides the number of operations, what else should you weigh when choosing an algorithm for a task?

    Model answer

    How much memory it needs and how much is available, the size of the data set now and in the future, whether the data is already partly sorted, whether the data arrives all at once or item by item, whether equal items must keep their original order, and how complicated the algorithm is to implement correctly and maintain.

  5. 5

    Why is complexity a fairer basis for comparing two algorithms than the time they take on one test?

    Model answer

    A single test result depends on the hardware, the language and that particular data set, and it says nothing about behaviour on data ten times larger. Complexity describes the growth of the algorithm itself, so a comparison made on complexity holds on any machine and predicts what happens as the data set grows.

  6. 6

    An embedded device has very little spare memory and must sort a list of about 30 readings. Which of merge sort and insertion sort would you choose, and why?

    Model answer

    Insertion sort. It sorts in place using O(1) extra memory, whereas merge sort needs O(n) additional storage the device may not have. With only about 30 items the O(n squared) growth costs very little in absolute terms, so the memory constraint decides it.

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.