Skip to content

Commit 4160a21

Browse files
committed
feat: add binarySearch algorithm and update exports in index.ts; bump version in package.json
1 parent 80d7253 commit 4160a21

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dsacjs",
3-
"version": "0.0.1-test",
3+
"version": "0.0.2-test",
44
"description": "A high-performance JavaScript and TypeScript library offering a comprehensive set of efficient data structures. Simplify your algorithm implementation and data manipulation with optimized, easy-to-use tools.",
55
"main": "./dist/index.js",
66
"types": "./types/index.d.ts",

src/Algorithms/binarySearch.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default function binarySearch(arr: number[], target: number): number {
2+
let left = 0;
3+
let right = arr.length - 1;
4+
5+
while (left <= right) {
6+
const mid = Math.floor((left + right) / 2);
7+
8+
if (arr[mid] === target) {
9+
return mid;
10+
}
11+
12+
if (arr[mid] < target) {
13+
left = mid + 1;
14+
} else {
15+
right = mid - 1;
16+
}
17+
}
18+
19+
return -1;
20+
}

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import binarySearch from "./Algorithms/binarySearch";
12
import DoublyLinkedList from "./dataStructures/DoublyLinkedList";
23
import Queue from "./dataStructures/Queue";
34
import SingleLinkedList from "./dataStructures/SingleLinkedList";
@@ -10,4 +11,5 @@ export {
1011
Queue,
1112
DoublyLinkedList as LinkedList,
1213
Logger,
14+
binarySearch,
1315
};

0 commit comments

Comments
 (0)