Skip to content
IGCSE·Tuition
Computer Science · Topics

Searching, sorting and files

You can follow an algorithm when the teacher walks through it, then freeze when a blank trace table appears on the page.

On this page
  1. What should I already know?
  2. Orienting example: one trace, three ideas
  3. What order should I study the lessons in?
  4. Which traps catch students most often?
  5. How do I use the practice set?

This module is about three jobs a program does with stored data: finding a value, putting values in order, and reading and writing files. Exam questions rarely ask you to invent a clever method. They ask you to follow a given algorithm exactly, say what it outputs, and spot where it breaks.

For the exact scope and the notation expected in your exam year, read the Cambridge IGCSE Computer Science syllabus page. The wider picture is in the Computer Science learning guide, and the 2029 changes page explains how later exam years may differ.

What should I already know?

You need variables, selection and loops. The module on repetition and arrays covers the loop and array skills used here, and validation, verification and testing introduces the test-data thinking that the last lesson builds on.

Orienting example: one trace, three ideas

Here is a tiny search over a fictional list. The array Items holds "pen", "ruler", "glue" in positions 1 to 3, and the target is "glue".

Found ← FALSE
Index ← 1
WHILE Index <= 3 AND Found = FALSE
   IF Items[Index] = Target THEN
      Found ← TRUE
   ELSE
      Index ← Index + 1
   ENDIF
ENDWHILE
CheckIndexItems[Index]Found
Before loop1FALSE
1st pass1penFALSE, so Index becomes 2
2nd pass2rulerFALSE, so Index becomes 3
3rd pass3glueTRUE

The loop stops with Index = 3, so the answer is position 3 after three comparisons. Notice what the table shows: the data, a variable that moves, and a stop condition. Every lesson in this module uses those three parts.

What order should I study the lessons in?

  1. Trace a linear search on original data: the clearest way to learn trace tables, including what happens when the target is missing.
  2. Explain a sorting pass where in scope: follow one pass of neighbour comparisons and swaps.
  3. Read a record without losing field boundaries: split a line of stored data into its fields correctly.
  4. Handle end-of-file in a restricted example: read a whole file with a loop that stops at the right moment.
  5. Check an algorithm against empty and duplicate data: test your own algorithm on the awkward cases.

Then attempt the mixed practice set. Two tools help: the restricted pseudocode trace trainer for stepping variables, and the safe Python reasoning sandbox for checking a short Python version.

Which traps catch students most often?

  • Changing the index after the match. A search that keeps counting after it finds the target reports the wrong position.
  • Losing a value in a swap. Without a temporary variable, one value overwrites the other.
  • Treating a number in a file as text. The characters 9 and 72 compare differently as text than as numbers.
  • Testing only the friendly case. An empty list or repeated values can break an algorithm that works on tidy data.

How do I use the practice set?

Draw the trace table on paper before you open any answer. Write each variable in its own column and update it one line at a time. Record every slip in the mistake log and retest queue and retry that question a few days later.

If you follow each step in class but your own traces drift off course, that is something our teachers can work on in online one-to-one Computer Science tuition.

Questions people ask

Do I have to memorise a full sorting algorithm?

Check the current syllabus for your exam year to see exactly what is required. Whatever the wording, the skill that carries over is tracing: following the data one comparison at a time. This module trains that skill on a small original example so you can apply it to any similar algorithm.

Why do file questions use a loop with end-of-file?

A program often cannot know how many lines a file holds. The end-of-file test lets the loop keep reading until nothing is left. Questions check whether you read inside the loop, test at the right moment and close the file afterwards.

Should I write answers in pseudocode or Python?

Use the notation your exam year expects, and check it on the Cambridge page. This site shows Cambridge-style pseudocode and adds Python only where it helps. The logic of a trace is identical in both, so practise the logic first.

Sources

  1. Cambridge IGCSE Computer Science 0478 syllabus page

Updated:

Your next step

If your traces go wrong at the same step each time, a one-to-one teacher can watch you work a trace line by line and show you exactly where the variable stopped matching the code.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service