Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Add @codinasion/binary-search
Browse files Browse the repository at this point in the history
  • Loading branch information
harshraj8843 committed Dec 1, 2023
1 parent a360d99 commit ae5fc0d
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-buttons-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codinasion/binary-search": major
---

Initial Release
12 changes: 12 additions & 0 deletions npm/packages/dsa/search/binary-search/.eslintrc.js
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,
},
};
21 changes: 21 additions & 0 deletions npm/packages/dsa/search/binary-search/LICENSE.md
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.
54 changes: 54 additions & 0 deletions npm/packages/dsa/search/binary-search/README.md
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>
8 changes: 8 additions & 0 deletions npm/packages/dsa/search/binary-search/jestconfig.json
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"]
}
79 changes: 79 additions & 0 deletions npm/packages/dsa/search/binary-search/package.json
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"
}
}
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);
});
});
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./binary-search";
1 change: 1 addition & 0 deletions npm/packages/dsa/search/binary-search/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./binary-search";
5 changes: 5 additions & 0 deletions npm/packages/dsa/search/binary-search/tsconfig.json
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"]
}
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ae5fc0d

Please sign in to comment.