Skip to content

Commit 2e6f8ae

Browse files
authored
chore: Merge pull request TheAlgorithms#736 from charliejmoore/bucket-sort-tests
Bucket sort tests
2 parents 7e419f0 + fc07f38 commit 2e6f8ae

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

Sorts/BucketSort.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
2+
[Wikipedia says](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.): Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
33
elements of an array into a number of buckets. Each bucket is then sorted individually, either using
44
a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a
55
distribution sort, and is a cousin of radix sort in the most to least significant digit flavour.
@@ -11,6 +11,14 @@ Time Complexity of Solution:
1111
Best Case O(n); Average Case O(n); Worst Case O(n)
1212
1313
*/
14+
15+
/**
16+
* bucketSort returns an array of numbers sorted in increasing order.
17+
*
18+
* @param {number[]} list The array of numbers to be sorted.
19+
* @param {number} size The size of the buckets used. If not provided, size will be 5.
20+
* @return {number[]} An array of numbers sorted in increasing order.
21+
*/
1422
function bucketSort (list, size) {
1523
if (undefined === size) {
1624
size = 5
@@ -45,20 +53,12 @@ function bucketSort (list, size) {
4553
const sorted = []
4654
// now sort every bucket and merge it to the sorted list
4755
for (let iBucket = 0; iBucket < buckets.length; iBucket++) {
48-
const arr = buckets[iBucket].sort()
56+
const arr = buckets[iBucket].sort((a, b) => a - b)
4957
for (let iSorted = 0; iSorted < arr.length; iSorted++) {
5058
sorted.push(arr[iSorted])
5159
}
5260
}
5361
return sorted
5462
}
5563

56-
// Testing
57-
const arrOriginal = [5, 6, 7, 8, 1, 2, 12, 14]
58-
// > bucketSort(arrOriginal)
59-
// [1, 2, 5, 6, 7, 8, 12, 14]
60-
// Array before Sort
61-
console.log(arrOriginal)
62-
const arrSorted = bucketSort(arrOriginal)
63-
// Array after sort
64-
console.log(arrSorted)
64+
export { bucketSort }

Sorts/test/BucketSort.test.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { bucketSort } from '../BucketSort'
2+
3+
describe('Tests for bucketSort function', () => {
4+
it('should correctly sort an input list that is sorted backwards', () => {
5+
const array = [5, 4, 3, 2, 1]
6+
expect(bucketSort(array)).toEqual([1, 2, 3, 4, 5])
7+
})
8+
9+
it('should correctly sort an input list that is unsorted', () => {
10+
const array = [15, 24, 3, 2224, 1]
11+
expect(bucketSort(array)).toEqual([1, 3, 15, 24, 2224])
12+
})
13+
14+
describe('Variations of input array lengths', () => {
15+
it('should return an empty list with the input list is an empty list', () => {
16+
expect(bucketSort([])).toEqual([])
17+
})
18+
19+
it('should correctly sort an input list of length 1', () => {
20+
expect(bucketSort([100])).toEqual([100])
21+
})
22+
23+
it('should correctly sort an input list of an odd length', () => {
24+
expect(bucketSort([101, -10, 321])).toEqual([-10, 101, 321])
25+
})
26+
27+
it('should correctly sort an input list of an even length', () => {
28+
expect(bucketSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56])
29+
})
30+
})
31+
32+
describe('Variations of input array elements', () => {
33+
it('should correctly sort an input list that contains only positive numbers', () => {
34+
expect(bucketSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50])
35+
})
36+
37+
it('should correctly sort an input list that contains only negative numbers', () => {
38+
expect(bucketSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1])
39+
})
40+
41+
it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => {
42+
expect(bucketSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56])
43+
})
44+
45+
it('should correctly sort an input list that contains only whole numbers', () => {
46+
expect(bucketSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12])
47+
})
48+
49+
it('should correctly sort an input list that contains only decimal numbers', () => {
50+
expect(bucketSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45])
51+
})
52+
53+
it('should correctly sort an input list that contains only a mix of whole and decimal', () => {
54+
expect(bucketSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56])
55+
})
56+
57+
it('should correctly sort an input list that contains only fractional numbers', () => {
58+
expect(bucketSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98])
59+
})
60+
61+
it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => {
62+
expect(bucketSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12])
63+
})
64+
65+
it('should correctly sort an input list that contains duplicates', () => {
66+
expect(bucketSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4])
67+
})
68+
})
69+
})

0 commit comments

Comments
 (0)