Show understanding of the following algorithms, including their operation, step‑wise refinement, structured English, flow‑charts, efficiency (time & space), stability, best/average/worst‑case behaviour and typical use‑cases:
| Identifier | Type | Purpose |
|---|---|---|
| A[] | array of comparable items | data set to be searched |
| n | integer | number of elements in A |
| key | same type as A[] | value being looked for |
| i | integer | loop index |
| found | boolean | set to true when the key is located |
SET found ← FALSE
FOR i FROM 0 TO n‑1 DO
IF A[i] = key THEN
SET found ← TRUE
EXIT FOR
END‑IF
END‑FOR

found ← false
FOR i ← 0 TO n‑1
IF A[i] = key THEN
found ← true
BREAK
END IF
END FOR
found to false.key.found to true and terminate the loop.found indicates whether the key exists.Small or unsorted data sets where a simple linear scan is acceptable. It is also the first algorithm introduced to develop algorithmic thinking.
| Identifier | Type | Purpose |
|---|---|---|
| A[] | sorted array | data set to be searched |
| n | integer | size of A |
| key | same type as A[] | value being looked for |
| low, high | integer | current interval limits |
| mid | integer | middle index of the interval |
| found | boolean | result flag |
SET low ← 0, high ← n‑1, found ← FALSE
WHILE low ≤ high AND NOT found DO
SET mid ← (low + high) DIV 2
IF A[mid] = key THEN
SET found ← TRUE
ELSE IF A[mid] < key THEN
SET low ← mid + 1
ELSE
SET high ← mid – 1
END‑IF
END‑WHILE

found ← false
low ← 0
high ← n‑1
WHILE low ≤ high AND NOT found
mid ← (low + high) DIV 2
IF A[mid] = key THEN
found ← true
ELSE IF A[mid] < key THEN
low ← mid + 1
ELSE
high ← mid - 1
END IF
END WHILE
[low,high] = [0,n‑1].mid.A[mid] with key.found to true; otherwise discard the half that cannot contain the key.found tells whether the key was present.When the data is already sorted and fast look‑ups are required. It exemplifies divide‑and‑conquer and logarithmic time.
| Identifier | Type | Purpose |
|---|---|---|
| A[] | array of comparable items | data to be sorted |
| n | integer | size of A |
| i, j | integer | loop indices |
| key | same type as A[] | element currently being inserted |
FOR i FROM 1 TO n‑1 DO
SET key ← A[i]
SET j ← i‑1
WHILE j ≥ 0 AND A[j] > key DO
SET A[j+1] ← A[j] /* shift larger element right */
SET j ← j‑1
END‑WHILE
SET A[j+1] ← key /* insert key */
END‑FOR

FOR i ← 1 TO n‑1
key ← A[i]
j ← i‑1
WHILE j ≥ 0 AND A[j] > key
A[j+1] ← A[j]
j ← j‑1
END WHILE
A[j+1] ← key
END FOR
A[0]).i.key one position to the right.key into the vacated slot (j+1).i = 1 … n‑1.FUNCTION InsertionSortRecursive(A, n)
IF n ≤ 1 THEN RETURN
CALL InsertionSortRecursive(A, n‑1) // sort first n‑1 items
key ← A[n‑1]
j ← n‑2
WHILE j ≥ 0 AND A[j] > key
A[j+1] ← A[j]
j ← j‑1
END WHILE
A[j+1] ← key
END FUNCTION
The recursive calls use O(log n) stack space; the algorithm remains in‑place and stable.
| Identifier | Type | Purpose |
|---|---|---|
| A[] | array of comparable items | data to be sorted |
| n | integer | size of A |
| i | integer | index for the inner loop |
| swapped | boolean | detects whether a pass made any exchanges |
SET swapped ← TRUE
WHILE swapped DO
SET swapped ← FALSE
FOR i FROM 0 TO n‑2 DO
IF A[i] > A[i+1] THEN
SWAP A[i] WITH A[i+1]
SET swapped ← TRUE
END‑IF
END‑FOR
END‑WHILE

do
swapped ← false
for i ← 0 TO n‑2
if A[i] > A[i+1] then
swap A[i] and A[i+1]
swapped ← true
end if
end for
while swapped
swapped to true to guarantee at least one pass.while loop:swapped to false.for loop). For each adjacent pair, swap if they are out of order and set swapped to true.swapped stays false and the algorithm terminates.FUNCTION BubbleSortRecursive(A, n)
IF n = 1 THEN RETURN
FOR i ← 0 TO n‑2
IF A[i] > A[i+1] THEN
SWAP A[i] WITH A[i+1]
END IF
END FOR
CALL BubbleSortRecursive(A, n‑1) // largest element now at position n‑1
END FUNCTION
Each recursive call reduces the unsorted portion by one, giving a recursion depth of n and O(n) stack space.
| Algorithm | Category | Best‑case | Average‑case | Worst‑case | Space | Stability | Typical use‑case |
|---|---|---|---|---|---|---|---|
| Linear search | Search | Θ(1) | Θ(n) | Θ(n) | O(1) | N/A | Unsorted or very small data sets; introductory teaching. |
| Binary search | Search | Θ(1) | Θ(log n) | Θ(log n) | O(1) (iterative) / O(log n) (recursive) | N/A | Fast look‑ups in a sorted collection. |
| Insertion sort | Sort | Θ(n) | Θ(n²) | Θ(n²) | O(1) | Stable | Very small or nearly sorted lists; final phase of hybrid sorts. |
| Bubble sort | Sort | Θ(n) | Θ(n²) | Θ(n²) | O(1) | Stable | Teaching tool; tiny or almost‑sorted data sets where simplicity matters. |
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.