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
You’re currently reading “Recursive Linear Search #2,” an entry on Connecting the dots
- Published:
- 2.22.08 / 12am
- Category:
- Programming
No comments
Jump to comment form | comments rss [?] | trackback uri [?]