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 941c83e

Browse files
committed
--update : add BinarySearch test
1 parent 131a270 commit 941c83e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { binarySearch, binarySearchRecursive } = require('.');
2+
3+
describe('Binary Search', () => {
4+
const array = [1, 2, 3, 4, 5, 6, 7, 8];
5+
const low = 0;
6+
const high = array.length - 1;
7+
8+
describe('When element to find is at 1st position ', () => {
9+
it('Binary search with Loop', () => {
10+
expect(binarySearch(array, 1)).toEqual(0);
11+
});
12+
it('Binary serach with recursion', () => {
13+
expect(binarySearchRecursive(array, low, high, 1)).toEqual(0);
14+
});
15+
});
16+
describe('When element to find is at last position ', () => {
17+
it('Binary search with Loop', () => {
18+
expect(binarySearch(array, 7)).toEqual(6);
19+
});
20+
it('Binary serach with recursion', () => {
21+
expect(binarySearchRecursive(array, low, high, 7)).toEqual(6);
22+
});
23+
});
24+
describe('When element to find is at random position ', () => {
25+
it('Binary search with Loop', () => {
26+
expect(binarySearch(array, 3)).toEqual(2);
27+
});
28+
it('Binary serach with recursion', () => {
29+
expect(binarySearchRecursive(array, low, high, 3)).toEqual(2);
30+
});
31+
});
32+
describe('When element to find is no present in array ', () => {
33+
it('Binary search with Loop', () => {
34+
expect(binarySearch(array, 10)).toEqual(null);
35+
});
36+
it('Binary serach with recursion', () => {
37+
expect(binarySearchRecursive(array, low, high, 10)).toEqual(null);
38+
});
39+
});
40+
41+
});

‎src/Searching/BinarySearch/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ function binarySearchRecursive(arr, low, high, key) {
3333
return binarySearchRecursive(arr, mid + 1, high, key);
3434
}
3535
}
36+
37+
module.exports = {
38+
binarySearch,
39+
binarySearchRecursive,
40+
};

0 commit comments

Comments
(0)

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