CLASS PARTICIPATION (Search Algorithms)
CLASS PARTICIPATION (Search Algorithms)
discussion.
Answer: A linear search checks each item in a list one by one until it finds the target
or reaches the end. It’s easy to understand but can be slow if the target is far
down the list.
Answer: A binary search is a faster way to find something in a sorted list. It starts
by checking the middle item, then decides to search in either the left or right half.
It keeps splitting the list in half until it finds the target. This makes it quicker than
linear search for large lists.
3. Use linear and binary search algorithm to the following array and compare their
performance based on the number of comparisons (Target Value is 34).
54 45 12 67 34 27 78 91 83 8
Linear Search:
54 ≠ 34
45 ≠ 34
12 ≠ 34
67 ≠ 34
34 = 34 (Found!)
Binary Search:
mid = low+(high-low)/2
= 0+(9-0)/2
=4.5/4
34=34
The index 4 is 34 and the number that should get.
Binary Search Comparisons: 1