From 9f0c186d538bee9802c83ed29688f7db31d4b8a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Szu=C5=82a?= Date: Wed, 3 Jan 2024 17:41:39 +0100 Subject: [PATCH] Added Counting Sort & Radix Sort --- README.md | 2 + src/lib/sort-algorithms/algorithms.ts | 10 ++++ src/lib/sort-algorithms/counting-sort.ts | 34 ++++++++++++ src/lib/sort-algorithms/radix-sort.ts | 67 ++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 src/lib/sort-algorithms/counting-sort.ts create mode 100644 src/lib/sort-algorithms/radix-sort.ts diff --git a/README.md b/README.md index 88d6396..ea3259c 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ https://github.com/mszula/visual-sorting/assets/17621834/3077ad21-ed6e-432c-ae51 - Quick Sort - Shell Sort - Merge Sort +- Counting Sort +- Radix Sort ## 🙌 Contribution diff --git a/src/lib/sort-algorithms/algorithms.ts b/src/lib/sort-algorithms/algorithms.ts index 7440d2d..a12eab6 100644 --- a/src/lib/sort-algorithms/algorithms.ts +++ b/src/lib/sort-algorithms/algorithms.ts @@ -1,7 +1,9 @@ import { bubleSort } from "./buble-sort"; +import { countingSort } from "./counting-sort"; import { insertionSort } from "./insertion-sort"; import { mergeSort } from "./merge-sort"; import { quickSort } from "./quick-sort"; +import { radixSort } from "./radix-sort"; import { selectionSort } from "./selection-sort"; import { shellSort } from "./shell-sort"; import type { AlgorithmDefinition } from "./types"; @@ -31,4 +33,12 @@ export const algorithms: AlgorithmDefinition[] = [ name: "Merge Sort", function: mergeSort, }, + { + name: "Counting Sort", + function: countingSort, + }, + { + name: "Radix Sort", + function: radixSort, + }, ]; diff --git a/src/lib/sort-algorithms/counting-sort.ts b/src/lib/sort-algorithms/counting-sort.ts new file mode 100644 index 0000000..0431a5f --- /dev/null +++ b/src/lib/sort-algorithms/counting-sort.ts @@ -0,0 +1,34 @@ +import type { SortingGenerator } from "./types"; + +export const countingSort = function* (arr: number[]): SortingGenerator { + let j = 0, + max = arr[0], + count = []; + + for (let i = 0; i < arr.length; i++) { + yield { accesing: [i], sound: i }; + if (arr[i] > max) { + max = arr[i]; + } + } + + for (let i = 0; i <= max; i++) { + yield { accesing: [i], sound: i }; + count[i] = 0; + } + + for (let i = 0; i < arr.length; i++) { + yield { accesing: [i], sound: i }; + count[arr[i]] += 1; + } + + console.log(count); + for (let i = 0; i <= max; i++) { + while (count[i] > 0) { + yield { accesing: [j], sound: j }; + arr[j] = i; + j++; + count[i]--; + } + } +}; diff --git a/src/lib/sort-algorithms/radix-sort.ts b/src/lib/sort-algorithms/radix-sort.ts new file mode 100644 index 0000000..dc32205 --- /dev/null +++ b/src/lib/sort-algorithms/radix-sort.ts @@ -0,0 +1,67 @@ +import type { SortingGenerator } from "./types"; + +const getMax = function* (arr: number[], n: number) { + let mx = arr[0]; + for (let i = 1; i < n; i++) { + yield { accesing: [i], sound: i }; + if (arr[i] > mx) { + mx = arr[i]; + } + } + return mx; +}; + +const countSort = function* ( + arr: number[], + n: number, + exp: number +): SortingGenerator { + const output = new Array(n); // output array + const count = new Array(10); + + let i; + for (let i = 0; i < 10; i++) count[i] = 0; + + // Store count of occurrences in count[] + for (i = 0; i < n; i++) { + let x = Math.floor(arr[i] / exp) % 10; + count[x]++; + yield { accesing: [i], sound: i }; + } + + // Change count[i] so that count[i] now contains + // actual position of this digit in output[] + for (i = 1; i < 10; i++) count[i] += count[i - 1]; + + // Build the output array + for (i = n - 1; i >= 0; i--) { + let x = Math.floor(arr[i] / exp) % 10; + output[count[x] - 1] = arr[i]; + count[x]--; + yield { accesing: [i], sound: i }; + } + + // Copy the output array to arr[], so that arr[] now + // contains sorted numbers according to current digit + for (i = 0; i < n; i++) { + arr[i] = output[i]; + yield { accesing: [i], sound: i }; + } +}; + +// The main function to that sorts arr[] of size n using +// Radix Sort +export const radixSort = function* (arr: number[]): SortingGenerator { + const len = arr.length; + // Find the maximum number to know number of digits + const m = yield* getMax(arr, len); + + // Do counting sort for every digit. Note that + // instead of passing digit number, exp is passed. + // exp is 10^i where i is current digit number + for (let exp = 1; Math.floor(m / exp) > 0; exp *= 10) { + yield { accesing: [] }; + yield* countSort(arr, len, exp); + yield { accesing: [] }; + } +};