Explore BrainMass
Share

Using a Linear Search to Find the Smallest Element in a List

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

Please help with the following problem.

I am having trouble figuring out how to determine the last element of the print statement. For array A(N) and input element ARG, what kind of algorithm is needed to determine which element of A(N) is closest to but not greater than ARG? I've gotten this far but can't find anything in my course manual that describes this situation.

If A(1) < ARG > A(N) then
CODE = 2
Print ARG, CODE, A(N) that is closest to but not greater than ARG

© BrainMass Inc. brainmass.com October 24, 2018, 9:54 pm ad1c9bdddf
https://brainmass.com/computer-science/searching/using-linear-search-find-the-smallest-element-list-141314

Solution Preview

In order to find the element in the array with the closest, but not greater than, value to ARG you will have to go through the entire array. Going through an array one element at a time requires some sort of loop statement. So we will have something like this:

I = 0;
WHILE (I < N)
// do ...

Solution Summary

In this solution, I demonstrate how to search a list of numbers for the largest value less than a given argument. I start with a simple solution and then add the extra logic necessary for the more complex condition.

$2.19
See Also This Related BrainMass Solution

Detailed Comparision of Linear and Binary Searches on a List

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.

View Full Posting Details