Computer Science · Theme B: Computational thinking and problem-solving
B2.4 — Programming algorithms
Computer Science · SL / HL · syllabus-mapped notes
CS_B2.4.1
Big O notation and efficiency
Time and space complexity, calculating Big O, and choosing by scalability.
Full note — freeBig O notation describes how an algorithm scales: how its running time or memory grows as the input size n grows. You must be able to describe efficiency by calculating it. Time and space complexity Time complexity : how the number of operations grows with n. Space complexity : how the extra memory used grows with n. Common orders (best to worst) O(1) constant, unaffected by n. O(log n) logarithmic, halves the problem each step (binary search). O(n) linear, one pass (linear search). O(n log n) good sorts (quicksort average). O(n^2) quadratic, a nested loop over the data (bubble sort, selection sort). Calculating it Count how operations depend on n and keep only the fastest-growing term, dropping constants: a loop over n is O(n); a loop inside a loop is O(n^2). Choosing by scalability e.g. for a small list an O(n^2) sort is fine, but for millions of items an O(n log n) algorithm scales far better.
CS_B2.4.2
Linear search and binary search
Constructing, tracing and comparing the two search algorithms.
You must be able to construct and trace a linear search and a binary search , and compare their efficiency. Linear search O(n), works on any list Check each element in turn until the target is found or the list ends. e.g. def linear(a, target): …
CS_B2.4.3
Bubble sort and selection sort
Constructing, tracing and evaluating the time and space complexities of each sort.
You must be able to construct and trace bubble sort and selection sort , and evaluate their time and space complexities. Bubble sort Repeatedly pass through the list, swapping any two adjacent elements that are out of order, until no swaps are needed. Large values …
CS_B2.4.4
The concept of recursion (HL)
Fundamentals, advantages and limitations, with applications including quicksort.
This card is HL only. You must be able to explain recursion : a technique where a function calls itself to solve a smaller version of the same problem. Fundamentals A recursive function has a base case (a simple case solved directly, which stops the …
CS_B2.4.5
Constructing recursive algorithms (HL)
Constructing and tracing simple, non-branching recursive algorithms.
This card is HL only. You must be able to construct and trace simple, non-branching recursive algorithms (a single recursive call each time). Constructing a recursive function Write the base case first, then the recursive case that moves toward it. e.g. factorial def factorial(n): if …
