Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
array-pseudocode
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
|
|
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
WHILEloop for flexibility (early exit if found) -
Clearly separates input, processing, and output
Identifier table
|
Identifier |
Data type |
Description |
|---|---|---|
|
|
ARRAY[0:4] OF INTEGER |
Stores the list of numbers to search through |
|
|
INTEGER |
The number the user wants to search for |
|
|
BOOLEAN |
Tracks whether the target value has been found |
|
|
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…
|
|
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…
|
|
6 |
ELSE you have not made any swaps…
|
Example
-
Perform a bubble sort on the following dataset
|
|
|
|
|
|
|
|
Step |
Instruction |
||||||||||||||||||||||||
|
1 |
Compare the first two values in the dataset
|
||||||||||||||||||||||||
|
2 |
IF they are in the wrong order…
|
||||||||||||||||||||||||
|
3 |
Compare the next two values
|
||||||||||||||||||||||||
|
4 |
REPEAT step 2 & 3 until you reach the end of the dataset
|
||||||||||||||||||||||||
|
5 |
IF you have made any swaps…
|
||||||||||||||||||||||||
|
6 |
ELSE you have not made any swaps…
|
-
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 |
|---|---|---|
|
|
ARRAY[0:5] OF INTEGER |
Stores the list of numbers to be sorted |
|
|
INTEGER |
Temporary variable used for swapping two elements in the array |
|
|
INTEGER |
Tracks the number of passes through the array |
|
|
INTEGER |
Tracks the current position being compared in the array |
|
|
BOOLEAN |
Indicates whether a swap occurred during a pass (used to optimise sorting) |
Responses