mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00

* Remove QuickSelect doctest There are more Jest test cases already. * Remove AverageMedian doctest Already migrated to jest * Migrate doctest for BinaryExponentiationRecursive.js (also remove inline "main" test method) * Migrate doctest for EulersTotient.js (also remove inline "main" test method) * Migrate doctest for PrimeFactors.js (also remove inline "main" test method) * Migrate doctest for BogoSort.js Re-write prototype-polluting helper methods, too. (also remove inline test driver code) * Migrate doctest for BeadSort.js (also remove inline test driver code) * Migrate doctest for BucketSort.js (also remove inline test driver code) * Migrate doctest for CocktailShakerSort.js (also remove inline test driver code) * Migrate doctest for MergeSort.js (also remove inline test driver code) * Migrate doctest for QuickSort.js (also remove inline test driver code) * Migrate doctest for ReverseString.js (also remove inline test driver code) * Migrate doctest for ReverseString.js * Migrate doctest for ValidateEmail.js * Migrate doctest for ConwaysGameOfLife.js (remove the animate code, too) * Remove TernarySearch doctest Already migrated to jest * Migrate doctest for BubbleSort.js (also remove inline test driver code) * Remove doctest from CI and from dependencies relates to #742 fixes #586 * Migrate doctest for RgbHsvConversion.js * Add --fix option to "standard" npm script * Migrate doctest for BreadthFirstSearch.js (also remove inline test driver code) * Migrate doctest for BreadthFirstShortestPath.js (also remove inline test driver code) * Migrate doctest for EulerMethod.js (also remove inline test driver code) Move manual test-code for plotting stuff in the browser in a distinct file, too. Those "*.manual-test.js" files are excluded from the UpdateDirectory.mjs script, as well. * Migrate doctest for Mandelbrot.js (also remove inline test driver code & moved manual drawing test into a *.manual-test.js) * Migrate doctest for FloodFill.js * Migrate doctest for KochSnowflake.js (also move manual drawing test into a *.manual-test.js) * Update npm lockfile * Update README and COMMITTING with a few bits & bobs regarding testing & code quality
61 lines
2.2 KiB
JavaScript
61 lines
2.2 KiB
JavaScript
/**
|
|
* BucketSort implementation.
|
|
*
|
|
* Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array
|
|
* into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by
|
|
* recursively applying the bucket sorting algorithm. It is a distribution sort, and is a cousin of radix sort in the
|
|
* most to least significant digit flavour. Bucket sort is a generalization of pigeonhole sort. Bucket sort can be
|
|
* implemented with comparisons and therefore can also be considered a comparison sort algorithm. The computational
|
|
* complexity estimates involve the number of buckets.
|
|
*
|
|
* @see https://en.wikipedia.org/wiki/Bucket_sort#:~:text=Bucket%20sort%2C%20or%20bin%20sort,applying%20the%20bucket%20sorting%20algorithm.&text=Sort%20each%20non%2Dempty%20bucket.
|
|
*
|
|
* Time Complexity of Solution:
|
|
* Best Case O(n); Average Case O(n); Worst Case O(n)
|
|
*
|
|
* @param {number[]} list The array of numbers to be sorted.
|
|
* @param {number} size The size of the buckets used. If not provided, size will be 5.
|
|
* @return {number[]} An array of numbers sorted in increasing order.
|
|
*/
|
|
export function bucketSort (list, size) {
|
|
if (undefined === size) {
|
|
size = 5
|
|
}
|
|
if (list.length === 0) {
|
|
return list
|
|
}
|
|
let min = list[0]
|
|
let max = list[0]
|
|
// find min and max
|
|
for (let iList = 0; iList < list.length; iList++) {
|
|
if (list[iList] < min) {
|
|
min = list[iList]
|
|
} else if (list[iList] > max) {
|
|
max = list[iList]
|
|
}
|
|
}
|
|
// how many buckets we need
|
|
const count = Math.floor((max - min) / size) + 1
|
|
|
|
// create buckets
|
|
const buckets = []
|
|
for (let iCount = 0; iCount < count; iCount++) {
|
|
buckets.push([])
|
|
}
|
|
|
|
// bucket fill
|
|
for (let iBucket = 0; iBucket < list.length; iBucket++) {
|
|
const key = Math.floor((list[iBucket] - min) / size)
|
|
buckets[key].push(list[iBucket])
|
|
}
|
|
const sorted = []
|
|
// now sort every bucket and merge it to the sorted list
|
|
for (let iBucket = 0; iBucket < buckets.length; iBucket++) {
|
|
const arr = buckets[iBucket].sort((a, b) => a - b)
|
|
for (let iSorted = 0; iSorted < arr.length; iSorted++) {
|
|
sorted.push(arr[iSorted])
|
|
}
|
|
}
|
|
return sorted
|
|
}
|