diff --git a/src/runtimes/02-binary-search.js b/src/runtimes/02-binary-search.js index 4e241b85..c8118160 100644 --- a/src/runtimes/02-binary-search.js +++ b/src/runtimes/02-binary-search.js @@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) { if (current === search) { return offset + half; - } if (array.length === 1) { + } + if (array.length === 1) { return -1; - } if (search > current) { + } + if (search > current) { const right = array.slice(half); return binarySearchRecursive(right, search, offset + half); } @@ -38,50 +40,24 @@ function binarySearchRecursive(array, search, offset = 0) { * @param {string|number} search value to search for */ function binarySearchIterative(array, search) { - // console.log('binarySearchIterative', {array, search}); let start = 0; - let end = array.length; - const half = () => parseInt((end - start) / 2, 10) + start; + let end = array.length - 1; + const half = () => start + parseInt((end - start) / 2, 10); - while (end - start > 0) { + while (start <= end) { const currentIndex = half(); const current = array[currentIndex]; - if (current === search) { - return currentIndex; - } if (search > current) { - start = currentIndex; + if (current === search) return currentIndex; + + if (search > current) { + start = currentIndex + 1; } else if (search < current) { - end = currentIndex; + end = currentIndex - 1; } } return -1; } -// const binarySearch = binarySearchRecursive; -const binarySearch = binarySearchIterative; - -// function test() { -// const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel', -// 'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia', -// 'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe']; -// -// const assert = require('assert'); -// assert.equal(binarySearch([], 'not found'), -1); -// assert.equal(binarySearch([1], 2), -1); -// assert.equal(binarySearch([1], 1), 0); -// assert.equal(binarySearch([1, 2, 3], 1), 0); -// assert.equal(binarySearch([1, 2, 3], 2), 1); -// assert.equal(binarySearch([1, 2, 3], 3), 2); -// assert.equal(binarySearch([1, 2, 3], 31), -1); -// assert.equal(binarySearch(directory, 'Adrian'), 0); -// assert.equal(binarySearch(directory, 'Hanna'), 5); -// assert.equal(binarySearch(directory, 'Zoe'), 18); -// assert.equal(binarySearch(directory, 'not found'), -1); -// } - -// test(); - - -module.exports = { binarySearch, binarySearchIterative, binarySearchRecursive }; +module.exports = { binarySearchIterative, binarySearchRecursive }; diff --git a/src/runtimes/02-binary-search.spec.js b/src/runtimes/02-binary-search.spec.js index 4850cd7c..1e8f8f56 100644 --- a/src/runtimes/02-binary-search.spec.js +++ b/src/runtimes/02-binary-search.spec.js @@ -1,29 +1,39 @@ -const binarySearch = require('./02-binary-search').binarySearchRecursive; +const { + binarySearchRecursive, + binarySearchIterative, +} = require("./02-binary-search"); -describe('Binary Search', () => { - let array; +const binarySearchImplementations = [ + binarySearchRecursive, + binarySearchIterative, +]; - beforeEach(() => { - array = [7, 9, 13, 23]; - }); +binarySearchImplementations.forEach((binarySearchImpl) => { + describe(binarySearchImpl.name, () => { + let array; - it('should find a middle element', () => { - expect(binarySearch(array, 9)).toEqual(1); - }); + beforeEach(() => { + array = [7, 9, 13, 23]; + }); - it('should find an first element', () => { - expect(binarySearch(array, 7)).toEqual(0); - }); + it("should find a middle element", () => { + expect(binarySearchImpl(array, 9)).toEqual(1); + }); - it('should find the last element', () => { - expect(binarySearch(array, 23)).toEqual(3); - }); + it("should find an first element", () => { + expect(binarySearchImpl(array, 7)).toEqual(0); + }); - it('should not find an bigger element', () => { - expect(binarySearch(array, 9000)).toEqual(-1); - }); + it("should find the last element", () => { + expect(binarySearchImpl(array, 23)).toEqual(3); + }); + + it("should not find an bigger element", () => { + expect(binarySearchImpl(array, 9000)).toEqual(-1); + }); - it('should find a smaller element', () => { - expect(binarySearch(array, -9)).toEqual(-1); + it("should find a smaller element", () => { + expect(binarySearchImpl(array, -9)).toEqual(-1); + }); }); });