Skip to content

Commit 63049b8

Browse files
authored
Merge pull request #61 from olshansky/feat/counting-sort
feat: add counting and flash sorts
2 parents 7153799 + 7bd9840 commit 63049b8

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Sorts/countingSort.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers;
3+
* that is, it is an integer sorting algorithm.
4+
* more information: https://en.wikipedia.org/wiki/Counting_sort
5+
* counting sort visualization: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html
6+
*/
7+
8+
function countingSort(arr, min, max) {
9+
let i;
10+
let z = 0;
11+
const count = [];
12+
13+
for (i = min; i <= max; i++) {
14+
count[i] = 0;
15+
}
16+
17+
for (i = 0; i < arr.length; i++) {
18+
count[arr[i]]++;
19+
}
20+
21+
for (i = min; i <= max; i++) {
22+
while (count[i]-- > 0) {
23+
arr[z++] = i;
24+
}
25+
}
26+
27+
return arr;
28+
}
29+
30+
const arr = [3, 0, 2, 5, 4, 1];
31+
32+
// Array before Sort
33+
console.log("-----before sorting-----");
34+
console.log(arr);
35+
// Array after sort
36+
console.log("-----after sorting-----");
37+
console.log(countingSort(arr, 0, 5));

Sorts/flashSort.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Flashsort is a distribution sorting algorithm showing linear computational complexity O(n) for uniformly distributed
3+
* data sets and relatively little additional memory requirement.
4+
* more information: https://en.wikipedia.org/wiki/Flashsort
5+
*/
6+
7+
function flashSort(arr) {
8+
let max = 0, min = arr[0];
9+
let n = arr.length;
10+
let m = ~~(0.45 * n);
11+
let l = new Array(m);
12+
13+
for (let i = 1; i < n; ++i) {
14+
if (arr[i] < min) {
15+
min = arr[i];
16+
}
17+
if (arr[i] > arr[max]) {
18+
max = i;
19+
}
20+
}
21+
22+
if (min === arr[max]) {
23+
return arr;
24+
}
25+
26+
let c1 = (m - 1) / (arr[max] - min);
27+
28+
for (let k = 0; k < m; k++) {
29+
l[k] = 0;
30+
}
31+
32+
for (let j = 0; j < n; ++j) {
33+
k = ~~(c1 * (arr[j] - min));
34+
++l[k];
35+
}
36+
37+
for (let p = 1; p < m; ++p) {
38+
l[p] = l[p] + l[p - 1];
39+
}
40+
41+
let hold = arr[max];
42+
arr[max] = arr[0];
43+
arr[0] = hold;
44+
45+
// permutation
46+
let move = 0, t, flash;
47+
j = 0;
48+
k = m - 1;
49+
50+
while (move < (n - 1)) {
51+
while (j > (l[k] - 1)) {
52+
++j;
53+
k = ~~(c1 * (arr[j] - min));
54+
}
55+
if (k < 0) break;
56+
flash = arr[j];
57+
while (j !== l[k]) {
58+
k = ~~(c1 * (flash - min));
59+
hold = arr[t = --l[k]];
60+
arr[t] = flash;
61+
flash = hold;
62+
++move;
63+
}
64+
}
65+
66+
// insertion
67+
for (j = 1; j < n; j++) {
68+
hold = arr[j];
69+
i = j - 1;
70+
while (i >= 0 && arr[i] > hold) {
71+
arr[i + 1] = arr[i--];
72+
}
73+
arr[i + 1] = hold;
74+
}
75+
return arr;
76+
}
77+
78+
const array = [3, 0, 2, 5, -1, 4, 1, -2];
79+
80+
// Array before Sort
81+
console.log("-----before sorting-----");
82+
console.log(array);
83+
// Array after sort
84+
console.log("-----after sorting-----");
85+
console.log(flashSort(array));

0 commit comments

Comments
 (0)