Recursive Linear Search

I am really not so good with programmig recursions so i decided to take the simplest kind. I chose recursive linear search (if there is a thing like that) .

a = [3,1,2,7,5,6,9,3,6]

def search(array,key,index)
begin
if key==array[index]
puts “#{key} has been found”
else
return search(array,key,index+1)
end
rescue
puts “#{key} was not found”
end
end

h = search(a,888,0)
First it takes the the array, key, and index. The base case is that if the key and index are the same then stop else move to the next element and test for it.


About this entry