Skip to content
Tactiverge

Learning Rooms/○ Level 3 · Innovator/Computing and algorithms

Getting Sorted & Big O Notation - Computerphile

Computerphile · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Implement insertion sort and trace it by hand on a small array.
  • Count comparisons in the best case (n − 1) and worst case (n(n − 1)/2).
  • Compare insertion sort with selection sort on sorted, reversed and nearly sorted inputs.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

Insertion sort and selection sort race on an already sorted list of 10 items. Which makes fewer comparisons?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute. It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The sorting race shows two bar charts of the same input, one for insertion sort and one for selection sort, with a running comparison count. For n = 8: sorted input costs insertion sort 7 comparisons and selection sort 28; reversed input costs both 28; shuffled input usually costs insertion sort noticeably fewer.

3.Watch, actively

Compares simple sorts by counting the work they do, and introduces big-O as a way to talk about how that work grows.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch how the presenter counts the work each sort does and why the count matters more than the code length.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

Comparing algorithms means counting the work they do, not timing one computer. Simple sorts compare roughly every pair, so their work grows with the square of the list length; big-O notation names that growth rate.

What the video covers (our own note, written when we verified it): Compares simple sorts by counting the work they do, and introduces big-O as a way to talk about how that work grows.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1

How many comparisons does insertion sort make on a reversed list of 10 items?

comparisons

Practice 2 · a fresh version every time

Part masses (g) arrive as a = [36, 35, 27, 11, 5]. They are sorted with insertion sort, where each new key moves left while j >= 0 and a[j] > key. Counting every evaluation of a[j] > key, how many comparisons are made?

comparisons

Practice 3

A 1,000-item log is sorted except for two neighbouring entries that are swapped. Roughly how many comparisons does insertion sort make?

Practice 4

Two runs both drew 1.5 A; run 4 is listed before run 9. After a stable sort by current, which comes first?

5.Try it yourself

No simulation models this idea yet. The lesson below has an interactive diagram instead.

6.Build something with it

Follow-up project

Generate lists of 100, 200 and 400 random numbers, and nearly sorted lists of the same sizes. Count insertion-sort comparisons for each and put them in a table. Does doubling n roughly quadruple the count for random data? What happens for nearly sorted data?

Where this video lives

In the course catalogue: CSE-320 Algorithms and numerical computation

The video is by Computerphile, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.