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 5b49d8a

Browse files
fix(runtimes/02-binary-search):fixes binary search iterative
1 parent ae41553 commit 5b49d8a

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

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

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,24 @@ function binarySearchRecursive(array, search, offset = 0) {
3838
* @param {string|number} search value to search for
3939
*/
4040
function binarySearchIterative(array, search) {
41-
// console.log('binarySearchIterative', {array, search});
4241
let start = 0;
43-
let end = array.length;
44-
const half = () => parseInt((end - start) / 2, 10)+start;
42+
let end = array.length-1;
43+
const half = () => start+parseInt((end - start) / 2, 10);
4544

46-
while (end-start >0) {
45+
while (start <=end) {
4746
const currentIndex = half();
4847
const current = array[currentIndex];
4948

50-
if (current === search) {
51-
returncurrentIndex;
52-
}if (search > current) {
53-
start = currentIndex;
49+
if (current === search) returncurrentIndex;
50+
51+
if (search > current) {
52+
start = currentIndex+1;
5453
} else if (search < current) {
55-
end = currentIndex;
54+
end = currentIndex-1;
5655
}
5756
}
5857

5958
return -1;
6059
}
6160

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 };
61+
module.exports = { binarySearchIterative, binarySearchRecursive };

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
1-
const binarySearch= require('./02-binary-search').binarySearchRecursive;
1+
const { binarySearchRecursive, binarySearchIterative }= require('./02-binary-search');
22

3-
describe('Binary Search', () => {
3+
describe('Binary Search Recursive', () => {
44
let array;
55

66
beforeEach(() => {
77
array = [7, 9, 13, 23];
88
});
99

1010
it('should find a middle element', () => {
11-
expect(binarySearch(array, 9)).toEqual(1);
11+
expect(binarySearchRecursive(array, 9)).toEqual(1);
1212
});
1313

1414
it('should find an first element', () => {
15-
expect(binarySearch(array, 7)).toEqual(0);
15+
expect(binarySearchRecursive(array, 7)).toEqual(0);
1616
});
1717

1818
it('should find the last element', () => {
19-
expect(binarySearch(array, 23)).toEqual(3);
19+
expect(binarySearchRecursive(array, 23)).toEqual(3);
2020
});
2121

2222
it('should not find an bigger element', () => {
23-
expect(binarySearch(array, 9000)).toEqual(-1);
23+
expect(binarySearchRecursive(array, 9000)).toEqual(-1);
2424
});
2525

2626
it('should find a smaller element', () => {
27-
expect(binarySearch(array, -9)).toEqual(-1);
27+
expect(binarySearchRecursive(array, -9)).toEqual(-1);
28+
});
29+
});
30+
31+
describe('Binary Search Iterative', () => {
32+
let array;
33+
34+
beforeEach(() => {
35+
array = [7, 9, 13, 23];
36+
});
37+
38+
it('should find a middle element', () => {
39+
expect(binarySearchIterative(array, 9)).toEqual(1);
40+
});
41+
42+
it('should find an first element', () => {
43+
expect(binarySearchIterative(array, 7)).toEqual(0);
44+
});
45+
46+
it('should find the last element', () => {
47+
expect(binarySearchIterative(array, 23)).toEqual(3);
48+
});
49+
50+
it('should not find an bigger element', () => {
51+
expect(binarySearchIterative(array, 9000)).toEqual(-1);
52+
});
53+
54+
it('should find a smaller element', () => {
55+
expect(binarySearchIterative(array, -9)).toEqual(-1);
2856
});
2957
});

0 commit comments

Comments
(0)

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