mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
Merge pull request #61 from olshansky/feat/counting-sort
feat: add counting and flash sorts
This commit is contained in:
37
Sorts/countingSort.js
Normal file
37
Sorts/countingSort.js
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers;
|
||||
* that is, it is an integer sorting algorithm.
|
||||
* more information: https://en.wikipedia.org/wiki/Counting_sort
|
||||
* counting sort visualization: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html
|
||||
*/
|
||||
|
||||
function countingSort(arr, min, max) {
|
||||
let i;
|
||||
let z = 0;
|
||||
const count = [];
|
||||
|
||||
for (i = min; i <= max; i++) {
|
||||
count[i] = 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < arr.length; i++) {
|
||||
count[arr[i]]++;
|
||||
}
|
||||
|
||||
for (i = min; i <= max; i++) {
|
||||
while (count[i]-- > 0) {
|
||||
arr[z++] = i;
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
const arr = [3, 0, 2, 5, 4, 1];
|
||||
|
||||
// Array before Sort
|
||||
console.log("-----before sorting-----");
|
||||
console.log(arr);
|
||||
// Array after sort
|
||||
console.log("-----after sorting-----");
|
||||
console.log(countingSort(arr, 0, 5));
|
85
Sorts/flashSort.js
Normal file
85
Sorts/flashSort.js
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Flashsort is a distribution sorting algorithm showing linear computational complexity O(n) for uniformly distributed
|
||||
* data sets and relatively little additional memory requirement.
|
||||
* more information: https://en.wikipedia.org/wiki/Flashsort
|
||||
*/
|
||||
|
||||
function flashSort(arr) {
|
||||
let max = 0, min = arr[0];
|
||||
let n = arr.length;
|
||||
let m = ~~(0.45 * n);
|
||||
let l = new Array(m);
|
||||
|
||||
for (let i = 1; i < n; ++i) {
|
||||
if (arr[i] < min) {
|
||||
min = arr[i];
|
||||
}
|
||||
if (arr[i] > arr[max]) {
|
||||
max = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (min === arr[max]) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
let c1 = (m - 1) / (arr[max] - min);
|
||||
|
||||
for (let k = 0; k < m; k++) {
|
||||
l[k] = 0;
|
||||
}
|
||||
|
||||
for (let j = 0; j < n; ++j) {
|
||||
k = ~~(c1 * (arr[j] - min));
|
||||
++l[k];
|
||||
}
|
||||
|
||||
for (let p = 1; p < m; ++p) {
|
||||
l[p] = l[p] + l[p - 1];
|
||||
}
|
||||
|
||||
let hold = arr[max];
|
||||
arr[max] = arr[0];
|
||||
arr[0] = hold;
|
||||
|
||||
// permutation
|
||||
let move = 0, t, flash;
|
||||
j = 0;
|
||||
k = m - 1;
|
||||
|
||||
while (move < (n - 1)) {
|
||||
while (j > (l[k] - 1)) {
|
||||
++j;
|
||||
k = ~~(c1 * (arr[j] - min));
|
||||
}
|
||||
if (k < 0) break;
|
||||
flash = arr[j];
|
||||
while (j !== l[k]) {
|
||||
k = ~~(c1 * (flash - min));
|
||||
hold = arr[t = --l[k]];
|
||||
arr[t] = flash;
|
||||
flash = hold;
|
||||
++move;
|
||||
}
|
||||
}
|
||||
|
||||
// insertion
|
||||
for (j = 1; j < n; j++) {
|
||||
hold = arr[j];
|
||||
i = j - 1;
|
||||
while (i >= 0 && arr[i] > hold) {
|
||||
arr[i + 1] = arr[i--];
|
||||
}
|
||||
arr[i + 1] = hold;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
const array = [3, 0, 2, 5, -1, 4, 1, -2];
|
||||
|
||||
// Array before Sort
|
||||
console.log("-----before sorting-----");
|
||||
console.log(array);
|
||||
// Array after sort
|
||||
console.log("-----after sorting-----");
|
||||
console.log(flashSort(array));
|
Reference in New Issue
Block a user