We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fdd3c10 commit 844c331Copy full SHA for 844c331
algorithms/searching/binary-search/binary-search.js
@@ -31,8 +31,8 @@
31
function binarySearch(items, value){
32
33
var startIndex = 0,
34
- middle = Math.floor(items.length / 2),
35
- stopIndex = items.length;
+ stopIndex = items.length - 1,
+ middle = Math.floor((stopIndex + startIndex)/2);
36
37
while(items[middle] != value && startIndex < stopIndex){
38
@@ -44,10 +44,9 @@ function binarySearch(items, value){
44
}
45
46
//recalculate middle
47
- middle = Math.floor((stopIndex + startIndex)/2);
48
-
49
50
51
//make sure it's the right value
52
return (items[middle] != value) ? -1 : middle;
53
-}
+}
0 commit comments