Explore BrainMass

Detailed comparision of linear and binary searches on a list

This content was STOLEN 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.

© BrainMass Inc. brainmass.com October 25, 2018, 5:44 am ad1c9bdddf

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.

See Also This Related BrainMass Solution

Qualitative Analysis for Management

Solving real world complex problems using either traditional optimization techniques or modern heuristic techniques would make interesting work.

What do the terms heuristic and optimization (or optimal) mean?
What is a quantitative application for each? Some independent research is needed.
What was your source(s) for addressing this question? Provide a URL address(s) if applicable.
Don't hesitate to use outside sources in addressing questions in this course, and apply your understanding as it relates to your prior knowledge and experience where appropriate.

View Full Posting Details