mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-14 06:52:59 +08:00
Bucket Sort
Not perfect but a good start
This commit is contained in:
parent
dc1047df72
commit
f1c261695a
39
src/algorithms/sorting/Bucket-sort/BucketSort.js
Normal file
39
src/algorithms/sorting/Bucket-sort/BucketSort.js
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import Sort from '../Sort';
|
||||||
|
|
||||||
|
export default class BucketSort extends Sort {
|
||||||
|
sort(originalArray) {
|
||||||
|
let array = [];
|
||||||
|
let bucketSize = originalArray.length;
|
||||||
|
|
||||||
|
let minValue = originalArray[0];
|
||||||
|
let maxValue = originalArray[0];
|
||||||
|
originalArray.forEach(function (currentVal) { //get Min and Max number in array
|
||||||
|
if (currentVal < minValue) {
|
||||||
|
minValue = currentVal;
|
||||||
|
} else if (currentVal > maxValue) {
|
||||||
|
maxValue = currentVal;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
let buckets = new Array(bucketSize);
|
||||||
|
|
||||||
|
for (var i = 0; i < buckets.length; i++) { //make 2d array for storing numbers in different buckets
|
||||||
|
buckets[i] = new Array(bucketSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
originalArray.forEach(element => { //for each item in array sort them into their respected bucket
|
||||||
|
let index = Math.floor(element - minValue); // get index by getting the number - min value in array.
|
||||||
|
buckets[index].push(element); //pushes element into bucket of index we found
|
||||||
|
});
|
||||||
|
|
||||||
|
buckets.forEach(element => { // Concat all buckets into one array
|
||||||
|
element.forEach(number => {
|
||||||
|
if (typeof number !== 'undefined') {
|
||||||
|
array.push(number);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return array
|
||||||
|
}
|
||||||
|
}
|
42
src/algorithms/sorting/Bucket-sort/README.md
Normal file
42
src/algorithms/sorting/Bucket-sort/README.md
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# Bucket Sort
|
||||||
|
|
||||||
|
**Bucket sort** is a sorting algorithm that works by distributing
|
||||||
|
elements of an array into a number of different buckets.Each bucket
|
||||||
|
representing a number range, then the buckets are sorted individually,
|
||||||
|
either using a different sorting algorithm, or by recursively applying
|
||||||
|
the bucket sorting algorithm.
|
||||||
|
|
||||||
|
The average time complexity for Bucket Sort is O(n + k) (k = number of buckets). The worst time
|
||||||
|
complexity is O(n²).
|
||||||
|
|
||||||
|
## Algorithm
|
||||||
|
|
||||||
|
**Step I**
|
||||||
|
|
||||||
|
Our first real step is to create our buckets, we make an array thats the size of our original array, and then that array into a 2D array that will represent our buckets. This array will hold all of our elements for when we sort them.
|
||||||
|
|
||||||
|
<!-- ![Counting Sort](https://3.bp.blogspot.com/-jJchly1BkTc/WLGqCFDdvCI/AAAAAAAAAHA/luljAlz2ptMndIZNH0KLTTuQMNsfzDeFQCLcB/s1600/CSortUpdatedStepI.gif) -->
|
||||||
|
|
||||||
|
**Step II**
|
||||||
|
|
||||||
|
Next step we do the sorting, we find the index of the bucket we want to add our value to by doing a small equation. We take the element we want to sort and subtract the smallest value from our original array, and thats our index, so we just push that to our buckets array.
|
||||||
|
|
||||||
|
<!-- ![Counting Sort](https://1.bp.blogspot.com/-1vFu-VIRa9Y/WLHGuZkdF3I/AAAAAAAAAHs/8jKu2dbQee4ap9xlVcNsILrclqw0UxAVACLcB/s1600/Step-II.png) -->
|
||||||
|
|
||||||
|
**Step III**
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!-- ![Counting Sort](https://1.bp.blogspot.com/-xPqylngqASY/WLGq3p9n9vI/AAAAAAAAAHM/JHdtXAkJY8wYzDMBXxqarjmhpPhM0u8MACLcB/s1600/ResultArrayCS.gif) -->
|
||||||
|
|
||||||
|
## Complexity
|
||||||
|
|
||||||
|
| Name | Best | Average | Worst | Memory | Stable | Comments |
|
||||||
|
| --------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
|
||||||
|
| **Counting sort** | n + r | n + r | n + r | n + r | Yes | r - biggest number in array |
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- [Wikipedia](https://en.wikipedia.org/wiki/Bucket_sort)
|
||||||
|
- [YouTube](https://www.youtube.com/watch?v=VuXbEb5ywrU)
|
||||||
|
- [GeeksforGeeks](https://www.geeksforgeeks.org/bucket-sort-2/)
|
@ -0,0 +1,64 @@
|
|||||||
|
import BucketSort from '../BucketSort';
|
||||||
|
import {
|
||||||
|
equalArr,
|
||||||
|
notSortedArr,
|
||||||
|
reverseArr,
|
||||||
|
sortedArr,
|
||||||
|
SortTester,
|
||||||
|
} from '../../SortTester';
|
||||||
|
|
||||||
|
// Complexity constants.
|
||||||
|
// const SORTED_ARRAY_VISITING_COUNT = 20;
|
||||||
|
// const NOT_SORTED_ARRAY_VISITING_COUNT = 189;
|
||||||
|
// const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209;
|
||||||
|
// const EQUAL_ARRAY_VISITING_COUNT = 20;
|
||||||
|
|
||||||
|
describe('BucketSort', () => {
|
||||||
|
it('should sort array', () => {
|
||||||
|
SortTester.testSort(BucketSort);
|
||||||
|
});
|
||||||
|
|
||||||
|
// it('should sort array with custom comparator', () => {
|
||||||
|
// SortTester.testSortWithCustomComparator(BucketSort);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('should do stable sorting', () => {
|
||||||
|
// SortTester.testSortStability(BucketSort);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('should sort negative numbers', () => {
|
||||||
|
// SortTester.testNegativeNumbersSort(BucketSort);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('should visit EQUAL array element specified number of times', () => {
|
||||||
|
// SortTester.testAlgorithmTimeComplexity(
|
||||||
|
// BucketSort,
|
||||||
|
// equalArr,
|
||||||
|
// EQUAL_ARRAY_VISITING_COUNT,
|
||||||
|
// );
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('should visit SORTED array element specified number of times', () => {
|
||||||
|
// SortTester.testAlgorithmTimeComplexity(
|
||||||
|
// BucketSort,
|
||||||
|
// sortedArr,
|
||||||
|
// SORTED_ARRAY_VISITING_COUNT,
|
||||||
|
// );
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('should visit NOT SORTED array element specified number of times', () => {
|
||||||
|
// SortTester.testAlgorithmTimeComplexity(
|
||||||
|
// BucketSort,
|
||||||
|
// notSortedArr,
|
||||||
|
// NOT_SORTED_ARRAY_VISITING_COUNT,
|
||||||
|
// );
|
||||||
|
// });
|
||||||
|
|
||||||
|
// it('should visit REVERSE SORTED array element specified number of times', () => {
|
||||||
|
// SortTester.testAlgorithmTimeComplexity(
|
||||||
|
// BucketSort,
|
||||||
|
// reverseArr,
|
||||||
|
// REVERSE_SORTED_ARRAY_VISITING_COUNT,
|
||||||
|
// );
|
||||||
|
// });
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user