Skip to content

Commit 84376f2

Browse files
add bead sort
1 parent e959b60 commit 84376f2

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
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)
1212
7. [Гномья сортировка / Gnome sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/gnome-sort/gnome-sort.js) | [Инфо](http://sorting.valemak.com/gnome/)
1313
7. [Соломонова Сортировка / Solomon sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/solomon-sort/solomon-sort.js) | [Инфо](http://sorting.valemak.com/solomon/)
14+
7. [Бисерная сортировка / Bead Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/bead-sort/bead-sort.js) | [Инфо](http://sorting.valemak.com/bead/)
1415

1516
## Структуры данных / Data Structures
1617
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)

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<script src="./source/sorts/quick-sort/quick-sort.js" type="module"></script>
1717
<script src="./source/sorts/gnome-sort/gnome-sort.js" type="module"></script>
1818
<script src="./source/sorts/solomon-sort/solomon-sort.js" type="module"></script>
19+
<script src="./source/sorts/bead-sort/bead-sort.js" type="module"></script>
1920
<script src="./source/structures/stack.js" type="module"></script>
2021
<script src="./source/structures/queue.js" type="module"></script>
2122
<script src="./source/structures/singly-list.js" type="module"></script>

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+
});

0 commit comments

Comments
 (0)