Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Bucket Sort #49

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/algorithms/sorting/bucketSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const insertionSort = require("./insertionSort");

function bucketSort(arr, bucketSize = 4) {
let minValue = arr.reduce((min, p) => (p < min ? p : min), arr[0]);
let maxValue = arr.reduce((max, p) => (p > max ? p : max), arr[0]);

let buckets = [];
if (arr.length === 0) {
return arr;
}
let bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
for (let i = 0; i < bucketCount; i++) {
buckets.push([]);
}
for (let i = 0; i < arr.length; i++) {
let index_b = Math.floor((arr[i] - minValue) / bucketSize);
buckets[index_b].push(arr[i]);
}
for (let i = 0; i < bucketCount; i++) {
buckets[i] = insertionSort(buckets[i]);
}
let k = 0;
for (let i = 0; i < bucketCount; i++) {
for (let j = 0; j < buckets[i].length; j++) {
arr[k] = buckets[i][j];
k++;
}
}
return arr;
}

module.exports = bucketSort;
18 changes: 18 additions & 0 deletions src/algorithms/sorting/insertionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function insertionSort(arr) {
if (arr.length === 0) {
return arr;
}
for (let i = 1; i < arr.length; i++) {
let maxValue = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > maxValue) {
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = maxValue;
}
arr[j + 1] = maxValue;
}
return arr;
}

module.exports = insertionSort;
34 changes: 34 additions & 0 deletions test/algorithms/sorting/bucketSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const bucketSort = require("../../../src/algorithms/sorting/bucketSort");
const compareArrays = require("../../helpers/compareArrays");

describe("Bucket Sort Algorithm", () => {
it("Sorted array remains in order", () => {
const arr1 = [1, 2, 3, 4, 5];
const arr2 = bucketSort(arr1);
expect(compareArrays(arr1, arr2)).toBe(true);
});

it("Unsorted array is sorted", () => {
const arr1 = [5, 2, 3, 1, 4];
const arr2 = bucketSort(arr1);
expect(compareArrays([1, 2, 3, 4, 5], arr2)).toBe(true);
});

it("Single item array is unchanged", () => {
const arr1 = [5];
const arr2 = bucketSort(arr1);
expect(compareArrays(arr1, arr2)).toBe(true);
});

it("Empty array is unchanged", () => {
const arr1 = [];
const arr2 = bucketSort(arr1);
expect(compareArrays(arr1, arr2)).toBe(true);
});

it("Negative array is sorted", () => {
const arr1 = [-5, -1, -10, -15, -20];
const arr2 = bucketSort(arr1);
expect(compareArrays([-20, -15, -10, -5, -1], arr2)).toBe(true);
});
});
34 changes: 34 additions & 0 deletions test/algorithms/sorting/insertionSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const insertionSort = require("../../../src/algorithms/sorting/insertionSort");
const compareArrays = require("../../helpers/compareArrays");

describe("Insertion Sort Algorithm", () => {
it("Sorted array remains in order", () => {
const arr1 = [1, 2, 3, 4, 5];
const arr2 = insertionSort(arr1);
expect(compareArrays(arr1, arr2)).toBe(true);
});

it("Unsorted array is sorted", () => {
const arr1 = [5, 2, 3, 1, 4];
const arr2 = insertionSort(arr1);
expect(compareArrays([1, 2, 3, 4, 5], arr2)).toBe(true);
});

it("Single item array is unchanged", () => {
const arr1 = [5];
const arr2 = insertionSort(arr1);
expect(compareArrays(arr1, arr2)).toBe(true);
});

it("Empty array is unchanged", () => {
const arr1 = [];
const arr2 = insertionSort(arr1);
expect(compareArrays(arr1, arr2)).toBe(true);
});

it("Negative array is sorted", () => {
const arr1 = [-5, -1, -10, -15, -20];
const arr2 = insertionSort(arr1);
expect(compareArrays([-20, -15, -10, -5, -1], arr2)).toBe(true);
});
});