Explore BrainMass
Share

# Detailed comparision of linear and binary searches on a list

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

Suppose that you have a dictionary whose words are not sorted in alphabetical order. As a function of the number, n of words, what is the efficiency of searching for a particular word in this dictionary ?

Do the same for dictionary whose words are sorted alphabetically. Compare results.

https://brainmass.com/computer-science/searching/detailed-comparision-linear-and-binary-searches-list-432843

#### Solution Preview

The only way to search a list of unsorted elements is to use a loop to look at each element in the list. For example:

foreach (e in list) {
if (e == targetValue) {
found = true;
}
}

Assuming random data, approximately half of the time the sought after element will be in the first half of the list while half of the time it will be in the second half of the list. This means that the average number of elements that have to be inspected is n/2 (for a list of n elements). In O() notation this is O(n).

A dictionary whose words are not sorted is simply a list ...

#### Solution Summary

This solution provides a detailed comparison of searching a list of words using a linear search vs. using a binary search. The discussions explains the expected performance in O() notation and shows how the O() value is calculated.

\$2.19