Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Sorting and searching

Trace bubble sort on paper, then compare linear search with binary search and see why a sorted list can be searched in far fewer steps.

Get the whole book: Computers and coding for classes 9 and 10 (6 lessons, free to print)

Let us begin

The big idea

Computers spend a lot of their time putting things in order and finding things. A phone contact list, a leaderboard and a dictionary all depend on sorting and searching. You can learn the classic methods with nothing but a pencil and a row of numbers.

Bubble sort puts a list in order by comparing neighbours. Go along the list; whenever the left item is bigger than the right item, swap them. After one full pass, the largest value has "bubbled" to the end. Repeat the passes until a whole pass makes no swaps; then the list is sorted.

Linear search finds a value by checking the items one by one from the start until it is found or the list ends. It works on any list, sorted or not, but in the worst case it looks at every item: 1,000 items could mean 1,000 checks.

Binary search works only on a sorted list, but it is much faster. Look at the middle item. If it is the one you want, stop. If the item you want is smaller, throw away the right half; if bigger, throw away the left half. Repeat on the remaining half. Each step halves the list, so 1,000 items need at most about 10 checks, and 1,000,000 need about 20.

This is why sorting matters: a dictionary is sorted so you can open it near the middle and jump, instead of reading from page one. The number of steps a method needs as the list grows is called its efficiency, and choosing an efficient method is a big part of computer science.

Bubble sort: the first pass on 5, 2, 4, 1Four rows show the list after each comparison in the first pass of bubble sort. Row one: 5, 2, 4, 1 with 5 and 2 highlighted and the note swap. Row two: 2, 5, 4, 1 with 5 and 4 highlighted, swap. Row three: 2, 4, 5, 1 with 5 and 1 highlighted, swap. Row four: 2, 4, 1, 5 with the note that 5 has reached the end.Pass 1 of bubble sort step 1 5 2 4 1 5 > 2, swap step 2 2 5 4 1 5 > 4, swap step 3 2 4 5 1 5 > 1, swap result 2 4 1 5 5 is at the end Next passes: [2, 1, 4, 5] then [1, 2, 4, 5]. A pass with no swaps means sorted.
One pass of bubble sort: each pair of neighbours is compared and swapped if out of order. The largest value reaches the end.

Worked examples

See how it works

  1. Bubble sort trace

    Sort [5, 2, 4, 1]. Pass 1: compare 5,2 swap to [2, 5, 4, 1]; compare 5,4 swap to [2, 4, 5, 1]; compare 5,1 swap to [2, 4, 1, 5]. Pass 2: 2,4 no swap; 4,1 swap to [2, 1, 4, 5]; 4,5 no swap. Pass 3: 2,1 swap to [1, 2, 4, 5]; then no more swaps. Pass 4 makes no swaps, so the list is sorted.

  2. Linear search

    Find 7 in [3, 9, 7, 1, 8]. Check 3: no. Check 9: no. Check 7: yes, found at index 2 after 3 checks. To find 8 would take 5 checks; to find 4, which is absent, also takes 5 checks before giving up.

  3. Binary search

    Find 23 in the sorted list [4, 8, 15, 16, 23, 42, 50]. Middle is index 3, value 16. 23 is bigger, so keep the right half [23, 42, 50]. Middle is 42. 23 is smaller, so keep [23]. Middle is 23: found in 3 checks. Linear search would have needed 5.

Try it at home

Sort and search with cards

You will need

  • Eight small pieces of paper
  • A pencil
  • A table or floor to lay the cards in a row

What to do

  1. Write a different number from 1 to 50 on each card and lay them face up in a random row.
  2. Bubble sort them: compare the first two cards and swap if the left is bigger; move one step right and repeat to the end. Count your swaps. Repeat passes until a pass has no swaps.
  3. Turn all cards face down. Ask a friend to name one of the numbers. Find it by linear search, turning cards from the left one at a time, and count the checks.
  4. Turn them face down again (still in sorted order). Find another number by binary search: turn over the middle card, decide which half to keep, and repeat. Count the checks.
  5. Compare the counts. Try with sixteen cards and see how little the binary search count grows.

Practice sheet

Check what you learned

Write your answers in a notebook, or print this sheet and write on it. When you finish, mark your work with the answer key.

Practice sheet: Sorting and searching (Classes 9-10, Computers and coding)

  1. In bubble sort, when do you swap two neighbours?
  2. After the first pass of bubble sort, where is the largest value?
  3. How do you know bubble sort has finished?
  4. Write the list after the first pass of bubble sort on [3, 8, 1, 6].
  5. How many checks does linear search need to find 8 in [3, 9, 7, 1, 8]?
  6. What must be true about a list before binary search can be used?
  7. Binary search for 15 in [4, 8, 15, 16, 23, 42, 50]: which values are checked?
  8. Roughly how many checks does binary search need for 1,000 sorted items?
  9. Why is a printed dictionary sorted?
  10. You have an unsorted list and need to find one item just once. Is it worth sorting first?

Answer key

Answers: Sorting and searching

Do the practice sheet first, then check here. If an answer is different from yours, read the lesson again and try once more.

  1. When the left one is bigger than the right one.
  2. At the end (the last position).
  3. When a whole pass makes no swaps.
  4. [3, 1, 6, 8]: 3,8 no swap; 8,1 swap; 8,6 swap.
  5. 5 checks, because 8 is the last item.
  6. It must be sorted.
  7. 16 (middle), then 8 (middle of the left half), then 15: found in 3 checks.
  8. About 10, because each check halves the list and 2 to the power 10 is 1,024.
  9. So you can jump near the right place and search it quickly, like binary search, instead of reading from the start.
  10. No; sorting takes many steps, so a single linear search is cheaper. Sorting pays off when you will search many times.

Find out more

Want to learn more about computers and coding?

These are free, well-known places to explore. Ask a grown-up before you sign up for anything or type your name anywhere.

More places to learn, and free reading ideas

WhatsApp us