For this program you will rst read in 10 integers and store them in an array. Th
ID: 3591658 • Letter: F
Question
For this program you will rst read in 10 integers and store them in an
array. Then you will search the array using the recursive version of linear
search. Be sure to test the program both for the case where the integer
for which you are looking is in the array and the integer for which you are
looking is not in the array. If the integer is in the array, be sure to give the
index of the rst occurrence of the integer for which you are looking.
Notice that there are a couple of minor changes from the algorithm dis-
cussed in class. Why is this better?
Search the array a[0,..,N-1] for the element s value.
Procedure LSearch ( array Array, int i,
int M, value Search )
begin LSearch
if ( i > M )
return not found
if ( Array[i] = Search )
return found at i
LSearch ( Array, i + 1, Search )
end LSearch
Linear Search:
begin
LSearch ( a, 0, N - 1, s value )
end
1
Explanation / Answer
Hi Friend, I did not get your question clearly. What you want to understand from given algorithm.
Search the array a[0,..,N-1] for the element s value.
Procedure LSearch ( array Array, int i, int M, value Search )
begin LSearch
if ( i > M )
return not found
if ( Array[i] = Search )
return found at i
LSearch ( Array, i + 1, Search )
end LSearch
Linear Search:
begin
LSearch ( a, 0, N - 1, s value )
end
Linear Search takes O(N) time in worst case.
This is better because when we get an element in an array that is equal to search value, we immediately returning the index value.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.