Skip to main content
Codepiko
TR
All activities

To save a PDF, choose "Save as PDF" as the destination in the print window.

Sorting and searching

The big idea

Of two algorithms that do the same job, one can be much faster. In a sorting network many comparisons happen at the same time. Looking in the middle of a sorted list (binary search) rules out half of the places left with every look.

Where do we meet it?

Search engines, contact lists and dictionaries search sorted lists. Game leaderboards, a shop’s “cheapest first” button and the files on a computer are put in order by sorting algorithms.

40 minutes in classFor teachers

  1. 5 minGetting attentionSearch the dictionaryAsk: “How do you find a word in a dictionary? Do you start on the first page and go one by one?” Write the pupils’ methods on the board.
  2. 15 minLessonA sorting network on the floorDraw the six-wire network from the sheet large on the floor with chalk or tape. Six pupils enter on the left, each holding a number card; at every crossing two pupils meet and compare cards, and the smaller takes the upper path. Show that they leave in order, then play the Sorting Network game together on the board.
  3. 12 minLessonHidden number racePairs play with the “Hidden number” table on the sheet. Then, in the Hidden Number game, they compare unsorted boxes with sorted ones: where did they open fewer boxes?
  4. 8 minAssessmentHow many looks?Ask the board: “In a sorted list of 1000 people, how many looks do you need at most if you look in the middle?” Find the answer (10) together by halving. Exit card: “Why does binary search only work on a sorted list?”

Codepiko · Computer science: Sorting and searching

Sort and find

Goal: Sort by comparing, and search fast by looking in the middle.

Time: 35 min · You need: Pencil; for class play, chalk or floor tape

Name:
Class:
Date:

1. Sorting network

Numbers travel left to right along the wires. Where a line joins two wires, compare the two numbers: the smaller moves to the upper wire, the larger to the lower wire. After each column, write the numbers in the boxes.

In8312591Out

2. Hidden number

A friend picks a number from 1 to 100. After every guess they say “higher” or “lower”. Write your guesses in the table. Then swap. Who found it in fewer guesses?

#GuessWhat did your friend say?
1
2
3
4
5
6
7
8

codepiko.com