Array Pseudocode (Cambridge International A-Level Computer Science)
Searching arrays
What is a linear search?
- 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
How do you perform a linear search?
| 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:
-
Works on a fixed-size array
List[0:4] -
Uses a
WHILEloop 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:
-
Uses a flag,
Swapped, in aREPEAT ... UNTILloop so the sort stops after the first pass with no swaps -
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) |
Was this revision note helpful?
Build on this topic