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 ab84445

Browse files
committed
--fix : eslint & coverage 100% & return -1 if not found
1 parent e3f3ff3 commit ab84445

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed
Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
const{jumpSearch, jumpSearchRecursive }=require('.');
1+
const{jumpSearch}=require('.');
22

3-
describe('Jump Search', () => {
4-
const array = [1, 2, 3, 4, 5, 6, 7, 8];
5-
describe('When element to find is at 1st position ', () => {
6-
it('Jump search', () => {
7-
expect(jumpSearch(array, 1)).toEqual(0);
8-
});
9-
});
10-
describe('When element to find is at last position ', () => {
11-
it('Jump search', () => {
12-
expect(jumpSearch(array, 7)).toEqual(6);
13-
});
14-
});
15-
describe('When element to find is at random position ', () => {
16-
it('Jump search', () => {
17-
expect(jumpSearch(array, 3)).toEqual(2);
18-
});
19-
});
20-
});
3+
describe('Jump Search', () => {
4+
const array = [1, 2, 3, 4, 5, 6, 7, 8, 20];
5+
describe('When element to find is at 1st position ', () => {
6+
it('Jump search', () => {
7+
expect(jumpSearch(array, 1)).toEqual(0);
8+
});
9+
});
10+
describe('When element to find is at last position ', () => {
11+
it('Jump search', () => {
12+
expect(jumpSearch(array, 20)).toEqual(8);
13+
});
14+
});
15+
describe('When element to find is at random position ', () => {
16+
it('Jump search', () => {
17+
expect(jumpSearch(array, 3)).toEqual(2);
18+
expect(jumpSearch(array, 5)).toEqual(4);
19+
expect(jumpSearch(array, 6)).toEqual(5);
20+
expect(jumpSearch(array, 8)).toEqual(7);
21+
});
22+
});
23+
describe('When element is not in array ', () => {
24+
it('Jump search', () => {
25+
expect(jumpSearch(array, 15)).toEqual(-1);
26+
expect(jumpSearch(array, 25)).toEqual(-1);
27+
expect(jumpSearch(array, 9)).toEqual(-1);
28+
});
29+
});
30+
});

‎src/_Searching_/JumpSearch/index.js‎

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
/**
22
* Note: Array must be sorted for jump search
3-
* Complexity:
3+
* Complexity:
44
* Worst case time complexity: O(√N)
55
* Average case time complexity: O(√N)
66
* Best case time complexity: O(1)
77
* Space complexity: O(1)
88
*/
99
function jumpSearch(arr, key) {
10-
const n = arr.length;
11-
const jump = Math.floor(Math.sqrt(n));
12-
let step = jump;
10+
const n = arr.length;
11+
const jump = Math.floor(Math.sqrt(n));
12+
let step = jump;
13+
let prev = 0;
1314

14-
let prev = 0;
15-
16-
while(arr[Math.min(step, n) - 1] < key) {
17-
prev = step;
18-
step += jump;
19-
if (prev >= n)
20-
return null;
21-
}
15+
while (arr[Math.min(step, n) - 1] < key) {
16+
prev = step;
17+
step += jump;
18+
if (prev >= n) { return -1; }
19+
}
20+
21+
while (arr[prev] < key && prev < Math.min(step, n)) {
22+
prev += 1;
23+
}
2224

23-
while(arr[prev] < key) {
24-
prev++;
25+
if (arr[prev] === key) { return prev; }
2526

26-
if (prev == Math.min(step, n))
27-
return null;
28-
}
29-
30-
if (arr[prev] == key)
31-
return prev;
27+
return -1;
28+
}
3229

33-
return null;
34-
}
35-
36-
module.exports = {
37-
jumpSearch,
38-
};
30+
module.exports = {
31+
jumpSearch,
32+
};

0 commit comments

Comments
(0)

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