Skip to content

Commit

Permalink
Fix Binary Search
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed Oct 10, 2017
1 parent de31ca8 commit bacabb4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/algorithms/__tests__/binarySearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ describe('binarySearch', () => {
expect(binarySearch([], 1)).toBe(-1);
});

test('one or two', () => {
test('one or two elements', () => {
expect(binarySearch([1], 1)).toBe(0);
expect(binarySearch([1], 2)).toBe(-1);
expect(binarySearch([1, 4], 1)).toBe(0);
expect(binarySearch([1, 4], 4)).toBe(1);
expect(binarySearch([1, 4], 5)).toBe(-1);
});

test('normal', () => {
test('more than two elements', () => {
expect(binarySearch([1, 2, 3, 10, 11, 20], 1)).toBe(0);
expect(binarySearch([1, 2, 3, 10, 11, 20], 2)).toBe(1);
expect(binarySearch([1, 2, 3, 10, 11, 20], 3)).toBe(2);
Expand Down
1 change: 1 addition & 0 deletions lib/algorithms/binarySearch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function binarySearch(arr, target) {
// TODO - Decide which index to return if there are duplicate values in array.
let left = 0;
let right = arr.length - 1;
while (left <= right) {
Expand Down

0 comments on commit bacabb4

Please sign in to comment.