Skip to content
  • Sponsor TheAlgorithms/TypeScript

  • Notifications You must be signed in to change notification settings
  • Fork 436
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1fe60c4

Browse files
committedAug 3, 2024·
fix formatting
1 parent 5bb6396 commit 1fe60c4

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed
 

‎search/fibonacci_search.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,40 @@ export const fibonacciSearch = (
1818
target: number
1919
): number | null => {
2020
const arrayLength = array.length
21-
let a = 0; // (n-2)'th Fibonacci No.
22-
let b = 1; // (n-1)'th Fibonacci No.
23-
let c = a + b; // n'th Fibonacci
21+
let a = 0 // (n-2)'th Fibonacci No.
22+
let b = 1 // (n-1)'th Fibonacci No.
23+
let c = a + b // n'th Fibonacci
2424

2525
while (c < arrayLength) {
26-
a = b;
27-
b = c;
28-
c = a + b;
26+
a = b
27+
b = c
28+
c = a + b
2929
}
3030

3131
let offset = -1
3232

3333
while (c > 1) {
34-
let i = Math.min(offset + a, arrayLength - 1);
34+
let i = Math.min(offset + a, arrayLength - 1)
3535

3636
if (array[i] < target) {
37-
c = b;
38-
b = a;
39-
a = c - b;
40-
offset = i;
37+
c = b
38+
b = a
39+
a = c - b
40+
offset = i
4141
} else if (array[i] > target) {
42-
c = a;
43-
b = b - a;
44-
a = c - b;
42+
c = a
43+
b = b - a
44+
a = c - b
4545
} else {
4646
// Element found then return index
47-
return i;
47+
return i
4848
}
4949
}
5050

5151
if (b && array[offset + 1] === target) {
52-
return offset + 1;
52+
return offset + 1
5353
}
5454

5555
// Element not found then return null
56-
return null;
56+
return null
5757
}

‎search/test/fibonacci_search.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { fibonacciSearch } from "../fibonacci_search";
1+
import { fibonacciSearch } from '../fibonacci_search'
22

33
describe('Fibonacci search', () => {
4-
test.each([
5-
[[1, 2, 3], 2, 1],
6-
[[4, 5, 6], 2, null],
7-
[[10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], 85, 8],
8-
[[], 1, null],
9-
[[1], 1, 0],
10-
[[1, 3, 5, 7, 9, 11, 13], 11, 5],
11-
[[1, 3, 5, 7, 9, 11, 13], 8, null]
12-
])(
13-
'of %o, searching for %o, expected %i',
14-
(array: number[], target: number, expected: number | null) => {
15-
expect(fibonacciSearch(array, target)).toBe(expected);
16-
}
17-
);
18-
});
4+
test.each([
5+
[[1, 2, 3], 2, 1],
6+
[[4, 5, 6], 2, null],
7+
[[10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], 85, 8],
8+
[[], 1, null],
9+
[[1], 1, 0],
10+
[[1, 3, 5, 7, 9, 11, 13], 11, 5],
11+
[[1, 3, 5, 7, 9, 11, 13], 8, null]
12+
])(
13+
'of %o, searching for %o, expected %i',
14+
(array: number[], target: number, expected: number | null) => {
15+
expect(fibonacciSearch(array, target)).toBe(expected)
16+
}
17+
)
18+
})

0 commit comments

Comments
 (0)
Please sign in to comment.