Skip to topic
    ← Back to course topics

    Algorithms — Edexcel GCSE Computer Science

    Verified courseTeaching note checked against the official specification.

    Algorithms explained

    Algorithms covers 7 specification points, including Be able to follow and write algorithms… and Understand the need for and be able to follow….

    Read the full explanation

    Open a point below for its explanation and revision guidance.

    1.2.1 be able to follow and write algorithms (flowcharts, pseudocode*, program code) that use sequence, selection, repetition (count-controlled, condition-controlled) and iteration (over every item in a data structure), and input, processing and output to solve problems

    This criterion asks you to do two things with algorithms: follow one that already exists and write your own, in flowcharts, pseudocode or program code. Every algorithm you meet must combine the three control structures. Sequence means steps run in order, one after another. Selection means a condition chooses between paths, as in an IF statement. Repetition means a loop repeats a block: count-controlled loops run a set number of times, while condition-controlled loops run until a test changes. Iteration over every item in a data structure means visiting each element in turn, for example a FOR EACH loop over a list of scores. Input, processing and output must also be present: data is read in, transformed by calculation or comparison, then displayed or stored. To follow an algorithm, trace it line by line, recording variable values in a table.

    1.2.2 understand the need for and be able to follow and write algorithms that use variables and constants and one- and two-dimensional data structures (strings, records, arrays)

    This criterion has two parts: understanding why variables and constants are needed, and following and writing algorithms that use them alongside one- and two-dimensional data structures. A variable is a named store whose value can change while the algorithm runs, such as a running total. A constant is a named value fixed when it is declared and never changed, such as the maximum number of attempts or the value of pi; using a constant makes the algorithm easier to read and to update because the value appears once. Data structures hold related data. A string is a sequence of characters. A record groups fields of different types, for example a pupil record with name, age and score. A one-dimensional array is a list accessed by one index, such as scores[3]. A two-dimensional array is a table accessed by two indices, such as grid[row][column].

    1.2.3 understand the need for and be able to follow and write algorithms that use arithmetic operators (addition, subtraction, division, multiplication, modulus, integer division, exponentiation), relational operators (equal to, less than, greater than, not equal to, less than or equal to, greater than or equal to) and logical operators (AND, OR, NOT)

    Algorithms need operators to turn stored data into decisions and results. Arithmetic operators compute values: addition combines, subtraction finds a difference, multiplication scales, division gives a quotient, modulus returns the remainder, integer division discards the fractional part, and exponentiation raises a base to a power such as 2³ = 8. Relational operators compare two values and yield true or false: equal to, less than, greater than, not equal to, less than or equal to, greater than or equal to. Logical operators combine or invert Boolean results: AND is true only when both inputs are true, OR is true when at least one is true, and NOT reverses a single Boolean. For example, a program accepts a score, then tests score ≥ 50 AND score ≤ 100 to decide whether the mark is valid.

    1.2.4 be able to determine the correct output of an algorithm for a given set of data and use a trace table to determine what value a variable will hold at a given point in an algorithm

    To find an algorithm's output you execute it exactly as written with the supplied data. Work through each statement in order, updating every variable as its value changes, and respect sequence, selection and iteration. A trace table records this process: one column per variable, one row per step, so you can read off the value a variable holds at any chosen point. For example, given total ← 0 then a loop that adds 4 three times, the trace shows total becoming 4, then 8, then 12, so the output is 12. When a condition is tested, record the Boolean result and follow only the branch it selects. When a loop repeats, keep adding rows until the loop condition fails. The final row gives the output; an intermediate row gives the value at that point.

    1.2.5 understand types of errors that can occur in programs (syntax, logic, runtime) and be able to identify and correct logic errors in algorithms

    Programs can fail in three distinct ways. A syntax error breaks the rules of the language, so the translator cannot produce runnable code: for example, writing a loop header without its closing bracket, or misspelling a keyword. A logic error is grammatically valid code that produces the wrong result: for example, a total that adds a value before resetting the accumulator, or a condition that uses the wrong comparison operator. A runtime error occurs only while the program executes, such as dividing by zero, indexing an array outside its bounds, or converting non-numeric text to an integer. To identify a logic error in an algorithm, trace the variables step by step with a trace table, compare each intermediate value with the expected value, and locate the first step where they diverge.

    1.2.6 understand how standard algorithms (bubble sort, merge sort, linear search, binary search) work

    Four standard algorithms must be understood. Bubble sort repeatedly compares adjacent items in a list and swaps them when they are in the wrong order, so large values move to the end on each pass; passes repeat until a complete pass makes no swaps. Merge sort splits a list into single-item sublists, then repeatedly merges pairs of sorted sublists in order until one sorted list remains. Linear search checks each item from the start of a list in turn until the target is found or the list ends, so it works on unsorted data. Binary search requires a sorted list: it examines the middle item, then discards the half that cannot contain the target and repeats on the remaining half until the target is found or the search space is empty.

    1.2.7 be able to use logical reasoning and test data to evaluate an algorithm’s fitness for purpose and efficiency (number of compares, number of passes through a loop, use of memory)

    Fitness for purpose means an algorithm produces correct output for every valid input, including boundary and edge cases; efficiency means it does so with acceptable execution time and storage. Compare counts quantify execution: a linear search on n items takes up to n compares, whereas a binary search on sorted data needs only about 10 compares for 1 000 items. Pass counts quantify loop behaviour: a single loop runs n passes, while nested loops run n² passes. Memory use measures extra storage: an in-place sort uses minimal extra memory for temporary variables, whereas creating a copy requires additional memory proportional to list size. Test data (normal, boundary, erroneous) verifies correctness before evaluating resource efficiency.

    Your focus

    1. Trace a given algorithm accurately and state its output.
    2. Write an algorithm that combines sequence, selection, repetition and iteration to solve a stated problem.
    3. Represent an algorithm correctly as a flowchart, pseudocode or program code including input, processing and output.
    Show all 21 objectives
    1. Explain the difference between a variable and a constant and why each is needed.
    2. Follow an algorithm that uses variables, constants and data structures, and state its output.
    3. Write an algorithm that correctly uses strings, records, one-dimensional arrays and two-dimensional arrays.
    4. Identify the arithmetic, relational and logical operators and state what each one does.
    5. Evaluate expressions that combine arithmetic, relational and logical operators to give a value or a Boolean result.
    6. Write a short algorithm that uses an appropriate operator to solve a stated problem.
    7. Trace an algorithm with given data, recording each variable's value at every step.
    8. State the output an algorithm produces for a specified set of data.
    9. Identify the value a named variable holds at a stated point in an algorithm.
    10. Classify a given fault as a syntax, logic or runtime error and justify the classification.
    11. Trace an algorithm step by step to locate the first point where a logic error causes divergence from expected values.
    12. Correct a logic error by amending the faulty step or condition and verify the correction by re-tracing.
    13. Describe the steps of bubble sort, merge sort, linear search and binary search.
    14. Trace each algorithm on a given list and state the list state or items examined at each stage.
    15. Select an appropriate algorithm for a given list and target, justifying the choice by whether the data is sorted.
    16. Evaluate whether an algorithm produces correct output for normal, boundary and erroneous test data.
    17. Count and compare the number of compares and the number of passes through a loop for a given algorithm.
    18. Assess the memory used by an algorithm and reach a justified overall judgement of fitness and efficiency.

    Algorithms exam tips

    Marking Points
    • Traces a given algorithm accurately, recording the value of each variable after every step and stating the final output.
    • Writes an algorithm that uses sequence, so steps are ordered correctly and each depends on the previous one.
    • Uses selection correctly, with a condition that chooses between two or more paths, such as IF score >= 50 THEN grade = "pass".
    • Uses count-controlled repetition correctly, for example FOR i = 1 TO 10, and states how many times the body runs.
    • Uses condition-controlled repetition correctly, for example WHILE answer != "yes", and identifies the condition that ends the loop.
    • Uses iteration over every item in a data structure, for example FOR EACH item IN scores, so each element is processed once.
    • Includes input, processing and output, showing where data enters, how it is changed and what is produced.
    • Presents the algorithm in the required form, using correct flowchart symbols or pseudocode/program code syntax.
    • Explains that a variable is a named store whose value can change during execution, and a constant is fixed once declared and cannot be altered.
    • Explains the need for constants, for example avoiding repeated literals and making an algorithm easier to maintain and read.
    • Declares and uses variables correctly, assigning values and updating them, such as total = total + score.
    • Uses a one-dimensional array correctly, accessing elements by a single index and processing each element in turn.
    • Uses a two-dimensional array correctly, accessing elements by two indices such as grid[row][column] and processing rows and columns.
    • Uses a string correctly, for example finding its length or accessing a character by position.
    • Uses a record correctly, grouping named fields of different data types and accessing a field such as pupil.name.
    • Traces an algorithm that uses these structures, tracking index values and stored data accurately to state the output.
    • Addition combines two values, for example total ← total + price.
    • Subtraction finds a difference, for example change ← paid − cost.
    • Multiplication scales a value, for example area ← length × width.
    • Division produces a quotient that may include a fractional part, for example average ← total ÷ count.
    • Modulus returns the remainder after division, for example 17 MOD 5 gives 2.
    • Integer division discards the fractional part, for example 17 DIV 5 gives 3.
    • Exponentiation raises a base to a power, for example 2³ = 8.
    • Relational operators compare values and produce true or false: equal to, less than, greater than, not equal to, less than or equal to, greater than or equal to.
    • AND is true only when both operands are true; OR is true when at least one operand is true; NOT reverses a Boolean value.
    • A correct answer selects the operator whose meaning matches the stated task, such as using modulus to test divisibility.
    • Execute statements in the order written, updating each variable as its value changes.
    • Record one column per variable and one row per step in the trace table.
    • Evaluate conditions correctly and follow only the branch selected by the Boolean result.
    • Repeat loop rows until the loop condition becomes false, then stop.
    • Read the final row to determine the algorithm's output.
    • Read the row at the specified point to determine the value a variable holds there.
    • Substitute the given data accurately so arithmetic and comparisons use the supplied values.
    • Syntax error: code violates the grammar of the language, so it cannot be translated or run; the translator reports it before execution.
    • Logic error: the code is syntactically valid and runs, but produces an incorrect result because the algorithm's steps or conditions are wrong.
    • Runtime error: the fault occurs during execution, for example division by zero, an out-of-range array index, or invalid type conversion.
    • Identification method: trace the algorithm with a trace table, record each variable after every step, and find the first step where a value differs from the expected value.
    • Correction method: amend the specific step or condition that causes the divergence, then re-trace the whole algorithm to confirm the output is now correct.
    • Distinguishing the types: a syntax error prevents the program running at all, a runtime error stops or crashes it mid-execution, and a logic error lets it finish with wrong output.
    • Bubble sort compares adjacent pairs and swaps them when out of order, repeating passes until a pass produces no swaps.
    • Merge sort divides the list into single items, then merges sorted sublists pairwise in order until a single sorted list remains.
    • Linear search examines items one at a time from the start until the target is found or the list is exhausted, and does not require sorted data.
    • Binary search requires a sorted list, tests the middle item, then discards the half that cannot contain the target and repeats on the remaining half.
    • Efficiency comparison: binary search is generally faster than linear search on large sorted lists, while linear search is the only option for unsorted data.
    • Correct application: for a given list and target, state the items examined in order and the final result for each algorithm.
    • Defines fitness for purpose as producing correct output for all valid inputs, including boundary and erroneous test data, and justifies this with a traced example.
    • Counts the number of compares for a named algorithm, for example stating that a linear search of n items performs up to n compares and a binary search performs about log₂ n compares.
    • Counts passes through a loop by tracing the loop variable, for example a loop from 1 to n runs n passes and a nested loop over n items runs n² passes.
    • Evaluates use of memory by distinguishing extra storage, such as minimal extra space for an in-place sort against creating a second list of items in memory.
    • Uses normal, boundary and erroneous test data to demonstrate correctness and to expose failures in the algorithm.
    • Reaches a reasoned judgement that weighs correctness against compare count, pass count and memory use rather than treating one measure alone as decisive.
    Examiner Tips
    • 💡Plan the algorithm before writing: list the inputs, the processing steps and the outputs, then choose sequence, selection or repetition for each part.
    • 💡Trace your finished algorithm with a small sample input, writing variable values in a table, to prove it produces the correct output.
    • 💡Label loops and conditions clearly so an examiner can see which structure you intended, and keep indentation consistent in pseudocode or code.
    • 💡Name variables and constants meaningfully so their purpose is clear, and declare constants with a fixed value before use.
    • 💡When tracing, draw a table with one column per variable or array element and fill it in step by step.
    • 💡For two-dimensional data, sketch the rows and columns and mark the current cell so index errors are easy to spot.
    • 💡Underline the key word in the question, such as remainder, quotient or power, before choosing an operator.
    • 💡Work out each expression step by step, applying brackets and operator precedence before comparing values.
    • 💡Check Boolean expressions by substituting true and false for each operand and evaluating the logical operators last.
    • 💡Draw the trace table before starting and label every column with a variable name.
    • 💡Write the Boolean result of each condition in the row so the chosen branch is clear.
    • 💡After the last row, state the output explicitly and check it against the algorithm's purpose.
    • 💡Read the code or algorithm carefully and ask first whether it could be translated at all; if not, it is a syntax error.
    • 💡For logic-error questions, trace the variables in a table and state the first step where the actual value differs from the expected value.
    • 💡When asked to correct an algorithm, write only the amended step or condition and briefly justify why that change removes the error.
    • 💡For a given list, write out the state of the list after each pass or merge so the examiner can follow your working.
    • 💡State clearly whether a list must be sorted before an algorithm can be used, and name the algorithm that works on unsorted data.
    • 💡When comparing search algorithms, refer to the number of items examined rather than vague claims about speed.
    • 💡Structure the answer as correctness first, then compare count, then pass count, then memory, and finish with an overall fitness judgement.
    • 💡Support every efficiency claim with a concrete count or calculation, such as 1 000 compares for a worst-case linear search of 1 000 items.
    • 💡Use the test data given in the question explicitly, naming which values are normal, boundary or erroneous and what each reveals.
    Common Mistakes
    • Confusing count-controlled and condition-controlled repetition: a FOR loop runs a known number of times, while a WHILE loop runs while a condition remains true. Choose the loop that matches whether the number of repeats is known in advance.
    • Writing a loop that never ends because the condition never becomes false. Check that something inside the loop changes the variable used in the condition.
    • Treating iteration over a data structure as the same as a count-controlled loop over indices. FOR EACH visits every item directly and is the clearer choice when each element must be processed.
    • Omitting output or input so the algorithm cannot solve the problem. State clearly where data is read and where the result is displayed.
    • Trying to change a constant after it is declared. A constant is set once and stays fixed; use a variable if the value must change.
    • Confusing a record with an array: a record groups named fields that may have different types, while an array holds elements of the same type accessed by index.
    • Using one index for a two-dimensional array. A table needs two indices, one for the row and one for the column.
    • Assuming array indices always start at 1. Check the convention used in the question and trace accordingly.
    • Confusing modulus with division: modulus returns the remainder, so 17 MOD 5 is 2, while 17 DIV 5 is 3.
    • Treating integer division as ordinary division: integer division discards the fractional part, so 17 DIV 5 is 3, not 3.4.
    • Assuming OR is true only when exactly one operand is true: OR is true when at least one operand is true, so true OR true is true.
    • Reading NOT as a comparison rather than an inversion: NOT reverses a single Boolean value, so NOT true is false.
    • Skipping loop iterations: every repetition needs its own trace row, so a loop that runs three times produces three rows.
    • Updating a variable before evaluating the condition that uses its old value: evaluate the condition with the current value, then update.
    • Ignoring the branch not taken: only the statements inside the selected branch are executed, so the other branch must not change any variable.
    • Reading the wrong row: the output comes from the final row, while an intermediate value comes from the row at the stated point.
    • Treating any wrong output as a syntax error. Correction: a syntax error stops translation before the program runs, whereas wrong output from a running program indicates a logic error.
    • Assuming a program that runs without crashing is correct. Correction: it may still contain a logic error, so the output must be checked against expected results.
    • Changing many lines at once when fixing a logic error. Correction: change the single step where the trace first diverges, then re-trace to verify, so the cause of the fix is clear.
    • Confusing a runtime error with a logic error. Correction: a runtime error halts execution (for example division by zero), while a logic error allows execution to complete with an incorrect result.
    • Using binary search on an unsorted list. Correction: binary search depends on sorted order, so the list must be sorted first or linear search used instead.
    • Stopping bubble sort after one pass. Correction: passes continue until a complete pass makes no swaps, because one pass may not fully sort the list.
    • Merging sublists in merge sort without comparing their items. Correction: merging must place items in order, comparing the front items of the sublists at each step.
    • Skipping items in a linear search. Correction: linear search checks each item in turn from the start until the target is found or the list ends.
    • Confusing fitness for purpose with efficiency: a correct algorithm can still be inefficient. Correction: test correctness first with test data, then measure compares, passes and memory separately.
    • Assuming a binary search always beats a linear search: it requires sorted data, and sorting costs time and possibly memory. Correction: state the sorting precondition and include its cost in the evaluation.
    • Counting loop passes incorrectly, for example saying a loop from 1 to n runs n − 1 passes. Correction: trace the loop variable and count each execution of the body, so 1 to n inclusive gives n passes.
    • Treating memory use as only the input size: extra working storage such as a duplicate list or recursion stack also counts. Correction: identify the additional storage the algorithm allocates beyond its input.