Skip to content

Commit

Permalink
Added Counting Sort & Radix Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
mszula committed Jan 3, 2024
1 parent fba9e0d commit 9f0c186
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions src/lib/sort-algorithms/algorithms.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,4 +33,12 @@ export const algorithms: AlgorithmDefinition[] = [
name: "Merge Sort",
function: mergeSort,
},
{
name: "Counting Sort",
function: countingSort,
},
{
name: "Radix Sort",
function: radixSort,
},
];
34 changes: 34 additions & 0 deletions src/lib/sort-algorithms/counting-sort.ts
Original file line number Diff line number Diff line change
@@ -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]--;
}
}
};
67 changes: 67 additions & 0 deletions src/lib/sort-algorithms/radix-sort.ts
Original file line number Diff line number Diff line change
@@ -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: [] };
}
};

0 comments on commit 9f0c186

Please sign in to comment.