KnowledgeBoat Logo
|
LoginJOIN NOW

Computer Science

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.

Python Searching

4 Likes

Answer

list1 = [1, -2, 32, 8, 17, 19, 42, 13, 0, 44]
n = 10

1. Item = 8

index0123456789
elements1-232817194213044

The step-by-step process of linear search is as follows:

indexindex < nlist1[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

Therefore, for item 8, linear search returns 3 (index).

2. Item = 1

index0123456789
elements1-232817194213044

The step-by-step process of linear search is as follows:

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

Therefore, for item 1, linear search returns 0 (index).

3. Item = 99

index0123456789
elements1-232817194213044

The step-by-step process of linear search is as follows:

indexindex < nlist1[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 = ? No10
1010 < 10 ? No

Since the item 99 is not found in the list, the linear search algorithm returns -1.

4. Item = 44

index0123456789
elements1-232817194213044

The step-by-step process of linear search is as follows:

indexindex < nlist1[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

Therefore, for item 44, linear search returns 9 (index).

Answered By

1 Like


Related Questions