Skip to content
Advertisement

Improve speed of my binary search algorithm

I have written a binary search algorithm in JavaScript:

JavaScript

I wanted to ask if I can improve this algorithm to search faster or if some mistake is made here?

EDIT:

thank you guys for your help, this solution should work correctly now:

JavaScript

Advertisement

Answer

You are taking values as indices. If you take greater values than indices, you see your codes does not work.

Instead, you could take the index of middle for left or rightif not found.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement