Skip to content

Commit

Permalink
Add countingSort.sortWithMinMax(array, min, max) called through sort,…
Browse files Browse the repository at this point in the history
… added tests. Minor optimisation to counting sort. Bumped minor version number.
  • Loading branch information
Tyriar committed Jun 22, 2014
1 parent 4deec31 commit 69d5987
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-sorting",
"version": "1.0.0",
"version": "1.1.0",
"homepage": "https://github.com/Tyriar/js-sorting",
"authors": [
"Daniel Imms (http://www.growingwiththeweb.com)"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-sorting",
"version": "1.0.0",
"version": "1.1.0",
"description": "A collection of sorting algorithms written in JavaScript.",
"devDependencies": {
"jasmine-node": "~1.14.3"
Expand Down
53 changes: 47 additions & 6 deletions src/counting-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,65 @@
sort: sort
};

// maxValue is optional, specifying it will initalise the array to the correct
// size
function sort(array, maxValue) {
// Routes function calls to the correct internal method, call like:
// sort(array, maxValue)
// sort(array, minValue, maxValue)
function sort() {
if (arguments.length === 2) {
return sortWithMax.apply(null, arguments);
}
if (arguments.length === 3) {
return sortWithMinAndMax.apply(null, arguments);
}
throw "Cannot sort with counting sort with " + arguments.length +
" arguments";
}

function sortWithMax(array, maxValue) {
var buckets = new Array(maxValue + 1);
var sortedIndex = 0;
var i;

for (i = 0; i < array.length; i++) {
if (!buckets[array[i]]) {
buckets[array[i]] = 0;
}
buckets[array[i]]++;
}

for (i = 0; i < buckets.length; i++) {
buckets[i] = 0;
while (buckets[i] > 0) {
array[sortedIndex++] = i;
buckets[i]--;
}
}

return array;
}

function sortWithMinAndMax(array, minValue, maxValue) {
if (array.length === 0) {
return array;
}

var rangeSize = maxValue - minValue;
var buckets = new Array(rangeSize);
var sortedIndex = 0;
var i;

for (i = 0; i < array.length; i++) {
buckets[array[i]]++;
// Change the value to a zero-based index
var bucketIndex = array[i] - minValue;
if (!buckets[bucketIndex]) {
buckets[bucketIndex] = 0;
}
buckets[bucketIndex]++;
}

for (i = 0; i < buckets.length; i++) {
while (buckets[i] > 0) {
array[sortedIndex++] = i;
// Change the index to the correct value
array[sortedIndex++] = i + minValue;
buckets[i]--;
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/counting-sort-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ describe("counting-sort", function () {
}
});

// Test sort(array, minValue, maxValue) for arrays with a specific range.
describe("counting-sort", function () {
for (var i = 0; i < testHelper.tests.length; i++) {
(function (test) {
var sorted = testHelper.getSorted(test);
var original = testHelper.getOriginal(test);
var minValue = sorted[0];
var maxValue = sorted[sorted.length - 1];

it(test.it, function () {
expect(algorithm.sort(original, minValue, maxValue))
.toEqual(sorted);
});
})(testHelper.tests[i]);
}
});

0 comments on commit 69d5987

Please sign in to comment.