Skip to content

Commit e959b60

Browse files
add gnome and solomon sort
1 parent d0af596 commit e959b60

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
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/)
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)
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+
7. [Соломонова Сортировка / Solomon sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/solomon-sort/solomon-sort.js) | [Инфо](http://sorting.valemak.com/solomon/)
1214

1315
## Структуры данных / Data Structures
1416
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<script src="./source/sorts/selection-sort/selection-sort.js" type="module"></script>
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>
17+
<script src="./source/sorts/gnome-sort/gnome-sort.js" type="module"></script>
18+
<script src="./source/sorts/solomon-sort/solomon-sort.js" type="module"></script>
1719
<script src="./source/structures/stack.js" type="module"></script>
1820
<script src="./source/structures/queue.js" type="module"></script>
1921
<script src="./source/structures/singly-list.js" type="module"></script>

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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import solomonSort from "./solomon-sort";
2+
3+
describe("solomon-sort", () => {
4+
test("should return [1,2,3,4,5,6] if sort", () => {
5+
const testArray = [3, 1, 2, 4, 6, 5];
6+
expect(solomonSort(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(solomonSort(testArray)).toEqual([12, 22, 44, 45, 56, 76]);
12+
});
13+
});

0 commit comments

Comments
 (0)