Data Structures · Searching · EXERCISE 1 (a)

Exercise 1 (a): Linear Search

🔎 The array DataSet[8]

being compared already compared (not equal) found i = current index · id = the value we look for
looking for id = compared 0 time(s) array length n =
return(not finished yet)

🧾 Comparison log

💬 Current step

💻 The algorithm (the courseware pseudocode)

Yellow = the line being executed. Linear search starts at element 0 and compares one by one; it returns the index as soon as it matches, and -1 if every element was compared without a match.

📄 The question’s wording & key points

Exercise 1 (a): Perform the following search operations and show each step clearly.
Use Linear Search to find the number 27 in the array:
[10, 5, 18, 27, 32, 40, 3, 9]
1Start at i = 0 — linear search can only walk from the beginning
2Compare DataSet[i] == id — equal means return i (found)
3Nothing matched → return -1 (not found)
Why does it work on an unsorted array? Linear search never looks at the order, it just checks element after element — so an unsorted array is fine (binary search is not, it needs sorted data).
Cost: 1 comparison in the best case, n comparisons in the worst and average case (n = 8) → time O(n), extra space O(1).
The courseware example: looking for 27 → compare 10, 5, 18, 27 in turn; the 4th one is equal → return 3.
Speed
Shortcuts: Space play/pause · ←→ step · R reset · E result