Recursive Linear Search #2

So, the code for the first search was OK, but will curtainly not help an non ruby programmer. So i have decided to modify it a bit. This time, instead of using the begin…..rescue…….end statement we maintain a state of our position with respect to the last position.

1  a = [1,2,3,4,5,4]

2  def find(array,position,key)
3    if position >array.size
4      puts  “#{key} not found”
5    elsif key == array[position]
6     puts “Found #{key}”
7    else
8      find(array,position+1,key)
9    end
10  end

11  find(a,0,4)

Hope this is better. I am thinking of writing on a ruby bubble sort.


About this entry