IB Diploma Computer Science · Computational thinking and problem-solving
Computational thinking
1The four parts of computational thinking
Computational thinking is a way of solving problems so that a computer (or a person) can carry out the solution. It has four main parts:
- Decomposition: breaking a big problem into smaller parts that are easier to solve. Building a school timetable app breaks into storing students, storing rooms, checking clashes and displaying timetables.
- Pattern recognition: spotting similarities, so a solution to one part can be reused. Calculating the area of every room uses the same steps each time.
- Abstraction: removing unnecessary detail and keeping only what matters. A metro map shows the order of stations and the lines, not the real distances or streets.
- Algorithmic thinking: designing a clear, step-by-step method (an algorithm) that always solves the problem.
Other useful ideas:
- Thinking ahead: identifying the inputs, outputs and preconditions (what must be true before the algorithm runs, such as "the list is sorted") before starting.
- Thinking procedurally: ordering the sub-problems and deciding which modules (procedures, functions) solve each.
- Thinking logically: identifying the decisions and conditions that change the path through the algorithm.
- Thinking concurrently: spotting parts that can happen at the same time, such as downloading several files at once.
Check yourself
A map of a metro system leaves out the streets and real distances. Which part of computational thinking is this?
Show the answer
Abstraction
Only the detail needed to plan a journey is kept.
2Representing algorithms
An algorithm must be precise (no ambiguity), finite (it ends), and give the correct output for every valid input. It can be written as:
- Flowcharts: ovals for start and end, parallelograms for input and output, rectangles for processes, diamonds for decisions, with arrows showing the flow.
- Pseudocode: structured English that reads like code but ignores the exact rules of a programming language.
The three building blocks of every algorithm:
- Sequence: steps carried out in order.
- Selection: choosing a path with a condition (if ... else).
- Iteration: repeating steps, either a fixed number of times (a count-controlled loop,
for) or until a condition changes (a condition-controlled loop,while).
Flowchart: finding the largest number in a list (sequence, selection and iteration)
An algorithm that finds the largest number in a list, in Python-style pseudocode:
largest = numbers[0]
for value in numbers:
if value > largest:
largest = value
print(largest)
Check yourself
Which building block repeats a set of steps?
Show the answer
Iteration
Loops such as for and while.
3Tracing algorithms
A trace table follows an algorithm step by step, recording the value of each variable as it changes. It is used to check that an algorithm works and to find logic errors.
Trace the largest-value algorithm with numbers = [4, 9, 2, 11]
- Start: largest = 4.
- value = 4: 4 > 4 is false, largest stays 4.
- value = 9: 9 > 4 is true, largest = 9.
- value = 2: 2 > 9 is false, largest stays 9.
- value = 11: 11 > 9 is true, largest = 11. The output is 11.
Testing should use different kinds of data:
- Normal data: typical values that should be accepted (age 15).
- Boundary (extreme) data: values at the edges of the allowed range (ages 11 and 19 if 11–19 is allowed), and just outside them.
- Erroneous (abnormal) data: values that should be rejected (age −3, or "fifteen").
Errors come in three kinds: syntax errors break the language's rules (a missing bracket), runtime errors happen while running (dividing by zero), and logic errors give wrong answers without crashing (using < instead of <=).
Check yourself
A program allows marks from 0 to 100. Which is boundary test data?
Show the answer
100
Boundaries are the edges of the valid range.
4Searching algorithms
Linear search checks each item in turn until it finds the target or reaches the end.
- Works on unsorted lists. Simple, but slow for large lists: up to $n$ comparisons for $n$ items.
Binary search works only on a sorted list. It checks the middle item; if that is not the target, it discards the half that cannot contain it, and repeats.
Binary search for 31 in [3, 8, 12, 17, 23, 31, 40, 52, 66]
- low = 0, high = 8, middle = 4: item 23. 31 > 23, so search the right half: low = 5.
- low = 5, high = 8, middle = 6: item 40. 31 < 40, so search the left half: high = 5.
- low = 5, high = 5, middle = 5: item 31. Found, after only 3 comparisons.
Each step halves the list, so binary search needs at most about $\log_2 n$ comparisons. For a million items, that is 20 comparisons instead of up to a million.
Binary search for 31: the middle item is checked and half the list is discarded each step
Check yourself
What must be true of a list before binary search can be used?
Show the answer
It must be sorted
Binary search relies on order to discard half each time.
5Sorting algorithms
Bubble sort repeatedly steps through the list, comparing neighbouring pairs and swapping them if they are in the wrong order. After each pass, the largest remaining item has "bubbled" to the end.
Bubble sort [5, 1, 4, 2, 8]
- Pass 1: swap 5 and 1, swap 5 and 4, swap 5 and 2, leave 5 and 8. Result [1, 4, 2, 5, 8].
- Pass 2: swap 4 and 2. Result [1, 2, 4, 5, 8].
- Pass 3: no swaps, so the list is sorted and the algorithm can stop early.
Selection sort finds the smallest item in the unsorted part and swaps it into the next position, repeating until done.
Insertion sort takes each item in turn and inserts it into its correct place among the items already sorted: [7, 3, 9, 1] becomes [3, 7, 9, 1], then [3, 7, 9, 1], then [1, 3, 7, 9]. It is fast for lists that are nearly sorted.
Bubble, selection and insertion sort all take about $n^2$ steps for $n$ items in the worst case, so they are slow for large data. Faster algorithms such as merge sort and quicksort take about $n \log n$ steps.
Check yourself
After the first pass of bubble sort, which item is guaranteed to be in its final place?
Show the answer
The largest item, at the end
The largest bubbles to the end.
6Efficiency and Big O notation
Big O notation describes how an algorithm's running time (or memory use) grows as the input size $n$ grows. It describes the worst case and ignores constants.
- $O(1)$, constant: the time does not depend on $n$, such as reading one item of an array by its index.
- $O(\log n)$, logarithmic: binary search.
- $O(n)$, linear: linear search, or finding the largest value.
- $O(n^2)$, quadratic: bubble, selection and insertion sort (a loop inside a loop).
- $O(2^n)$, exponential: trying every combination, such as every subset of a set. Impractical for large $n$.
How running time grows with input size for common complexities
Choosing an algorithm is a trade-off: binary search is much faster than linear search, but needs a sorted list, and sorting costs time too. For a small list searched only once, linear search may be best.
Check yourself
Which complexity describes binary search?
Show the answer
O(log n)
The list halves at each step.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera