Skip to content

Commit

Permalink
Second take: Binary search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas C. Zakas committed Aug 30, 2009
1 parent fdd3c10 commit 844c331
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions algorithms/searching/binary-search/binary-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
function binarySearch(items, value){

var startIndex = 0,
middle = Math.floor(items.length / 2),
stopIndex = items.length;
stopIndex = items.length - 1,
middle = Math.floor((stopIndex + startIndex)/2);

while(items[middle] != value && startIndex < stopIndex){

Expand All @@ -44,10 +44,9 @@ function binarySearch(items, value){
}

//recalculate middle
middle = Math.floor((stopIndex + startIndex)/2);

middle = Math.floor((stopIndex + startIndex)/2);
}

//make sure it's the right value
return (items[middle] != value) ? -1 : middle;
}
}

0 comments on commit 844c331

Please sign in to comment.