Skip to content

Commit db82f72

Browse files
committed
first
0 parents  commit db82f72

File tree

101 files changed

+4172
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+4172
-0
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
7+
[*.{js,ts,json}]
8+
charset = utf-8
9+
indent_style = space
10+
indent_size = 2
11+
12+
13+
[*.{md,txt}]
14+
indent_size = 2
15+
indent_style = space
16+
trim_trailing_whitespace = false

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data/dictionary

README.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Algorithms and Data Structures
2+
3+
WIP
4+
5+
Beautifully crafted and well documented examples of Algorithms and Data Structures.
6+
7+
## Algorithms
8+
9+
[Circular Array](./algorithms/circular-array/index.ts)
10+
11+
[Damerau Levenshtein Distance](./algorithms/damerau-levenshtein-distance/index.ts)
12+
13+
[Euler's Totient](./algorithms/eulers-totient/index.ts)
14+
15+
[Fibonacci Numbers](./algorithms/fibonacci-numbers/index.ts)
16+
17+
[Fisher–Yates Shuffle](./algorithms/fisher-yates-shuffle/index.ts)
18+
19+
[Levenshtein Distance](./algorithms/levenshtein-distance/index.ts)
20+
21+
[Linear Congruential Generator](./algorithms/linear-congruential-generator/index.ts)
22+
23+
[Lucas Numbers](./algorithms/lucas-numbers/index.ts)
24+
25+
[Mersenne Twister](./algorithms/mersenne-twister/index.ts)
26+
27+
[Phi](./algorithms/phi/index.ts)
28+
29+
[Sieve of Eratosthenes](./algorithms/sieve-of-eratosthenes/index.ts)
30+
31+
### Search Algorithms
32+
33+
[Binary Search](./algorithms/binary-search/index.ts)
34+
35+
[Exponential Search](./algorithms/exponential-search/index.ts)
36+
37+
[Interpolation Search](./algorithms/interpolation-search/index.ts)
38+
39+
[Linear Search](./algorithms/linear-search/index.ts)
40+
41+
[Ternary Search](./algorithms/ternary-search/index.ts)
42+
43+
### Hash Algorithms
44+
45+
[djb2](./algorithms/hash/djb2/index.ts)
46+
47+
[sdbm](./algorithms/hash/sdbm/index.ts)
48+
49+
[loselose](./algorithms/hash/loselose/index.ts)
50+
51+
### Sort Algorithms
52+
53+
[Bubble Sort](./algorithms/sorting/bubble-sort/index.ts)
54+
55+
[Comb Sort](./algorithms/sorting/comb-sort/index.ts)
56+
57+
[Heapsort](./algorithms/sorting/heapsort/index.ts)
58+
59+
[Insertion Sort](./algorithms/sorting/insertion-sort/index.ts)
60+
61+
[Merge Sort](./algorithms/sorting/merge-sort/index.ts)
62+
63+
[Quicksort](./algorithms/sorting/quicksort/index.ts)
64+
65+
[Selection Sort](./algorithms/sorting/selection-sort/index.ts)
66+
67+
[Shellsort](./algorithms/sorting/shellsort/index.ts)
68+
69+
## Data Structures
70+
71+
[AVL Tree](./data-structures/avl-tree/index.ts)
72+
73+
[Binary Search Tree](./data-structures/binary-search-tree/index.ts)
74+
75+
[Binary Tree](./data-structures/binary-tree/index.ts)
76+
77+
[Circular Doubly Linked List](./data-structures/circular-doubly-linked-list/index.ts)
78+
79+
[Circular Linked List](./data-structures/circular-linked-list/index.ts)
80+
81+
[Deque](./data-structures/deque/index.ts)
82+
83+
[Dictionary](./data-structures/dictionary/index.ts)
84+
85+
[Doubly Linked List](./data-structures/doubly-linked-list/index.ts)
86+
87+
[Graph](./data-structures/graph/index.ts)
88+
89+
[Hash Table](./data-structures/hash-table/index.ts)
90+
91+
[Linked List](./data-structures/linked-list/index.ts)
92+
93+
[Queue](./data-structures/queue/index.ts)
94+
95+
[Red Black Tree](./data-structures/red-black-tree/index.ts)
96+
97+
[Skip List](./data-structures/skip-list/index.ts)
98+
99+
[Stack](./data-structures/stack/index.ts)
100+
101+
[Tree](./data-structures/tree/index.ts)
102+
103+
[Trie](./data-structures/trie/index.ts)
104+
105+
[Unrolled Linked List](./data-structures/unrolled-linked-list/index.ts)
106+
107+
## Licence
108+
109+
MIT

algorithms/binary-search/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Binary Search
2+
3+
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_algorithm)
4+
5+
<table>
6+
<tbody align="left">
7+
<tr>
8+
<th colspan="3">Time Complexity</th>
9+
<th>Space Complexity</th>
10+
</tr>
11+
<tr>
12+
<th>Average</th>
13+
<th>Best</th>
14+
<th>Worst</th>
15+
<th>Worst</th>
16+
</tr>
17+
<tr>
18+
<td><code class="yellow-green">Θ(log(n))</code></td>
19+
<td><code class="green">O(1)</code></td>
20+
<td><code class="yellow-green">Θ(log(n))</code></td>
21+
<td><code class="green">O(1)</code></td>
22+
</tr>
23+
</tbody>
24+
</table>

algorithms/binary-search/index.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export function binarySearch(
2+
haystack: number[],
3+
needle: number,
4+
left: number = 0,
5+
right: number = haystack.length - 1
6+
): number | undefined {
7+
while (left <= right) {
8+
const middle = Math.floor((left + right) / 2);
9+
if (haystack[middle] < needle) {
10+
left = middle + 1;
11+
} else if (haystack[middle] > needle) {
12+
right = middle - 1;
13+
} else {
14+
return middle;
15+
}
16+
}
17+
}
18+
19+
export function binarySearchLeftmost(
20+
haystack: number[],
21+
needle: number,
22+
left: number = 0,
23+
right: number = haystack.length
24+
) {
25+
while (left < right) {
26+
const middle = Math.floor((left + right) / 2);
27+
if (haystack[middle] < needle) {
28+
left = middle + 1;
29+
} else {
30+
right = middle;
31+
}
32+
}
33+
return left;
34+
}
35+
36+
export function binarySearchRightmost(
37+
haystack: number[],
38+
needle: number,
39+
left: number = 0,
40+
right: number = haystack.length
41+
) {
42+
while (left < right) {
43+
const middle = Math.floor((left + right) / 2);
44+
if (haystack[middle] > needle) {
45+
right = middle;
46+
} else {
47+
left = middle + 1;
48+
}
49+
}
50+
return right - 1;
51+
}
52+
53+
// const array = [3, 3];
54+
// console.log(binarySearchLeftmost(array, 3));
55+
// console.log(binarySearchRightmost(array, 3));

algorithms/binary-search/test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
binarySearch,
3+
binarySearchLeftmost,
4+
binarySearchRightmost,
5+
} from "./index.ts";
6+
7+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
8+
9+
Deno.test("Binary search odd sized array", () => {
10+
const array = [0, 1, 2];
11+
assertEquals(binarySearch(array, 1), 1);
12+
assertEquals(binarySearch(array, 3), undefined);
13+
});
14+
15+
Deno.test("Binary search even sized array", () => {
16+
const array = [0, 1];
17+
assertEquals(binarySearch(array, 1), 1);
18+
assertEquals(binarySearch(array, 2), undefined);
19+
});
20+
21+
Deno.test("Binary search empty array returns undefined", () => {
22+
assertEquals(binarySearch([], 1), undefined);
23+
});
24+
25+
Deno.test("Binary search duplicates", () => {
26+
const array = [3, 3];
27+
assertEquals(binarySearchLeftmost(array, 3), 0);
28+
assertEquals(binarySearchRightmost(array, 3), 1);
29+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Damerau Levenshtein Distance
2+
3+
In information theory and computer science, the Damerau–Levenshtein distance (named after Frederick J. Damerau and Vladimir I. Levenshtein) is a string metric for measuring the edit distance between two sequences. Informally, the Damerau–Levenshtein distance between two words is the minimum number of operations (consisting of insertions, deletions or substitutions of a single character, or transposition of two adjacent characters) required to change one word into the other.
4+
5+
The Damerau–Levenshtein distance differs from the classical Levenshtein distance by including transpositions among its allowable operations in addition to the three classical single-character edit operations (insertions, deletions and substitutions).
6+
7+
In his seminal paper, Damerau stated that more than 80% of all human misspellings can be expressed by a single error of one of the four types. Damerau's paper considered only misspellings that could be corrected with at most one edit operation. While the original motivation was to measure distance between human misspellings to improve applications such as spell checkers, Damerau–Levenshtein distance has also seen uses in biology to measure the variation between protein sequences. [Wikipedia](https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
2+
3+
export function optimalStringAlignmentDistance(a: string, b: string) {
4+
let i;
5+
let j;
6+
7+
const d: number[][] = [];
8+
9+
for (i = 0; i <= a.length; i++) {
10+
d[i] = [i];
11+
}
12+
13+
for (j = 0; j <= b.length; j++) {
14+
d[0][j] = j;
15+
}
16+
17+
for (i = 1; i <= a.length; i++) {
18+
for (j = 1; j <= b.length; j++) {
19+
d[i][j] = Math.min(
20+
d[i - 1][j] + 1, // deletion
21+
d[i][j - 1] + 1, // insertion
22+
d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1) // substitution
23+
);
24+
if (i > 1 && j > 1 && a[i] === b[j - 1] && a[i - 1] === b[j]) {
25+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
26+
} // transposition
27+
}
28+
}
29+
30+
return d[a.length][b.length];
31+
}
32+
33+
export function damerauLevenshteinDistance(a: string, b: string) {
34+
let i: number;
35+
let j: number;
36+
37+
const da: { [key: string]: number } = {};
38+
39+
a = a.toUpperCase();
40+
b = b.toUpperCase();
41+
42+
for (const letter of a + b) {
43+
if (!da[letter]) {
44+
da[letter] = 0;
45+
}
46+
}
47+
48+
const d: number[][] = new Array(a.length);
49+
50+
for (i = -1; i <= a.length; i++) {
51+
d[i] = new Array(b.length);
52+
for (j = -1; j <= b.length; j++) {
53+
d[i][j] = 0;
54+
}
55+
}
56+
57+
const INFINITY = a.length + b.length;
58+
d[-1][-1] = INFINITY;
59+
60+
for (i = 0; i < a.length; i++) {
61+
d[i][-1] = INFINITY;
62+
d[i][0] = i;
63+
}
64+
65+
for (j = 0; j < b.length; j++) {
66+
d[-1][j] = INFINITY;
67+
d[0][j] = j;
68+
}
69+
70+
for (i = 1; i <= a.length; i++) {
71+
let db = 0;
72+
for (j = 1; j <= b.length; j++) {
73+
const k: number = da[b[j - 1]];
74+
const l: number = db;
75+
76+
let cost;
77+
78+
if (a[i - 1] === b[j - 1]) {
79+
db = j;
80+
cost = 0;
81+
} else {
82+
cost = 1;
83+
}
84+
85+
d[i][j] = Math.min(
86+
d[i - 1][j - 1] + cost, // substitution
87+
d[i][j - 1] + 1, // insertion
88+
d[i - 1][j] + 1, // deletion
89+
d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1) // transposition
90+
);
91+
}
92+
93+
da[a[i - 1]] = i;
94+
}
95+
96+
// console.log(d);
97+
98+
// console.log(da);
99+
100+
return d[a.length][b.length];
101+
}
102+
103+
// 3
104+
console.log(optimalStringAlignmentDistance('ca', 'abc'));
105+
106+
// 2
107+
console.log(damerauLevenshteinDistance('ca', 'abc'));
108+
109+
// 2
110+
console.log(damerauLevenshteinDistance('a cat', 'a abct'));
111+
112+
// 1
113+
console.log(damerauLevenshteinDistance('smtih', 'smith'));
114+
115+
// 1
116+
console.log(damerauLevenshteinDistance('cat', 'cta'));

algorithms/eulers-totient/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Euler's Totient
2+
3+
In number theory, Euler's totient function counts the positive integers up to a given integer n that are relatively prime to n. It is written using the Greek letter phi as φ(n) or ϕ(n), and may also be called Euler's phi function. In other words, it is the number of integers k in the range 1 ≤ k ≤ n for which the greatest common divisor gcd(n, k) is equal to 1. The integers k of this form are sometimes referred to as totatives of n.
4+
5+
<img src="https://render.githubusercontent.com/render/math?math=\phi(n)">

algorithms/eulers-totient/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function gcd(a: number, b: number): number {
2+
if (a === 0) {
3+
return b;
4+
}
5+
6+
return gcd(b % a, a);
7+
}
8+
9+
export function eulersTotient(n: number) {
10+
let totient = 1;
11+
12+
for (let i = 2; i < n; i++) {
13+
if (gcd(i, n) === 1) {
14+
totient++;
15+
}
16+
}
17+
18+
return totient;
19+
}
20+
21+
for (let i = 1; i <= 10; i++) {
22+
console.log(eulersTotient(i));
23+
}

0 commit comments

Comments
 (0)