Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: wolffles/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 4 files changed
  • 1 contributor

Commits on Jan 19, 2019

  1. Added a bucket sort #206

    wolffles committed Jan 19, 2019
    Copy the full SHA
    850f0ec View commit details
Showing with 1,539 additions and 1,418 deletions.
  1. +1,418 −1,418 package-lock.json
  2. +69 −0 src/algorithms/sorting/bucket-sort/BucketSort.js
  3. +37 −0 src/algorithms/sorting/bucket-sort/README.md
  4. +15 −0 src/algorithms/sorting/bucket-sort/_test_/BucketSort.test.js
2,836 changes: 1,418 additions & 1,418 deletions package-lock.json

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions src/algorithms/sorting/bucket-sort/BucketSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// this code utilizes my own hash_sort
import Sort from '../Sort';

export default class BucketSort extends Sort {
/**
* @param {number[]} array
* @description BucketSort is ideal for an even distribution.
*/

// InsertionSort to be used within bucket sort
insertionSort(array) {
const noReassign = array;
const size = noReassign.length;
for (let i = 1; i < size; i += 1) {
const temp = noReassign[i];
for (let j = i - 1; j >= 0 && noReassign[j] > temp; j -= 1) {
noReassign[j + 1] = noReassign[j];
noReassign[j + 1] = temp;
}
}

return noReassign;
}

// Implement bucket sort
sort(array, bucketSize = 1) {
// Declaring vars
const noReassign = array;
let i;
let minValue = noReassign[0];
let maxValue = noReassign[0];
if (noReassign.length === 0) {
return noReassign;
}
// Setting min and max values
noReassign.forEach((currentVal) => {
if (currentVal < minValue) {
minValue = currentVal;
} else if (currentVal > maxValue) {
maxValue = currentVal;
}
});

// Initializing buckets
const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
const allBuckets = new Array(bucketCount);

for (i = 0; i < allBuckets.length; i += 1) {
allBuckets[i] = [];
}

// Pushing values to buckets
noReassign.forEach((element) => {
allBuckets[Math.floor((element - minValue) / bucketSize)].push(element);
});

// Sorting buckets
noReassign.length = 0;

allBuckets.forEach((bucket) => {
this.insertionSort(bucket);
bucket.forEach((element) => {
noReassign.push(element);
});
});

return noReassign;
}
}
37 changes: 37 additions & 0 deletions src/algorithms/sorting/bucket-sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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. It is a distribution sort, a generalization of pigeonhole sort, and is a cousin of radix sort in the most-to-least significant digit flavor. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. 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.

## Algorithm

**Phase I**

1. Set up an array of initially empty "buckets".
2. Scatter: Go over the original array, putting each object in its bucket.

![Bucket Sort](https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/Bucket_sort_1.svg/311px-Bucket_sort_1.svg.png)

**Phase II**

3. Sort each non-empty bucket.
**or**
3. recurse and BucketSort the buckets again

![Bucket Sort](https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Bucket_sort_2.svg/311px-Bucket_sort_2.svg.png)

**Phase 3**

4. Gather: Visit the buckets in order and put all elements back into the original array.


## Complexity

| Name | Best | Average | Worst | Memory | Stable
| --------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
| **Bucket Sort** | n + k | n + k | n<sup>2</sup> | n | Yes

## References

- [Wikipedia](https://en.wikipedia.org/wiki/Counting_sort)
- [BigOCheatSheet](http://bigocheatsheet.com)

15 changes: 15 additions & 0 deletions src/algorithms/sorting/bucket-sort/_test_/BucketSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import BucketSort from '../BucketSort';
import {
SortTester,
} from '../../SortTester';


describe('BucketSort', () => {
it('should sort array', () => {
SortTester.testSort(BucketSort);
});

it('should sort negative numbers', () => {
SortTester.testNegativeNumbersSort(BucketSort);
});
});