Question 1 of 8intermediate🔧 ApplyNumerical4 marks

Using linear search determine the position of 8, 1, 99 and 44 in the list: [1, -2, 32, 8, 17, 19, 42, 13, 0, 44]. Draw a detailed table showing the values of the variables and the decisions taken in each pass of linear search.

Correct Answer

Given List: [1, -2, 32, 8, 17, 19, 42, 13, 0, 44] with n = 10 elements.

Search for Key = 8:

indexindex < nnumList[index] = keyindex = index + 1
00 < 10? Yes1 = 8? No1
11 < 10? Yes-2 = 8? No2
22 < 10? Yes32 = 8? No3
33 < 10? Yes8 = 8? Yes

Element 8 found at position 4 after 4 comparisons.

Search for Key = 1:

indexindex < nnumList[index] = keyindex = index + 1
00 < 10? Yes1 = 1? Yes

Element 1 found at position 1 after 1 comparison (minimum work).

Search for Key = 99:

indexindex < nnumList[index] = keyindex = index + 1
00 < 10? Yes1 = 99? No1
11 < 10? Yes-2 = 99? No2
22 < 10? Yes32 = 99? No3
33 < 10? Yes8 = 99? No4
44 < 10? Yes17 = 99? No5
55 < 10? Yes19 = 99? No6
66 < 10? Yes42 = 99? No7
77 < 10? Yes13 = 99? No8
88 < 10? Yes0 = 99? No9
99 < 10? Yes44 = 99? No10
1010 < 10? No

Element 99 not found in the list after 10 comparisons.

Search for Key = 44:

indexindex < nnumList[index] = keyindex = index + 1
00 < 10? Yes1 = 44? No1
11 < 10? Yes-2 = 44? No2
22 < 10? Yes32 = 44? No3
33 < 10? Yes8 = 44? No4
44 < 10? Yes17 = 44? No5
55 < 10? Yes19 = 44? No6
66 < 10? Yes42 = 44? No7
77 < 10? Yes13 = 44? No8
88 < 10? Yes0 = 44? No9
99 < 10? Yes44 = 44? Yes

Element 44 found at position 10 after 10 comparisons (maximum work).

Exercise: EXERCISE | Q: 1 | (Page Page 14)
For More Understanding

Explanation

This question tests the student's understanding of linear search algorithm execution. The algorithm compares each element sequentially from index 0 until the key is found or the list ends. When key is at position 1 (index 0), only 1 comparison is needed - this is the minimum work. When key is at the last position or not found, n comparisons are needed - this is the maximum work. The tables follow the format shown in the textbook (Tables 6.2, 6.4) where each row represents one pass showing the index value, loop condition check, comparison result, and index increment.

Solution Steps

  1. Step 1: Identify the list [1, -2, 32, 8, 17, 19, 42, 13, 0, 44] with n=10 elements and indices 0-9.

  2. Step 2: For key=8, traverse from index 0 to 3, comparing each element until match found at index 3 (position 4).

  3. Step 3: For key=1, match found immediately at index 0 (position 1) - minimum comparisons.

  4. Step 4: For key=99, traverse entire list without finding, exit when index=10 fails condition index<n.

  5. Step 5: For key=44, traverse entire list, match found at last index 9 (position 10) - maximum comparisons.