Sort an array of integers using Counting sort (where you only have integers and the min and max values are known)
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Javascript implementation of Counting sort

Sort an array of integers using Counting sort

Counting sort is the fastest sorting algorithm when the following criteria are met:

  • You only have integers
  • You know the minimum and the maximum value contained in the array

Note: Not only is it better to sort an integer array using Counting sort, it will actually not work if you try to sort anything else (like floats), so if things don't work, that's where you should be looking

Install using npm

npm install contingsort


var countingSort = require('countingsort');

var myArray = [5,3,2,0,6,4,3,6,10,0,3,2];

countingSort(myArray, 0, 10); // should sort the array to [0,0,2,2,3,3,3,4,5,6,6,10]

Note: Like the Array#sort() method in Javascript, it will not make a new array, but modify the one passed in.

Comparison to javascript Array#sort()

Counting sort on