Array Pseudocode (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Searching arrays

  • A linear search starts with the first value in a dataset and checks every value one at a time until all values have been checked

  • A linear search can be performed even if the values are not in order

Step

Instruction

1

Check the first value

2

IF it is the value you are looking for

  • STOP!

3

ELSE move to the next value and check

4

REPEAT UNTIL you have checked all values and not found the value you are looking for

  • A linear search can be used to find elements in an array

  • Each element in the array is checked in order, from the lower bound to the upper bound, until the item is found or the upper bound is reached

  • An example pseudocode algorithm to perform a linear search on a 1D array could be:

DECLARE List : ARRAY[0:4] OF INTEGER
DECLARE Target : INTEGER
DECLARE Found : BOOLEAN
DECLARE Index : INTEGER

// Initialise array values
List[0] ← 12
List[1] ← 25
List[2] ← 37
List[3] ← 42
List[4] ← 56

// Input the value to search for
OUTPUT "Enter the value to search for:"
INPUT Target

Found ← FALSE
Index ← 0

WHILE Index <= 4 AND Found = FALSE DO
    IF List[Index] = Target THEN
        Found ← TRUE
    ELSE
        Index ← Index + 1
    ENDIF
ENDWHILE

IF Found = TRUE THEN
    OUTPUT "Item found at index ", Index
ELSE
    OUTPUT "Item not found"
ENDIF
  • Works on a fixed-size array List[0:4]

  • Uses a WHILE loop for flexibility (early exit if found)

  • Clearly separates input, processing, and output

Identifier table

Identifier

Data type

Description

List

ARRAY[0:4] OF INTEGER

Stores the list of numbers to search through

Target

INTEGER

The number the user wants to search for

Found

BOOLEAN

Tracks whether the target value has been found

Index

INTEGER

The current position being checked in the array

Sorting arrays

What is a bubble sort?

  • A bubble sort is a simple sorting algorithm that starts at the beginning of a dataset and checks values in 'pairs' and swaps them if they are not in the correct order

  • One full run of comparisons from beginning to end is called a 'pass', a bubble sort may require multiple 'passes' to sort the dataset

  • The algorithm is finished when there are no more swaps to make

How do you perform a bubble sort?

Step

Instruction

1

Compare the first two values in the dataset

2

IF they are in the wrong order...

  • Swap them

3

Compare the next two values

4

REPEAT step 2 & 3 until you reach the end of the dataset (pass 1)

5

IF you have made any swaps...

  • REPEAT from the start (pass 2,3,4...)

6

ELSE you have not made any swaps...

  • STOP! the list is in the correct order

Example

  • Perform a bubble sort on the following dataset

5

2

4

1

6

3

Step

Instruction

1

Compare the first two values in the dataset

5

2

4

1

6

3

2

IF they are in the wrong order...

  • Swap them

2

5

4

1

6

3

3

Compare the next two values

2

5

4

1

6

3

4

REPEAT step 2 & 3 until you reach the end of the dataset

  • 5 & 4 SWAP!

2

4

5

1

6

3

  • 5 & 1 SWAP!

2

4

1

5

6

3

  • 5 & 6 NO SWAP!

2

4

1

5

6

3

  • 6 & 3 SWAP!

2

4

1

5

3

6

  • End of pass 1

5

IF you have made any swaps...

  • REPEAT from the start

  • End of pass 2 (swaps made)

2

1

4

3

5

6

  • End of pass 3 (swaps made)

1

2

3

4

5

6

  • End of pass 4 (no swaps)

1

2

3

4

5

6

6

ELSE you have not made any swaps...

  • STOP! the list is in the correct order

  • A bubble sort can be used to put elements in an array in order

  • A pseudocode algorithm for a bubble sort on a 1D array could be:

DECLARE Numbers : ARRAY[0:5] OF INTEGER
DECLARE Temp : INTEGER
DECLARE Pass : INTEGER
DECLARE Index : INTEGER
DECLARE Swapped : BOOLEAN

// Initialise the array
Numbers[0] ← 5
Numbers[1] ← 2
Numbers[2] ← 4
Numbers[3] ← 1
Numbers[4] ← 6
Numbers[5] ← 3

// Bubble sort algorithm
FOR Pass ← 1 TO 5
    Swapped ← FALSE
    FOR Index ← 0 TO 4
        IF Numbers[Index] > Numbers[Index + 1] THEN
            Temp ← Numbers[Index]
            Numbers[Index] ← Numbers[Index + 1]
            Numbers[Index + 1] ← Temp
            Swapped ← TRUE
        ENDIF
    NEXT Index
    IF Swapped = FALSE THEN
        // List is already sorted
        EXIT
    ENDIF
NEXT Pass

// Output the sorted array
FOR Index ← 0 TO 5
    OUTPUT Numbers[Index]
NEXT Index
  • Uses a fixed array of size 6

  • Includes an optimisation to exit early if no swaps were made on a pass

Identifier table

Identifier

Data type

Description

Numbers

ARRAY[0:5] OF INTEGER

Stores the list of numbers to be sorted

Temp

INTEGER

Temporary variable used for swapping two elements in the array

Pass

INTEGER

Tracks the number of passes through the array

Index

INTEGER

Tracks the current position being compared in the array

Swapped

BOOLEAN

Indicates whether a swap occurred during a pass (used to optimise sorting)

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.