This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a360d99
commit ae5fc0d
Showing
12 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@codinasion/binary-search": major | ||
--- | ||
|
||
Initial Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** @type {import("eslint").Linter.Config} */ | ||
module.exports = { | ||
root: true, | ||
extends: ["@codinasion/eslint-config/library.js"], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: true, | ||
}, | ||
env: { | ||
jest: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023 Codinasion | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# `@codinasion/binary-search` | ||
|
||
Typescript implementation of binary search. | ||
|
||
![NPM Version](https://img.shields.io/npm/v/%40codinasion%2Fbinary-search?color=33cd56&logo=npm) ![Downloads](https://img.shields.io/npm/dm/%40codinasion%2Fbinary-search?logo=docusign&label=Downloads&logoColor=white) ![LICENSE](https://img.shields.io/npm/l/%40codinasion%2Fbinary-search?logo=googledocs&logoColor=white&label=LICENSE) | ||
|
||
<div align="center"> | ||
<img src="https://raw.githubusercontent.com/codinasion/codinasion/master/assets/rainbow-hr.png" alt="rainbow hr" width="100%" height="70%"> | ||
</div> | ||
|
||
## Installation | ||
|
||
```bash | ||
# if you're using pnpm | ||
pnpm add @codinasion/binary-search | ||
|
||
# or, if you're using npm | ||
npm install @codinasion/binary-search | ||
|
||
# or, if you're using yarn | ||
yarn add @codinasion/binary-search | ||
``` | ||
|
||
<div align="center"> | ||
<img src="https://raw.githubusercontent.com/codinasion/codinasion/master/assets/rainbow-hr.png" alt="rainbow hr" width="100%" height="70%"> | ||
</div> | ||
|
||
## Usage | ||
|
||
```javascript | ||
// TODO: add usage examples | ||
``` | ||
|
||
<div align="center"> | ||
<img src="https://raw.githubusercontent.com/codinasion/codinasion/master/assets/rainbow-hr.png" alt="rainbow hr" width="100%" height="70%"> | ||
</div> | ||
|
||
## Sponsors & Backers | ||
|
||
![Sponsors](https://raw.githubusercontent.com/codinasion/sponsors/sponsors/sponsors.svg) | ||
|
||
<div align="center"> | ||
<img src="https://raw.githubusercontent.com/codinasion/codinasion/master/assets/rainbow-hr.png" alt="rainbow hr" width="100%" height="70%"> | ||
</div> | ||
|
||
<br/> | ||
|
||
<p align="center"> | ||
Made with 💖 by <a href="https://github.com/codinasion"><b>Codinasion</b></a> | ||
</p> | ||
|
||
<div align="center"> | ||
<img src="https://raw.githubusercontent.com/codinasion/codinasion/master/assets/rainbow-hr.png" alt="rainbow hr" width="100%" height="70%"> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.(t|j)sx?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__test__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", | ||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"], | ||
"testPathIgnorePatterns": ["dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"name": "@codinasion/binary-search", | ||
"version": "0.0.0", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Typescript implementation of binary search", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"license": "MIT", | ||
"homepage": "https://codinasion.tech", | ||
"funding": [ | ||
{ | ||
"type": "patreon", | ||
"url": "https://www.patreon.com/Codinasion" | ||
}, | ||
{ | ||
"type": "github", | ||
"url": "https://github.com/sponsors/codinasion" | ||
}, | ||
{ | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/codinasion" | ||
} | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/codinasion/codinasion.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/codinasion/codinasion/issues", | ||
"email": "codinasion@gmail.com" | ||
}, | ||
"author": { | ||
"name": "Codinasion", | ||
"email": "codinasion@gmail.com", | ||
"url": "https://github.com/codinasion" | ||
}, | ||
"keywords": [ | ||
"codinasion", | ||
"dsa", | ||
"data structures", | ||
"algorithms", | ||
"binary search" | ||
], | ||
"tsup": { | ||
"treeshake": true, | ||
"splitting": true, | ||
"entry": [ | ||
"src/index.ts" | ||
], | ||
"format": [ | ||
"cjs" | ||
], | ||
"dts": true, | ||
"minify": true, | ||
"clean": true | ||
}, | ||
"scripts": { | ||
"lint": "eslint \"**/*.ts*\"", | ||
"build": "tsup", | ||
"dev": "tsup --watch", | ||
"check-types": "tsc --noEmit", | ||
"test": "jest --config jestconfig.json --maxWorkers=50% --passWithNoTests" | ||
}, | ||
"devDependencies": { | ||
"@codinasion/eslint-config": "workspace:*", | ||
"@codinasion/typescript-config": "workspace:*", | ||
"@types/jest": "^29.5.10", | ||
"@types/node": "^20.10.0", | ||
"jest": "^29.7.0", | ||
"ts-jest": "^29.1.1", | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
npm/packages/dsa/search/binary-search/src/binary-search/binary-search.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { BinarySearch } from "./binary-search"; | ||
|
||
describe("BinarySearch", () => { | ||
it("should return the index of the first occurrence of the value in the array", () => { | ||
const arr = [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]; | ||
const val = 4; | ||
const index = BinarySearch(arr, val); | ||
expect(index).toBe(5); | ||
}); | ||
|
||
it("should return -1 if the value is not found in the array", () => { | ||
const arr = [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]; | ||
const val = 42; | ||
const index = BinarySearch(arr, val); | ||
expect(index).toBe(-1); | ||
}); | ||
|
||
it("should return -1 when given an empty array", () => { | ||
const arr: number[] = []; | ||
const val = 42; | ||
const index = BinarySearch(arr, val); | ||
expect(index).toBe(-1); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
npm/packages/dsa/search/binary-search/src/binary-search/binary-search.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Performs binary search on a sorted array to find the index of a given value. | ||
* @param arr - The sorted array to search in. | ||
* @param val - The value to search for. | ||
* @returns The index of the value in the array, or -1 if the value is not found. | ||
*/ | ||
export function BinarySearch(arr: number[], val: number): number { | ||
let left = 0; | ||
let right = arr.length - 1; | ||
while (left <= right) { | ||
const mid = Math.floor((left + right) / 2); | ||
if (arr[mid] === val) { | ||
return mid; | ||
// @ts-expect-error - We know that arr[mid] is not undefined. | ||
} else if (arr[mid] < val) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
return -1; | ||
} |
1 change: 1 addition & 0 deletions
1
npm/packages/dsa/search/binary-search/src/binary-search/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./binary-search"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./binary-search"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "@codinasion/typescript-config/base.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.