Skip to content

Commit

Permalink
fix : prettier fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Fechuli committed Aug 4, 2024
1 parent 5b37d3b commit 44b698d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
67 changes: 33 additions & 34 deletions search/exponential_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,37 @@
*/

export const exponentialSearch = (
array: number[],
x: number
): number | null => {
const arrayLength = array.length;
if (arrayLength === 0) return null;

if (array[0] === x) return 0;

let i = 1;
while (i < arrayLength && array[i] <= x) {
i = i * 2;
}

return binarySearch(array, x, i / 2, Math.min(i, arrayLength - 1));
};

const binarySearch = (
array: number[],
x: number,
start: number,
end: number
): number | null => {
while (start <= end) {
const mid = Math.floor((start + end) / 2);
if (array[mid] === x) {
return mid;
} else if (array[mid] < x) {
start = mid + 1;
} else {
end = mid - 1;
}
array: number[],
x: number
): number | null => {
const arrayLength = array.length
if (arrayLength === 0) return null

if (array[0] === x) return 0

let i = 1
while (i < arrayLength && array[i] <= x) {
i = i * 2
}

return binarySearch(array, x, i / 2, Math.min(i, arrayLength - 1))
}

const binarySearch = (
array: number[],
x: number,
start: number,
end: number
): number | null => {
while (start <= end) {
const mid = Math.floor((start + end) / 2)
if (array[mid] === x) {
return mid
} else if (array[mid] < x) {
start = mid + 1
} else {
end = mid - 1
}
return null;
};
}
return null
}
8 changes: 4 additions & 4 deletions search/test/exponential_search.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exponentialSearch } from '../exponential_search';
import { exponentialSearch } from '../exponential_search'

describe('Exponential search', () => {
test.each([
Expand All @@ -13,7 +13,7 @@ describe('Exponential search', () => {
])(
'of %o, searching for %o, expected %i',
(array: number[], target: number, expected: number | null) => {
expect(exponentialSearch(array, target)).toBe(expected);
expect(exponentialSearch(array, target)).toBe(expected)
}
);
});
)
})

0 comments on commit 44b698d

Please sign in to comment.