Given a list 'numList' of 'n' elements and the key value 'K'. Arrange the following steps in linear search algorithm 'LinearSearch(numList, key, n)' to find the position of the key 'K' in 'numList':
(A) PRINT “Search unsuccessful”
(B) WHILE index < n, REPEAT Step 3
(C) SET index = 0
(D) IF numlist[index]= key THEN
PRINT “Element found at position”, index+1
STOP
ELSE
index = index+1
Given a list 'numList' of 'n' elements and the key value 'K'. Arrange the following steps in linear search algorithm 'LinearSearch(numList, key, n)' to find the position of the key 'K' in 'numList':
(A) PRINT “Search unsuccessful”
(B) WHILE index < n, REPEAT Step 3
(C) SET index = 0
(D) IF numlist[index]= key THEN
PRINT “Element found at position”, index+1
STOP
ELSE
index = index+1
Solution
The index must be initialised first, then the loop runs the comparison on each element, and "Search unsuccessful" is printed only after the loop ends without a match.
(C) SET index = 0: start at the first element.
(B) WHILE index < n, REPEAT Step 3: loop through the list while elements remain.
(D) IF numlist[index] = key THEN print the position (index+1) and STOP, ELSE index = index+1: compare the current element, and either stop or move on.
(A) PRINT "Search unsuccessful": reached only when the loop finishes without finding the key.
Option 2: the loop (B) cannot run before index is set in (C).
Option 1: it prints "Search unsuccessful" first, before any searching has happened.
Related questions:
2025: 15 May Shift 2
2025: 14 May Shift 1
2026: 29 May Shift 2
2026: 23 May Shift 2
2026: 20 May Shift 1