mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add Bucket Sort.
This commit is contained in:
parent
e95d856e67
commit
1ad60dc510
@ -133,6 +133,7 @@ a set of rules that precisely define a sequence of operations.
|
||||
* `B` [Shellsort](src/algorithms/sorting/shell-sort)
|
||||
* `B` [Counting Sort](src/algorithms/sorting/counting-sort)
|
||||
* `B` [Radix Sort](src/algorithms/sorting/radix-sort)
|
||||
* `B` [Bucket Sort](src/algorithms/sorting/bucket-sort)
|
||||
* **Linked Lists**
|
||||
* `B` [Straight Traversal](src/algorithms/linked-list/traversal)
|
||||
* `B` [Reverse Traversal](src/algorithms/linked-list/reverse-traversal)
|
||||
|
46
src/algorithms/sorting/bucket-sort/BucketSort.js
Normal file
46
src/algorithms/sorting/bucket-sort/BucketSort.js
Normal file
@ -0,0 +1,46 @@
|
||||
import RadixSort from '../radix-sort/RadixSort';
|
||||
|
||||
/**
|
||||
* Bucket Sort
|
||||
*
|
||||
* @param {number[]} arr
|
||||
* @param {number} bucketsNum
|
||||
* @return {number[]}
|
||||
*/
|
||||
export default function BucketSort(arr, bucketsNum = 1) {
|
||||
const buckets = new Array(bucketsNum).fill(null).map(() => []);
|
||||
|
||||
const minValue = Math.min(...arr);
|
||||
const maxValue = Math.max(...arr);
|
||||
|
||||
const bucketSize = Math.ceil(Math.max(1, (maxValue - minValue) / bucketsNum));
|
||||
|
||||
// Place elements into buckets.
|
||||
for (let i = 0; i < arr.length; i += 1) {
|
||||
const currValue = arr[i];
|
||||
const bucketIndex = Math.floor((currValue - minValue) / bucketSize);
|
||||
|
||||
// Edge case for max value.
|
||||
if (bucketIndex === bucketsNum) {
|
||||
buckets[bucketsNum - 1].push(currValue);
|
||||
} else {
|
||||
buckets[bucketIndex].push(currValue);
|
||||
}
|
||||
}
|
||||
|
||||
// Sort individual buckets.
|
||||
for (let i = 0; i < buckets.length; i += 1) {
|
||||
// Let's use the Radix Sorter here. This may give us
|
||||
// the average O(n + k) time complexity to sort one bucket
|
||||
// (where k is a number of digits in the longest number).
|
||||
buckets[i] = new RadixSort().sort(buckets[i]);
|
||||
}
|
||||
|
||||
// Merge sorted buckets into final output.
|
||||
const sortedArr = [];
|
||||
for (let i = 0; i < buckets.length; i += 1) {
|
||||
sortedArr.push(...buckets[i]);
|
||||
}
|
||||
|
||||
return sortedArr;
|
||||
}
|
35
src/algorithms/sorting/bucket-sort/README.md
Normal file
35
src/algorithms/sorting/bucket-sort/README.md
Normal file
@ -0,0 +1,35 @@
|
||||
# Bucket Sort
|
||||
|
||||
**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.
|
||||
|
||||
## Algorithm
|
||||
|
||||
Bucket sort works as follows:
|
||||
|
||||
1. Set up an array of initially empty `buckets`.
|
||||
2. **Scatter:** Go over the original array, putting each object in its `bucket`.
|
||||
3. Sort each non-empty `bucket`.
|
||||
4. **Gather:** Visit the `buckets` in order and put all elements back into the original array.
|
||||
|
||||
Elements are distributed among bins:
|
||||
|
||||
![Elements are distributed among bins](./images/bucket_sort_1.png)
|
||||
|
||||
Then, elements are sorted within each bin:
|
||||
|
||||
![Elements are sorted within each bin](./images/bucket_sort_2.png)
|
||||
|
||||
|
||||
## Complexity
|
||||
|
||||
The computational complexity depends on the algorithm used to sort each bucket, the number of buckets to use, and whether the input is uniformly distributed.
|
||||
|
||||
The **worst-case** time complexity of bucket sort is
|
||||
`O(n^2)` if the sorting algorithm used on the bucket is *insertion sort*, which is the most common use case since the expectation is that buckets will not have too many elements relative to the entire list. In the worst case, all elements are placed in one bucket, causing the running time to reduce to the worst-case complexity of insertion sort (all elements are in reverse order). If the worst-case running time of the intermediate sort used is `O(n * log(n))`, then the worst-case running time of bucket sort will also be
|
||||
`O(n * log(n))`.
|
||||
|
||||
On **average**, when the distribution of elements across buckets is reasonably uniform, it can be shown that bucket sort runs on average `O(n + k)` for `k` buckets.
|
||||
|
||||
## References
|
||||
|
||||
- [Bucket Sort on Wikipedia](https://en.wikipedia.org/wiki/Bucket_sort)
|
@ -0,0 +1,33 @@
|
||||
import BucketSort from '../BucketSort';
|
||||
import {
|
||||
equalArr,
|
||||
notSortedArr,
|
||||
reverseArr,
|
||||
sortedArr,
|
||||
} from '../../SortTester';
|
||||
|
||||
describe('BucketSort', () => {
|
||||
it('should sort the array of numbers with different buckets amounts', () => {
|
||||
expect(BucketSort(notSortedArr, 4)).toEqual(sortedArr);
|
||||
expect(BucketSort(equalArr, 4)).toEqual(equalArr);
|
||||
expect(BucketSort(reverseArr, 4)).toEqual(sortedArr);
|
||||
expect(BucketSort(sortedArr, 4)).toEqual(sortedArr);
|
||||
|
||||
expect(BucketSort(notSortedArr, 10)).toEqual(sortedArr);
|
||||
expect(BucketSort(equalArr, 10)).toEqual(equalArr);
|
||||
expect(BucketSort(reverseArr, 10)).toEqual(sortedArr);
|
||||
expect(BucketSort(sortedArr, 10)).toEqual(sortedArr);
|
||||
|
||||
expect(BucketSort(notSortedArr, 50)).toEqual(sortedArr);
|
||||
expect(BucketSort(equalArr, 50)).toEqual(equalArr);
|
||||
expect(BucketSort(reverseArr, 50)).toEqual(sortedArr);
|
||||
expect(BucketSort(sortedArr, 50)).toEqual(sortedArr);
|
||||
});
|
||||
|
||||
it('should sort the array of numbers with the default buckets of 1', () => {
|
||||
expect(BucketSort(notSortedArr)).toEqual(sortedArr);
|
||||
expect(BucketSort(equalArr)).toEqual(equalArr);
|
||||
expect(BucketSort(reverseArr)).toEqual(sortedArr);
|
||||
expect(BucketSort(sortedArr)).toEqual(sortedArr);
|
||||
});
|
||||
});
|
BIN
src/algorithms/sorting/bucket-sort/images/bucket_sort_1.png
Normal file
BIN
src/algorithms/sorting/bucket-sort/images/bucket_sort_1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
BIN
src/algorithms/sorting/bucket-sort/images/bucket_sort_2.png
Normal file
BIN
src/algorithms/sorting/bucket-sort/images/bucket_sort_2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
@ -3,37 +3,37 @@
|
||||
_Read this in other languages:_
|
||||
[_Português_](README.pt-BR.md),
|
||||
|
||||
In computer science, **radix sort** is a non-comparative integer sorting
|
||||
algorithm that sorts data with integer keys by grouping keys by the individual
|
||||
In computer science, **radix sort** is a non-comparative integer sorting
|
||||
algorithm that sorts data with integer keys by grouping keys by the individual
|
||||
digits which share the same significant position and value. A positional notation
|
||||
is required, but because integers can represent strings of characters
|
||||
(e.g., names or dates) and specially formatted floating point numbers, radix
|
||||
is required, but because integers can represent strings of characters
|
||||
(e.g., names or dates) and specially formatted floating point numbers, radix
|
||||
sort is not limited to integers.
|
||||
|
||||
*Where does the name come from?*
|
||||
|
||||
In mathematical numeral systems, the *radix* or base is the number of unique digits,
|
||||
including the digit zero, used to represent numbers in a positional numeral system.
|
||||
For example, a binary system (using numbers 0 and 1) has a radix of 2 and a decimal
|
||||
including the digit zero, used to represent numbers in a positional numeral system.
|
||||
For example, a binary system (using numbers 0 and 1) has a radix of 2 and a decimal
|
||||
system (using numbers 0 to 9) has a radix of 10.
|
||||
|
||||
## Efficiency
|
||||
|
||||
The topic of the efficiency of radix sort compared to other sorting algorithms is
|
||||
somewhat tricky and subject to quite a lot of misunderstandings. Whether radix
|
||||
sort is equally efficient, less efficient or more efficient than the best
|
||||
comparison-based algorithms depends on the details of the assumptions made.
|
||||
Radix sort complexity is `O(wn)` for `n` keys which are integers of word size `w`.
|
||||
Sometimes `w` is presented as a constant, which would make radix sort better
|
||||
(for sufficiently large `n`) than the best comparison-based sorting algorithms,
|
||||
which all perform `O(n log n)` comparisons to sort `n` keys. However, in
|
||||
general `w` cannot be considered a constant: if all `n` keys are distinct,
|
||||
then `w` has to be at least `log n` for a random-access machine to be able to
|
||||
store them in memory, which gives at best a time complexity `O(n log n)`. That
|
||||
would seem to make radix sort at most equally efficient as the best
|
||||
The topic of the efficiency of radix sort compared to other sorting algorithms is
|
||||
somewhat tricky and subject to quite a lot of misunderstandings. Whether radix
|
||||
sort is equally efficient, less efficient or more efficient than the best
|
||||
comparison-based algorithms depends on the details of the assumptions made.
|
||||
Radix sort complexity is `O(wn)` for `n` keys which are integers of word size `w`.
|
||||
Sometimes `w` is presented as a constant, which would make radix sort better
|
||||
(for sufficiently large `n`) than the best comparison-based sorting algorithms,
|
||||
which all perform `O(n log n)` comparisons to sort `n` keys. However, in
|
||||
general `w` cannot be considered a constant: if all `n` keys are distinct,
|
||||
then `w` has to be at least `log n` for a random-access machine to be able to
|
||||
store them in memory, which gives at best a time complexity `O(n log n)`. That
|
||||
would seem to make radix sort at most equally efficient as the best
|
||||
comparison-based sorts (and worse if keys are much longer than `log n`).
|
||||
|
||||
![Radix Sort](https://www.researchgate.net/publication/291086231/figure/fig1/AS:614214452404240@1523451545568/Simplistic-illustration-of-the-steps-performed-in-a-radix-sort-In-this-example-the.png)
|
||||
![Radix Sort](./images/radix-sort.png)
|
||||
|
||||
## Complexity
|
||||
|
||||
|
BIN
src/algorithms/sorting/radix-sort/images/radix-sort.png
Normal file
BIN
src/algorithms/sorting/radix-sort/images/radix-sort.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
Loading…
Reference in New Issue
Block a user