Q. 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.
Answer :-
For 8:-
index |
index < n |
List[ index ]= key |
index=index+1 |
0 |
0 < 10 |
1 != 8 |
1 |
1 |
1 < 10 |
-2 != 8 |
2 |
2 |
2 < 10 |
32 != 8 |
3 |
3 |
3 < 10 |
8 = 8 |
4 |
So, position of 8 is 4
For 1:-
index |
index < n |
List[ index ]= key |
index=index+1 |
0 |
0 < 10 |
1 = 1 |
1 |
So, position of 1 is 1
For 99:-
index |
index < n |
List[ index ]= key |
index=index+1 |
0 |
0 < 10 |
1 != 99 |
1 |
1 |
1 < 10 |
-2 != 99 |
2 |
2 |
2 < 10 |
32 != 99 |
3 |
3 |
3 < 10 |
8 != 99 |
4 |
4 |
4 < 10 |
17 != 99 |
5 |
5 |
5 < 10 |
19 != 99 |
6 |
6 |
6 < 10 |
42 != 99 |
7 |
7 |
7 < 10 |
13 != 99 |
8 |
8 |
8 < 10 |
0 != 99 |
9 |
9 |
9 < 10 |
44 != 99 |
10 |
So, 99 is not found in given list.
For 44:-
index |
index < n |
List[ index ]= key |
index=index+1 |
0 |
0 < 10 |
1 != 44 |
1 |
1 |
1 < 10 |
-2 != 44 |
2 |
2 |
2 < 10 |
32 != 44 |
3 |
3 |
3 < 10 |
8 != 44 |
4 |
4 |
4 < 10 |
17 != 44 |
5 |
5 |
5 < 10 |
19 != 44 |
6 |
6 |
6 < 10 |
42 != 44 |
7 |
7 |
7 < 10 |
13 != 44 |
8 |
8 |
8 < 10 |
0 != 44 |
9 |
9 |
9 < 10 |
44 = 44 |
10 |
So, the position of 44 is 10.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )