Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41205d6

Browse files
Merge pull request amejiarosario#113 from gabrielastelescu/binary-search-iterative
fix(runtimes/02-binary-search):fixes binary search iterative
2 parents ae41553 + c9249d3 commit 41205d6

File tree

2 files changed

+43
-57
lines changed

2 files changed

+43
-57
lines changed

‎src/runtimes/02-binary-search.js

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) {
1818

1919
if (current === search) {
2020
return offset + half;
21-
} if (array.length === 1) {
21+
}
22+
if (array.length === 1) {
2223
return -1;
23-
} if (search > current) {
24+
}
25+
if (search > current) {
2426
const right = array.slice(half);
2527
return binarySearchRecursive(right, search, offset + half);
2628
}
@@ -38,50 +40,24 @@ function binarySearchRecursive(array, search, offset = 0) {
3840
* @param {string|number} search value to search for
3941
*/
4042
function binarySearchIterative(array, search) {
41-
// console.log('binarySearchIterative', {array, search});
4243
let start = 0;
43-
let end = array.length;
44-
const half = () => parseInt((end - start) / 2, 10)+start;
44+
let end = array.length-1;
45+
const half = () => start+parseInt((end - start) / 2, 10);
4546

46-
while (end-start >0) {
47+
while (start <=end) {
4748
const currentIndex = half();
4849
const current = array[currentIndex];
4950

50-
if (current === search) {
51-
returncurrentIndex;
52-
}if (search > current) {
53-
start = currentIndex;
51+
if (current === search) returncurrentIndex;
52+
53+
if (search > current) {
54+
start = currentIndex+1;
5455
} else if (search < current) {
55-
end = currentIndex;
56+
end = currentIndex-1;
5657
}
5758
}
5859

5960
return -1;
6061
}
6162

62-
// const binarySearch = binarySearchRecursive;
63-
const binarySearch = binarySearchIterative;
64-
65-
// function test() {
66-
// const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel',
67-
// 'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia',
68-
// 'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe'];
69-
//
70-
// const assert = require('assert');
71-
// assert.equal(binarySearch([], 'not found'), -1);
72-
// assert.equal(binarySearch([1], 2), -1);
73-
// assert.equal(binarySearch([1], 1), 0);
74-
// assert.equal(binarySearch([1, 2, 3], 1), 0);
75-
// assert.equal(binarySearch([1, 2, 3], 2), 1);
76-
// assert.equal(binarySearch([1, 2, 3], 3), 2);
77-
// assert.equal(binarySearch([1, 2, 3], 31), -1);
78-
// assert.equal(binarySearch(directory, 'Adrian'), 0);
79-
// assert.equal(binarySearch(directory, 'Hanna'), 5);
80-
// assert.equal(binarySearch(directory, 'Zoe'), 18);
81-
// assert.equal(binarySearch(directory, 'not found'), -1);
82-
// }
83-
84-
// test();
85-
86-
87-
module.exports = { binarySearch, binarySearchIterative, binarySearchRecursive };
63+
module.exports = { binarySearchIterative, binarySearchRecursive };

‎src/runtimes/02-binary-search.spec.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
const binarySearch = require('./02-binary-search').binarySearchRecursive;
1+
const {
2+
binarySearchRecursive,
3+
binarySearchIterative,
4+
} = require("./02-binary-search");
25

3-
describe('Binary Search', () => {
4-
let array;
6+
const binarySearchImplementations = [
7+
binarySearchRecursive,
8+
binarySearchIterative,
9+
];
510

6-
beforeEach(() => {
7-
array=[7,9,13,23];
8-
});
11+
binarySearchImplementations.forEach((binarySearchImpl) => {
12+
describe(binarySearchImpl.name,()=>{
13+
letarray;
914

10-
it('should find a middle element',() => {
11-
expect(binarySearch(array, 9)).toEqual(1);
12-
});
15+
beforeEach(() => {
16+
array=[7, 9,13,23];
17+
});
1318

14-
it('should find an first element', () => {
15-
expect(binarySearch(array, 7)).toEqual(0);
16-
});
19+
it("should find a middle element", () => {
20+
expect(binarySearchImpl(array, 9)).toEqual(1);
21+
});
1722

18-
it('should find the last element', () => {
19-
expect(binarySearch(array, 23)).toEqual(3);
20-
});
23+
it("should find an first element", () => {
24+
expect(binarySearchImpl(array, 7)).toEqual(0);
25+
});
2126

22-
it('should not find an bigger element', () => {
23-
expect(binarySearch(array, 9000)).toEqual(-1);
24-
});
27+
it("should find the last element", () => {
28+
expect(binarySearchImpl(array, 23)).toEqual(3);
29+
});
30+
31+
it("should not find an bigger element", () => {
32+
expect(binarySearchImpl(array, 9000)).toEqual(-1);
33+
});
2534

26-
it('should find a smaller element', () => {
27-
expect(binarySearch(array, -9)).toEqual(-1);
35+
it("should find a smaller element", () => {
36+
expect(binarySearchImpl(array, -9)).toEqual(-1);
37+
});
2838
});
2939
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /