Programming foundations

Stage 4 · Lesson 7 of 10

Searching and sorting

Data structures and Big-O

Searching an unsorted list means checking every item, which is O(n) in . If the list is sorted, checks the middle and throws away half the list at each step, which is O(log n). That is about 20 steps for a million items.

This is why sorting matters. Simple sorts such as bubble, selection and compare pairs and take O(n²). and the sorts built into your language take O(n log n).

Implement binary search and one simple sort yourself to understand them, then use the built-in sort in real code. Binary search is famous for bugs, so test yours on lists of size 0, 1 and 2.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Step through linked lists, hash tables and sorts with your own input

  • Chapter 5 on lists, dicts, sets and deque: the built-ins you will use from now on

Also worth your time

Mark this stage as not done?