DEV Community

Discussion on: Algorithms for Ruby developer: Binary search

Collapse
 
djuber profile image
Daniel Uber

One thing to bear in mind about the performance benchmark results, the (slower) Array#find_index method uses a linear search because the Array method cannot assume that the array is already sorted (the precondition for binary search to work). Just pointing this out since the required "sorted input" condition wasn't mentioned in your post. I'm sure it's covered in the linked video (he says "hopefully sorted" but then uses a telephone book which is definitely sorted), so worth pointing out.

Collapse
 
kopylov_vlad profile image
Vladislav Kopylov

Thanks for your comment 😀
Yes, the binary search algorithm can work properly only with sorted data. Array#find_index can perfectly work with sorted and unsorted data. But if you have huge array of data and the array is sorted, binary search will be highly effective.

So, my intention was to inspire people (and me) to keep on studying algorithms, maybe it's the reason why I didn't mention it 🙈