Skip to content

fix(runtimes/02-binary-search):fixes binary search iterative #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 13 additions & 37 deletions src/runtimes/02-binary-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 };
50 changes: 30 additions & 20 deletions src/runtimes/02-binary-search.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});