Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 190 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,228 @@
# TypeScript Algorithms and Data Structures
# 🚀 TypeScript Algorithms and Data Structures

This repository is a collection of TypeScript algorithms and data structures. It is intended for educational purposes and serves as a reference for developers who want to learn and practice TypeScript.
**A comprehensive collection of algorithms, data structures, and coding challenges implemented in TypeScript**

![GitHub top language](https://img.shields.io/github/languages/top/behzadam/algorithms)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b6d0142c6cb448e28ea0dcc88d77b062)](https://app.codacy.com/gh/behzadam/algorithms/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
![Lines of code](https://img.shields.io/tokei/lines/github/behzadam/algorithms)
[![GitHub top language](https://img.shields.io/github/languages/top/behzadam/algorithms?style=for-the-badge)](https://github.com/behzadam/algorithms)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b6d0142c6cb448e28ea0dcc88b77b062?style=for-the-badge)](https://app.codacy.com/gh/behzadam/algorithms/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![Lines of code](https://img.shields.io/tokei/lines/github/behzadam/algorithms?style=for-the-badge)](https://github.com/behzadam/algorithms)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge)](https://opensource.org/licenses/MIT)

## Introduction
[Features](#-features) • [Quick Start](#-getting-started) • [Documentation](#-documentation)

It includes code and patterns from the "JavaScript Algorithms" repository, translated from JavaScript to TypeScript. [JavaScript Algorithms](https://github.com/trekhleb/javascript-algorithms)
---

## Data structure
## 📋 Table of Contents

- [HashTable](src/data-structure/hash-table/hash-table.ts)
- [MinHeap](src/data-structure/heap/min-heap.ts)
- [MaxHeap](src/data-structure/heap/max-heap.ts)
- [LinkedList](src/data-structure/linked-list/linked-list.ts)
- [Queue](src/data-structure/queue/queue.ts)
- [Stack](src/data-structure/stack/stack.ts)
- [MaxPriorityQueue](src/data-structure/priority-queue/max-priority-queue.ts)
- [MinPriorityQueue](src/data-structure/priority-queue/min-priority-queue.ts)
- [Trie](src/data-structure/trie/trie.ts)
<!-- markdownlint-disable MD033 -->
<details>
<summary>Click to expand</summary>
<!-- markdownlint-enable MD033 -->

## Algorithms
- [🚀 TypeScript Algorithms and Data Structures](#-typescript-algorithms-and-data-structures)
- [📋 Table of Contents](#-table-of-contents)
- [🎯 Introduction](#-introduction)
- [✨ Features](#-features)
- [📚 Documentation](#-documentation)
- [📦 Data Structures](#-data-structures)
- [🔍 Algorithms](#-algorithms)
- [🔎 Search Algorithms](#-search-algorithms)
- [🔄 Sorting Algorithms](#-sorting-algorithms)
- [💻 Coding Challenges](#-coding-challenges)
- [👉 Two Pointers](#-two-pointers)
- [🛠️ Utilities](#️-utilities)
- [🚀 Getting Started](#-getting-started)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [💻 Development](#-development)
- [Available Scripts](#available-scripts)
- [Project Structure](#project-structure)
- [📝 License](#-license)
- [📧 Contact](#-contact)

### Search
<!-- markdownlint-disable MD033 -->
</details>
<!-- markdownlint-enable MD033 -->

- [BinarySearch](src/algorithms/search/binary-search/binary-search.ts)
- [LinearSearch](src/algorithms/search/linear-search/linear-search.ts)
---

### Sort
## 🎯 Introduction

- [QuickSort](src/algorithms/sort/quick-sort/quick-sort.ts)
- [BubbleSort](src/algorithms/sort/bubble-sort/bubble-sort.ts)
- [BubbleSortSimple](src/algorithms/sort/bubble-sort/bubble-sort-simple.ts)
This repository is a **comprehensive collection** of TypeScript algorithms and data structures designed for educational purposes. It serves as a reference for developers who want to learn, practice, and master TypeScript while understanding fundamental computer science concepts.

## Utils
> 💡 **Note:** This repository includes code and patterns from the [JavaScript Algorithms](https://github.com/trekhleb/javascript-algorithms) repository, translated and enhanced from JavaScript to TypeScript.

- [Comparator](src/ds/comparator/comparator.ts)
---

Please note that the repository is still a work in progress, and new algorithms and patterns will be added over time.
## ✨ Features

## Getting Started
- 🔍 **Well-documented** code with TypeScript types
- ✅ **Fully tested** with Jest test suites
- 📚 **Educational** focus with clear implementations
- 🎨 **Clean code** following best practices
- 🚀 **Production-ready** implementations
- 🔄 **Regularly updated** with new algorithms and patterns

To get started with the repository, follow these simple steps:
---

Clone the repository to your local machine using the command:
## 📚 Documentation

```bash
git clone https://github.com/behzadam/algorithms.git
```
### 📦 Data Structures

Install the required dependencies by running the following command:
| Data Structure | Description | Implementation |
| -------------------- | ------------------------------------ | -------------------------------------------------------------------- |
| **HashTable** | Key-value mapping with hash function | [View Code](src/data-structure/hash-table/hash-table.ts) |
| **MinHeap** | Binary heap with minimum root | [View Code](src/data-structure/heap/min-heap.ts) |
| **MaxHeap** | Binary heap with maximum root | [View Code](src/data-structure/heap/max-heap.ts) |
| **LinkedList** | Linear collection of nodes | [View Code](src/data-structure/linked-list/linked-list.ts) |
| **Queue** | FIFO (First In First Out) structure | [View Code](src/data-structure/queue/queue.ts) |
| **Stack** | LIFO (Last In First Out) structure | [View Code](src/data-structure/stack/stack.ts) |
| **MaxPriorityQueue** | Priority queue with max priority | [View Code](src/data-structure/priority-queue/max-priority-queue.ts) |
| **MinPriorityQueue** | Priority queue with min priority | [View Code](src/data-structure/priority-queue/min-priority-queue.ts) |
| **Trie** | Prefix tree for string operations | [View Code](src/data-structure/trie/trie.ts) |
| **BinaryTree** | Hierarchical tree structure | [View Code](src/data-structure/tree/binary-tree.ts) |

```bash
pnpm install
```
### 🔍 Algorithms

#### 🔎 Search Algorithms

- **[Binary Search](src/algorithms/search/binary-search/binary-search.ts)** - Efficient search in sorted arrays
- **[Linear Search](src/algorithms/search/linear-search/linear-search.ts)** - Simple sequential search

#### 🔄 Sorting Algorithms

- **[Quick Sort](src/algorithms/sort/quick/quick-sort.ts)** - Divide and conquer sorting algorithm
- **[Bubble Sort](src/algorithms/sort/bubble-sort/bubble-sort.ts)** - Simple comparison-based sort
- **[Bubble Sort Simple](src/algorithms/sort/bubble-sort/bubble-sort-simple.ts)** - Simplified bubble sort implementation

### 💻 Coding Challenges

#### 👉 Two Pointers

- **[Pair Sum Sorted](src/code-challanges/two-pointers/pair-sum-sorted.ts)** - Find pairs in sorted array that sum to target

### 🛠️ Utilities

- **[Comparator](src/utils/comparator.ts)** - Comparison functions for sorting and searching
- **[Range](src/utils/range.ts)** - Utility for generating number ranges
- **[Type Definitions](src/types/)** - Common TypeScript type definitions

---

## 🚀 Getting Started

### Prerequisites

- **Node.js** (v16 or higher)
- **pnpm** (or npm/yarn)

### Installation

1. **Clone the repository**

```bash
git clone https://github.com/behzadam/algorithms.git
cd algorithms
```

2. **Install dependencies**

```bash
pnpm install
```

3. **Run tests**

```bash
pnpm test
```

Run all tests
---

## 💻 Development

### Available Scripts

<!-- markdownlint-disable MD033 -->
<details>
<summary><b>Test Commands</b></summary>
<!-- markdownlint-enable MD033 -->

```bash
# Run all tests
pnpm test
```

Format checking
# Run tests in watch mode
pnpm test:watch

```bash
pnpm format:check
# Run tests with coverage
pnpm test:coverage
```

Format fixing
<!-- markdownlint-disable MD033 -->
</details>

<details>
<summary><b>Code Quality Commands</b></summary>
<!-- markdownlint-enable MD033 -->

```bash
pnpm format:fix
```
# Check code formatting
pnpm format:check

Run ESLint
# Fix code formatting
pnpm format:fix

```bash
# Run ESLint
pnpm lint

# Fix ESLint issues
pnpm lint:fix
```

<!-- markdownlint-disable MD033 -->
</details>
<!-- markdownlint-enable MD033 -->

### Project Structure

```text
algorithms/
├── src/
│ ├── algorithms/ # Algorithm implementations
│ │ ├── search/ # Search algorithms
│ │ └── sort/ # Sorting algorithms
│ ├── code-challanges/ # Coding challenges
│ ├── data-structure/ # Data structure implementations
│ ├── types/ # TypeScript type definitions
│ └── utils/ # Utility functions
├── jest.config.js # Jest configuration
├── tsconfig.json # TypeScript configuration
└── package.json # Project dependencies
```

## License
---

## 📝 License

This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.

---

## 📧 Contact

Have questions or suggestions? Feel free to reach out!

- **Email:** [[email protected]](mailto:[email protected])
- **GitHub:** [@behzadam](https://github.com/behzadam)

---

<!-- markdownlint-disable MD033 -->
<div align="center">

The "TypeScript Algorithms, Data Structures and Patterns" repository is licensed under the [MIT License](https://opensource.org/licenses/MIT). Feel free to use, modify, and distribute the code as per the terms of the license.
**⭐ Star this repo if you find it helpful!**

## Contact
Made with ❤️ by [behzadam](https://github.com/behzadam)

If you have any questions or suggestions regarding the repository, please feel free to [contact me](mailto:[email protected]).
</div>
<!-- markdownlint-enable MD033 -->
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pairSumSortedBruteForce } from "./pair-sum";
import { pairSumSorted } from "./pair-sum-sorted";

describe("pairSumSortedBruteForce", () => {
describe("pairSumSorted", () => {
it.each([
{
input: [],
Expand All @@ -25,7 +25,7 @@ describe("pairSumSortedBruteForce", () => {
])(
"returns $expected for input $input and target $target",
({ input, target, expected }) => {
expect(pairSumSortedBruteForce(input, target)).toEqual(expected);
expect(pairSumSorted(input, target)).toEqual(expected);
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* The time complexity of this solution is O(n) because we are using two pointers to traverse the array.
* The space complexity is O(1) because we are not using any extra space.
*/
function pairSumSortedBruteForce(arr: number[], target: number) {
export function pairSumSorted(arr: number[], target: number) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
Expand All @@ -32,5 +32,3 @@ function pairSumSortedBruteForce(arr: number[], target: number) {
}
return [];
}

export { pairSumSortedBruteForce };