Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
node_modules
/dist
108 changes: 45 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,47 @@
# JavaScript Algorithms

Snippets for a bunch of useful JavaScript algorithms.

## Complexity - Beginner

| naiveSearch() | |
| -------------------- | -------------------------------------------------------------------------- |
| _Type_ | Loop in loop |
| _How it works_ | Compares a string with another string and returns number of matches found. |
| _Positive_ | Simple, good for small data sets. |
| _Negative_ | Inefficient for large data sets. |
| _Efficiency Average_ | O(n^2) |

| bubbleSort() | |
| -------------------- | ------------------------------------------- |
| _Type_ | Loop in loop |
| _How it works_ | Swapping, sorting from smallest to largest. |
| _Positive_ | Simple, good for small data sets. |
| _Negative_ | Inefficient for large data sets. |
| _Efficiency Average_ | O(n^2) |

| selectionSort() | |
| -------------------- | ------------------------------------------- |
| _Type_ | Loop in loop |
| _How it works_ | Swapping, sorting from largest to smallest. |
| _Positive_ | Simple, good for small data sets. |
| _Negative_ | Inefficient for large data sets. |
| _Efficiency Average_ | O(n^2) |

| insertionSort() | |
| -------------------- | ------------------------------------------------------------------------------------------------- |
| _Type_ | Loop in loop |
| _How it works_ | Sorting into new array. |
| _Positive_ | Simple, good for small data sets and nearly sorted data, or when new data is addded continuously. |
| _Negative_ | Inefficient for large data sets. |
| _Efficiency Average_ | O(n^2) |

## Complexity - Intermediate

| mergeSort() | |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| _Type_ | Recursive with loop |
| _How it works_ | Splits arrays until each array has only one item (= is sorted). Then merge and sort each array until there's only one array left. |
| _Positive_ | More complex, good for large data sets |
| _Negative_ | |
| _Efficiency Average_ | O(n log n) |

| quickSort() | |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| _Type_ | Recursive with loop |
| _How it works_ | Sets pivot on the mean value, puts smaller numbers left of pivot and larger right of pivot. Then sorts left half and right half of increasingly smaller sizes, until its done. |
| _Positive_ | |
| _Negative_ | With too large data sets you might run out of call stacks. |
| _Efficiency Average_ | O(n log n) |

## Complexity - Expert

| radixSort() | |
| -------------------- | --------------------------------------------------------------------------------------------------------- |
| _Type_ | Loop in loop |
| _How it works_ | Sorts an array of integers by loop-sorting them in buckets by number and then putting them back together. |
| _Positive_ | Fast. |
| _Negative_ | Only works on integers. |
| _Efficiency Average_ | O(n k) |
Library for a bunch of useful JavaScript algorithms.

## Installation

`npm install javascript-algorithms`

## Content

### Sorters

The sorters package contains a number of different kind of sorters which have different benefits and strengths. You can read more about them in detail [here]().

### Number Generators

Number generators can be used for creating different kind of numbers and number series.

### Data Structures

JavaScript doesn't inherently support certain types of Lists that exist in other languages. These classes will allow you to use List types such as _Singly Linked List_, _Doubly Linked List_, _Stacks_ and _Queues_.

## Example (Sorters)

```typescript
import { sorters } from "javascript-algorithms";

const arrayToSort = [1, 6, 2, 5, 9, 10, 11, 3, 4, 12];

sorters.selectionSorts.quicksort_inPlace(arrayToSort);

console.log(arrayToSort); // [1, 2, 3, 4, 5, 6, 9, 10, 11, 12]
```

Note: Some sorters sorts in-place, meaning they will mutate the original array. If you want to keep the original array untouched, you need to first make a copy.

```typescript
import { sorters } from "javascript-algorithms";

const unsortedArray = [1, 6, 2, 5, 9, 10, 11, 3, 4, 12];

const arrayToSort = [...unsortedArray];

sorters.selectionSorts.quicksort_inPlace(arrayToSort);

console.log(arrayToSort); // [1, 2, 3, 4, 5, 6, 9, 10, 11, 12]
```
235 changes: 0 additions & 235 deletions index.js

This file was deleted.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "javascript-algoritms",
"name": "javascript-algorithms",
"version": "1.0.0",
"description": "Snippets for a bunch of useful JavaScript algorithms.",
"main": "index.js",
"description": "A collection of sorting-algorithms",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"type": "module",
"scripts": {
"test": "jest --watch"
Expand Down
Loading