I’m solving a problem (leetcode 35). My code was accepted in run code result but when I submit it returns wrong answer.I don’t really understand what is wrong in my answer . Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were
Tag: binary-search
Improve speed of my binary search algorithm
I have written a binary search algorithm in 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: Answer You are taking values as indices. If you take greater values than indices, you see your codes
How to create a binary search with recursion
I am attempting to write a “binary search” which I’ve never done before. The code below does not work when the value searched for is 6 or 2 and I want to know what I am doing wrong and how to remedy it. EDIT To explain what it is suppose to do (based on my understanding) a binary search requires
Binary Search in Javascript
I’m trying to implement a binary search algorithm in JavaScript. Things seem okay, but my return statements appear to be returning undefined. Can anybody tell me what’s wrong here? Fiddle: http://jsfiddle.net/2mBdL/ Answer You’re not explicitly returning the recursive inner calls (i.e. return binarySearch()), so the call stack unfolds with no return value. Update your code like so: See a working