Skip to content

Instantly share code, notes, and snippets.

@albertein
Created January 31, 2013 18:23
Show Gist options
  • Save albertein/4685018 to your computer and use it in GitHub Desktop.
Save albertein/4685018 to your computer and use it in GitHub Desktop.
Recursive binary search
def search(array, number) #array must be sorted
pivot = array.size / 2
return pivot if array[pivot] == number
if array[pivot] > number #Search on the left
return search array.slice 0, pivot, number
else #Search on the right
return search array.slice pivot, array.size - pivot
end
end
@albertein
Copy link
Author

Ignoring the case where the number is not found for didactic purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment