# How the JavaScript Algorithms Repository Structures Implementations by Paradigm

> Explore how the trekhleb javascript-algorithms repository structures implementations by paradigm like sorting graph dynamic-programming and greedy ensuring organized and accessible code.

- Repository: [Oleksii Trekhleb/javascript-algorithms](https://github.com/trekhleb/javascript-algorithms)
- Tags: internals
- Published: 2026-02-24

---

**The `trekhleb/javascript-algorithms` repository organizes code under `src/algorithms` using subdirectories named after computational paradigms—such as `sorting`, `graph`, `dynamic-programming`, and `greedy`—with each algorithm residing in its own folder containing the implementation, README, and tests.**

When exploring the `trekhleb/javascript-algorithms` repository, developers quickly notice that the codebase does not dump every function into a single directory. Instead, the project uses a **paradigm-first hierarchy** to structure algorithm implementations by paradigm, making it straightforward to locate specific techniques—from classic sorting routines to advanced dynamic programming solutions. This article examines exactly how the repository organizes these implementations and highlights the key directories and files you need to know.

## Paradigm-Based Directory Layout

The repository root contains a `src/algorithms` folder that acts as the primary container. Inside, **each paradigm receives its own directory**, and within that directory, every algorithm gets a dedicated subfolder. This three-level nesting—`src/algorithms/<paradigm>/<algorithm-name>/`—ensures that related techniques stay grouped while individual implementations remain isolated and testable.

### Core Paradigms Covered

The repository explicitly names folders after well-established algorithmic paradigms and problem domains:

- **`sorting`** – Divide-and-conquer and comparison-based sorts (quick sort, merge sort, heap sort, counting sort, radix sort).
- **`search`** – Linear and logarithmic search techniques (binary search, jump search, interpolation search).
- **`graph`** – Pathfinding and spanning-tree algorithms (Dijkstra, Bellman-Ford, Floyd-Warshall, Kruskal, topological sort).
- **`math`** – Number-theoretic utilities (Sieve of Eratosthenes, prime factorization, Newton’s method for square roots).
- **`string`** – Text processing algorithms (Rabin-Karp, anagram detection, palindrome checks).
- **`tree`** – Binary tree traversals and trie operations.
- **`dynamic-programming`** – Memoized and tabular solutions (located in `uncategorized` for problem-specific instances).
- **`greedy`** – Optimization strategies that make locally optimal choices (often found in `uncategorized`).
- **`uncategorized`** – Algorithms that solve specific competitive-programming problems rather than generic reusable utilities (e.g., Tower of Hanoi, unique paths, best time to buy/sell stocks).

### File Organization Within Each Algorithm

Every algorithm subfolder follows a consistent, predictable structure:

```

src/algorithms/<paradigm>/<algorithm-name>/
├── <algorithmName>.js          # Main implementation (ES6 module)

├── README.md                     # Complexity analysis and usage guide

└── __test__/
    └── <algorithmName>.test.js   # Jest unit tests

```

This convention means that once you locate the paradigm folder, you can immediately dive into the source code, read the documentation, or run the tests without hunting through unrelated files.

## Navigating Key Paradigm Folders

Understanding the location of specific paradigms helps you import the right module for your project. Below are the critical paths and representative files for the most commonly used categories.

### Sorting Algorithms

Path: `src/algorithms/sorting/`

This directory houses comparison-based and non-comparison sorts. The `quick-sort` subfolder contains the classic divide-and-conquer implementation.

**Key file:** [`src/algorithms/sorting/quick-sort/quickSort.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/src/algorithms/sorting/quick-sort/quickSort.js)

```js
import quickSort from './src/algorithms/sorting/quick-sort/quickSort';

const unsorted = [9, 3, 5, 2, 8, 1];
const sorted = quickSort(unsorted);

console.log(sorted); // → [1, 2, 3, 5, 8, 9]

```

### Search Algorithms

Path: `src/algorithms/search/`

Contains linear and logarithmic search strategies. The `binary-search` folder provides the O(log n) implementation for sorted arrays.

**Key file:** [`src/algorithms/search/binary-search/binarySearch.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/src/algorithms/search/binary-search/binarySearch.js)

```js
import binarySearch from './src/algorithms/search/binary-search/binarySearch';

const sortedArray = [1, 3, 5, 7, 9, 11];
const target = 7;
const index = binarySearch(sortedArray, target);

console.log(`Found ${target} at index ${index}`); // → Found 7 at index 3

```

### Graph Algorithms

Path: `src/algorithms/graph/`

Holds shortest-path, spanning-tree, and traversal algorithms. The `dijkstra` folder implements the greedy shortest-path algorithm for weighted graphs.

**Key file:** [`src/algorithms/graph/dijkstra/dijkstra.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/src/algorithms/graph/dijkstra/dijkstra.js)

```js
import dijkstra from './src/algorithms/graph/dijkstra/dijkstra';
import { Graph } from './src/data-structures/graph/Graph';

const graph = new Graph(true); // directed weighted graph
// add vertices and weighted edges …
const distances = dijkstra(graph, 'A'); // shortest distances from A

console.log(distances);

```

### Math Algorithms

Path: `src/algorithms/math/`

Contains number-theoretic utilities. The `sieve-of-eratosthenes` folder provides the prime generation algorithm.

**Key file:** [`src/algorithms/math/sieve-of-eratosthenes/sieveOfEratosthenes.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/src/algorithms/math/sieve-of-eratosthenes/sieveOfEratosthenes.js)

```js
import sieveOfEratosthenes from './src/algorithms/math/sieve-of-eratosthenes/sieveOfEratosthenes';

const primesUpTo50 = sieveOfEratosthenes(50);
console.log(primesUpTo50); // → [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

```

### Dynamic Programming and Greedy Techniques

Path: `src/algorithms/uncategorized/`

While the repository has dedicated folders for many paradigms, **dynamic programming** and **greedy** solutions often appear in the `uncategorized` directory when they solve specific problem instances rather than providing generic reusable utilities.

**Key files:**
- [`src/algorithms/uncategorized/unique-paths/dpUniquePaths.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/src/algorithms/uncategorized/unique-paths/dpUniquePaths.js) – Dynamic programming solution for grid path counting.
- [`src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dpBestTimeToBuySellStocks.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dpBestTimeToBuySellStocks.js) – Greedy/dynamic programming hybrid for stock trading optimization.

## Summary

- The repository employs a **paradigm-first directory hierarchy** under `src/algorithms/`, grouping techniques by computational strategy (sorting, graph, math, etc.).
- Each algorithm lives in its own subfolder containing an ES6 implementation file, a [`README.md`](https://github.com/trekhleb/javascript-algorithms/blob/main/README.md) with complexity analysis, and a `__test__/` directory for Jest unit tests.
- **Dynamic programming** and **greedy** algorithms often reside in the `uncategorized` folder when they target specific problem constraints, whereas classic paradigms like sorting and graph traversal have dedicated top-level directories.
- Import paths follow the predictable pattern: `src/algorithms/<paradigm>/<algorithm-name>/<file>.js`.

## Frequently Asked Questions

### How do I locate a specific algorithm implementation in the repository?

Navigate to `src/algorithms/` and identify the paradigm folder that matches the technique you need—such as `sorting` for quick sort or `graph` for Dijkstra’s algorithm. Inside that folder, each algorithm has its own subdirectory named after the technique (e.g., `quick-sort`), where you will find the main JavaScript file, documentation, and test suite.

### Why are dynamic programming and greedy algorithms placed in the `uncategorized` folder?

The `uncategorized` directory houses algorithms that solve specific competitive-programming or puzzle-style problems rather than generic reusable utilities. Many dynamic programming implementations—such as [`dpUniquePaths.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/dpUniquePaths.js) for grid traversal and [`dpBestTimeToBuySellStocks.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/dpBestTimeToBuySellStocks.js) for optimization—fall into this category because they demonstrate a technique applied to a particular scenario rather than providing a general-purpose library function.

### What is the standard file structure inside an algorithm folder?

Every algorithm subfolder follows a consistent three-component convention: an implementation file written as an ES6 module (e.g., [`quickSort.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/quickSort.js)), a [`README.md`](https://github.com/trekhleb/javascript-algorithms/blob/main/README.md) that explains the algorithm’s logic, time/space complexity, and usage examples, and a `__test__/` directory containing Jest test files (e.g., [`quickSort.test.js`](https://github.com/trekhleb/javascript-algorithms/blob/main/quickSort.test.js)). This layout ensures that each technique is self-contained, documented, and fully tested.

### Can I import these algorithms directly into my own JavaScript project?

Yes. Because the repository uses standard ES6 modules, you can import any algorithm using a relative or absolute path to the specific implementation file. For example, you can write `import quickSort from './src/algorithms/sorting/quick-sort/quickSort.js';` in your own codebase. Ensure your build tool (Webpack, Rollup, or Node.js with `"type": "module"`) is configured to resolve these paths correctly.