0% found this document useful (0 votes)
3 views

Linear and Binary Search Algorithms Homework

The document explains binary and linear search algorithms, detailing how binary search efficiently narrows down the search space by comparing midpoints, while linear search checks each element sequentially. Examples illustrate the process of both algorithms using ordered and unordered lists. It concludes that binary search is generally more efficient for larger datasets compared to linear search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Linear and Binary Search Algorithms Homework

The document explains binary and linear search algorithms, detailing how binary search efficiently narrows down the search space by comparing midpoints, while linear search checks each element sequentially. Examples illustrate the process of both algorithms using ordered and unordered lists. It concludes that binary search is generally more efficient for larger datasets compared to linear search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Linear and Binary Search Algorithms Homework

Thursday 23 January 2025 11:46 am

1. A binary search starts by finding the midpoint of the index of the ordered data and compares the value of
that index with the value you want to find. If the value if less than the value you are currently at, then you
eliminate the other half of the index, and vice versa. You then repeat the steps until you find the value
you are searching for, at which the search stops.

2. Ensure the list is ordered


[3, 7, 10, 11, 13, 15]
Midpoint = 11, so look at lower half of list
[3, 7, 10]
Midpoint = 7, so look at upper half of list
[10]
Value has been found

3. [10, 14, 3, 11, 7, 13]


Order the list
[3, 7, 10, 11, 13, 15]
Midpoint = 11, so look at upper half of list
[13, 15]
Midpoint = 13, so look at upper half of list
[15]
Value has been found

4. Linear search:
[2, 4, 7, 12, 15, 18, 20]
Compare each value with the desired value:
2 = 20? No
4 = 20? No
7 = 20? No
12 = 20? No
15 = 20? No
18 = 20? No
20 = 20? Yes
Value has been found

Binary search:
[2, 4, 7, 12, 15, 18, 20]
Midpoint = 12, so look at the upper half of list
[15, 18, 20]
Midpoint = 18, so look at the upper half of list
[20]
Value has been found

5. A binary search is generally more efficient because it is quicker for searching larger sets of data, as it
removes half of the set of data with each comparison. A linear search would have to compare each value
from beginning to end, which means it is either very efficient or very inefficient, making its efficiency
much more unreliable than a binary search.

You might also like