Skip to content

Commit cce3160

Browse files
merge
2 parents 60e7557 + 2651723 commit cce3160

File tree

11 files changed

+272
-3
lines changed

11 files changed

+272
-3
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
3. [Сортировка подсчётом / Counting Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/counting-sort/counting-sort.js) | [Инфо](https://www.youtube.com/watch?v=6dk_csyWif0)
99
4. [Сортировка чёт-нечет / OddEven Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/odd-even-sort/odd-even-sort.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D1%87%D1%91%D1%82-%D0%BD%D0%B5%D1%87%D0%B5%D1%82)
1010
5. [Быстрая сортировка / Quick Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/quick-sort/quick-sort.js) | [Инфо](https://habrahabr.ru/sandbox/29775/)
11-
6. [Поразрядная сортировка / Radix Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/radix-sort/radix-sort.js) | [Инфо](http://trubetskoy1.narod.ru/alg/radixsort.html)
11+
6. [Сортировка кучей / Heap Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/heap/binary-heap/binary-heap.js) | [Инфо](https://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%BA%D1%83%D1%87%D0%B5%D0%B9)
12+
7. [Гномья сортировка / Gnome sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/gnome-sort/gnome-sort.js) | [Инфо](http://sorting.valemak.com/gnome/)
13+
8. [Соломонова Сортировка / Solomon sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/solomon-sort/solomon-sort.js) | [Инфо](http://sorting.valemak.com/solomon/)
14+
9. [Бисерная сортировка / Bead Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/bead-sort/bead-sort.js) | [Инфо](http://sorting.valemak.com/bead/)
15+
10. [Поразрядная сортировка / Radix Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/radix-sort/radix-sort.js) | [Инфо](http://trubetskoy1.narod.ru/alg/radixsort.html)
1216

1317
## Структуры данных / Data Structures
1418
1. [Стек / Stack](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/structures/stack.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%A1%D1%82%D0%B5%D0%BA)
@@ -19,10 +23,13 @@
1923
## Деревья / Tree
2024
1. [Двоичное дерево поиска / Binary Search Tree](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/trees/binary-tree.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%94%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D0%BE%D0%B5_%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%BE_%D0%BF%D0%BE%D0%B8%D1%81%D0%BA%D0%B0)
2125

26+
## Кучи / Heap
27+
1. [Двоичная куча / Binary Heap](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/heap/binary-heap/binary-heap.js) | [Инфо](https://neerc.ifmo.ru/wiki/index.php?title=%D0%94%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D0%B0%D1%8F_%D0%BA%D1%83%D1%87%D0%B0)
28+
2229
## Поиск / Search
2330
1. [Линейный поиск / Linear Search](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/search/linear-search.js) | [Инфо](https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm)
2431
2. [Двоичный поиск / Binary Search](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/search/binary-search.js) | [Инфо](https://prog-cpp.ru/search-binary/)
25-
32+
3. [Поиск в ширину / Breadth-First Search](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/search/breadth-first-search.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%9F%D0%BE%D0%B8%D1%81%D0%BA_%D0%B2_%D1%88%D0%B8%D1%80%D0%B8%D0%BD%D1%83)
2633

2734
## Хеширование / Hashing
2835
1. [Хеш-таблица / Hash-Table](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/hash-table/hash-table.js) | [Инфо](https://bitsofmind.wordpress.com/2008/07/28/introduction_in_hash_tables/)

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
<script src="./source/sorts/odd-even-sort/odd-even-sort.js" type="module"></script>
1616
<script src="./source/sorts/quick-sort/quick-sort.js" type="module"></script>
1717
<script src="./source/sorts/radix-sort/radix-sort.js" type="module"></script>
18+
<script src="./source/sorts/gnome-sort/gnome-sort.js" type="module"></script>
19+
<script src="./source/sorts/solomon-sort/solomon-sort.js" type="module"></script>
20+
<script src="./source/sorts/bead-sort/bead-sort.js" type="module"></script>
1821
<script src="./source/structures/stack.js" type="module"></script>
1922
<script src="./source/structures/queue.js" type="module"></script>
2023
<script src="./source/structures/singly-list.js" type="module"></script>
2124
<script src="./source/trees/binary-tree.js" type="module"></script>
2225
<script src="./source/search/linear-search/linear-search.js" type="module"></script>
2326
<script src="./source/search/binary-search/binary-search.js" type="module"></script>
2427
<script src="./source/hash-table/hash-table.js" type="module"></script>
25-
<script src="./source/cryptographic/caesar-cipher/caesar-cipher.js" type="module"></script>
28+
<script src="./source/hash-table/hash-table.js" type="module"></script>
29+
<script src="./source/heap/binary-heap/binary-heap.js" type="module"></script>
2630
<script src="./source/cryptographic/affine-cipher/affine-cipher.js" type="module"></script>
2731
<script src="./source/cryptographic/vigenere-cipher/vigenere-cipher.js" type="module"></script>
2832
<script src="./source/other/fisher-yates/fisher-yates.js" type="module"></script>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { swap } from "../../utils/helpers.js";
2+
3+
export default class BinaryHeap {
4+
constructor(compare) {
5+
this.heap = [];
6+
this.compare = compare;
7+
}
8+
add(value) {
9+
this.heap.push(value);
10+
let i = this.heap.length - 1;
11+
let parent = (i - 1) / 2;
12+
while (i > 0 && this.heap[parent] < this.heap[i]) {
13+
swap(this.heap, i, parent);
14+
i = parent;
15+
parent = (i - 1) / 2;
16+
}
17+
}
18+
heapify(index) {
19+
let leftChild;
20+
let rightChild;
21+
let topChild;
22+
while (true) {
23+
leftChild = 2 * index + 1;
24+
rightChild = 2 * index + 2;
25+
topChild = index;
26+
if (
27+
leftChild < this.heap.length &&
28+
this.compare(this.heap[leftChild], this.heap[topChild])
29+
) {
30+
topChild = leftChild;
31+
}
32+
if (
33+
rightChild < this.heap.length &&
34+
this.compare(this.heap[rightChild], this.heap[topChild])
35+
) {
36+
topChild = rightChild;
37+
}
38+
if (topChild == index) {
39+
break;
40+
}
41+
swap(this.heap, index, topChild);
42+
index = topChild;
43+
}
44+
}
45+
build(sourceArray) {
46+
this.heap = [...sourceArray];
47+
for (let i = this.heap.length / 2; i >= 0; i--) {
48+
this.heapify(i);
49+
}
50+
}
51+
getTop() {
52+
let result = this.heap[0];
53+
this.heap[0] = this.heap[this.heap.length - 1];
54+
this.heap.splice(this.heap.length - 1, this.heap.length);
55+
return result;
56+
}
57+
heapSort(array) {
58+
this.build(array);
59+
for (let i = array.length - 1; i >= 0; i--) {
60+
array[i] = this.getTop();
61+
this.heapify(0);
62+
}
63+
return array;
64+
}
65+
print() {
66+
console.log(`root -> ${this.heap[0]}`);
67+
for (let i = 0; i < this.heap.length; i++) {
68+
let left = this.heap[2 * i + 1];
69+
let right = this.heap[2 * i + 2];
70+
if (!!left || right) {
71+
console.log(
72+
`left ${!!left ? left : ""} -- right ${!!right
73+
? right
74+
: ""}`
75+
);
76+
if (i % 2 === 0) {
77+
console.log("__level__");
78+
}
79+
}
80+
}
81+
}
82+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import BinaryHeap from "./binary-heap";
2+
describe("binary-heap", () => {
3+
test("should return 12", () => {
4+
const heap = new BinaryHeap((a, b) => {
5+
return a > b;
6+
});
7+
heap.build([12, 10, 9, 5, 6, 1]);
8+
expect(heap.getTop()).toBe(12);
9+
});
10+
11+
test("should return 1", () => {
12+
const heap = new BinaryHeap((a, b) => {
13+
return a < b;
14+
});
15+
heap.build([12, 10, 9, 5, 6, 1]);
16+
expect(heap.getTop()).toBe(1);
17+
});
18+
19+
test("should return true", () => {
20+
const heap = new BinaryHeap((a, b) => {
21+
return a > b;
22+
});
23+
heap.build([12, 10, 9, 5, 6, 1]);
24+
expect(heap.heap).toEqual([12, 10, 9, 5, 6, 1]);
25+
});
26+
27+
test("should return [1,5,6,9,10,12] if sort", () => {
28+
const heap = new BinaryHeap((a, b) => {
29+
return a > b;
30+
});
31+
expect(heap.heapSort([12, 10, 9, 5, 6, 1])).toEqual([
32+
1,
33+
5,
34+
6,
35+
9,
36+
10,
37+
12
38+
]);
39+
});
40+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default function bfs(root) {
2+
let queue = [];
3+
let values = [];
4+
queue.push(root);
5+
while (queue.length > 0) {
6+
let current = queue.shift();
7+
values.push(current.data);
8+
if (current.left) {
9+
queue.push(current.left);
10+
}
11+
if (current.right) {
12+
queue.push(current.right);
13+
}
14+
}
15+
return values;
16+
}

source/sorts/bead-sort/bead-sort.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export default function beadSort(array) {
2+
let beadArray = [];
3+
let sortedArray = [];
4+
for (let i = 0; i < array.length; i++) {
5+
beadArray[i] = [];
6+
}
7+
let levelCount = [];
8+
for (let i = 0; i < Math.max(...array); i++) {
9+
levelCount[i] = 0;
10+
}
11+
for (let i = 0; i < array.length; i++) {
12+
for (let j = 0; j < array[i]; j++) {
13+
beadArray[i].push(0);
14+
}
15+
}
16+
for (let i = 0; i < array.length; i++) {
17+
let element = array[i];
18+
for (let j = 0; element > 0; j++) {
19+
beadArray[levelCount[j]++][j] = 1;
20+
element--;
21+
}
22+
}
23+
for (let i = beadArray.length - 1; i >= 0; i--) {
24+
let value = 0;
25+
for (let j = 0; j < beadArray[i].length; j++) {
26+
if (beadArray[i][j]) {
27+
value++;
28+
}
29+
}
30+
sortedArray.push(value);
31+
value = 0;
32+
}
33+
return sortedArray;
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import beadSort from "./bead-sort";
2+
3+
describe("bead-sort", () => {
4+
test("should return [1,2,3,4,5,6] if sort", () => {
5+
const testArray = [3, 1, 2, 4, 6, 5];
6+
expect(beadSort(testArray)).toEqual([1, 2, 3, 4, 5, 6]);
7+
});
8+
9+
test("should return [12,22,44,45,56,76] if sort", () => {
10+
const testArray = [22, 12, 45, 44, 76, 56];
11+
expect(beadSort(testArray)).toEqual([12, 22, 44, 45, 56, 76]);
12+
});
13+
});

source/sorts/gnome-sort/gnome-sort.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { swap } from "../../utils/helpers.js";
2+
3+
export default function gnomeSort(array) {
4+
let counter = 1;
5+
while (counter < array.length) {
6+
if (counter === 0) {
7+
counter = 1;
8+
}
9+
if (array[counter - 1] <= array[counter]) {
10+
counter++;
11+
} else {
12+
swap(array, counter, counter - 1);
13+
counter--;
14+
}
15+
}
16+
return array;
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import gnomeSort from "./gnome-sort";
2+
3+
describe("gnome-sort", () => {
4+
test("should return [1,2,3,4,5,6] if sort", () => {
5+
const testArray = [3, 1, 2, 4, 6, 5];
6+
expect(gnomeSort(testArray)).toEqual([1, 2, 3, 4, 5, 6]);
7+
});
8+
9+
test("should return [12,22,44,45,56,76] if sort", () => {
10+
const testArray = [22, 12, 45, 44, 76, 56];
11+
expect(gnomeSort(testArray)).toEqual([12, 22, 44, 45, 56, 76]);
12+
});
13+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export default function solomonSort(array) {
2+
const min = Math.min(...array);
3+
const max = Math.max(...array);
4+
const n = array.length - 1;
5+
const delta = Math.floor((max - min) / n);
6+
let sortedArray = [];
7+
let indexArray = [];
8+
let newArray = [];
9+
for (let i = 0; i < array.length; i++) {
10+
indexArray[i] = 0;
11+
newArray[i] = [];
12+
}
13+
for (let i = 0; i < array.length; i++) {
14+
let newIndex = (array[i] - min) / delta + 1;
15+
newIndex = Math.floor(newIndex);
16+
indexArray[newIndex - 1]++;
17+
newArray[newIndex - 1].push(array[i]);
18+
}
19+
for (let i = 0; i < array.length; i++) {
20+
if (!!newArray[i][0]) {
21+
while (newArray[i].length !== 0) {
22+
let minValue = Math.min(...newArray[i]);
23+
let minIndex = newArray[i].indexOf(minValue);
24+
newArray[i].splice(minIndex, 1);
25+
sortedArray.push(minValue);
26+
}
27+
}
28+
}
29+
return sortedArray;
30+
}

0 commit comments

Comments
 (0)