129k views
4 votes
Given list: {5 17 51 71 76 77 83 94}

Which list elements will be checked to find the value:
a) 5 using binary search?
b) 5 using linear search?
c) 76 using binary search?
d) 76 using linear search?
e) Which search method is faster to find 5?
f) Which search method is faster to find 76?

1 Answer

5 votes

Final answer:

Binary search and linear search are two different methods to search for values in a list. Binary search splits the search space in half at each step and is faster to find values like 5, while linear search checks each element sequentially and is faster to find values like 76.

Step-by-step explanation:

a) Binary search works by repeatedly dividing the search space in half. To find the value 5 in the given list, the algorithm would check the middle element first (51) and determine that 5 is smaller, so it would continue searching in the first half of the list. It would then check the middle element of that half (17) and determine that 5 is smaller, so it would continue searching in the first quarter of the list. Finally, it would check the middle element of that quarter (5) and find the desired value.

b) Linear search works by sequentially examining each element in the list until the desired value is found. To find the value 5 in the given list, the algorithm would start at the beginning and check each element until it finds 5.

c) To find the value 76 using binary search, the algorithm would follow a similar process as described above. It would start with the middle element (76) and determine that 76 is greater, so it would continue searching in the second half of the list. It would then check the middle element of that half (83) and determine that 76 is smaller, so it would continue searching in the third quarter of the list. Finally, it would check the middle element of that quarter (77) and find the desired value.

d) To find the value 76 using linear search, the algorithm would start at the beginning and check each element until it finds 76.

e) Binary search is faster to find 5, as it takes fewer comparisons. It divides the search space in half at each step, whereas linear search needs to examine each element.

f) Linear search is faster to find 76, as it would find the value on a later comparison in the list, while binary search would need to divide the search space multiple times to find it.

User Yusaf Khaliq
by
8.2k points