-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]--; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] }; | ||
} | ||
}; |