-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountingSort.js
More file actions
76 lines (62 loc) · 2 KB
/
CountingSort.js
File metadata and controls
76 lines (62 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var HpcAlgorithms = HpcAlgorithms || {};
HpcAlgorithms.Sorting = (function()
{
var HistogramOneByteComponentUInt8 = function(inArray)
{
var numberOfBins = 256;
var count = new Array(numberOfBins);
for (var b = 0; b < numberOfBins; b++)
count[b] = 0;
for (var i = 0; i < inArray.length; i++)
count[ inArray[i] ]++;
return count;
}
/**
* Counting Sort of typed unsigned byte array
* This algorithm is in-place
* @param {Array of bytes} inputArray Array of numbers, which must be a typed array of unsigned bytes
* @return {Array of bytes} Sorted array of unsigned bytes
*/
var SortCountingUInt8 = function(inputArray)
{
var count = HistogramOneByteComponentUInt8(inputArray);
var startIndex = 0;
for (var countIndex = 0; countIndex < count.length; countIndex++)
{
inputArray.fill(countIndex, startIndex, startIndex + count[countIndex]); // 2X faster than using a for loop to fill an array
startIndex += count[countIndex];
}
return inputArray;
}
var HistogramOneWordComponent = function(inArray)
{
var numberOfBins = 65536;
var count = new Array(numberOfBins);
for (var b = 0; b < numberOfBins; b++)
count[b] = 0;
for (var current = 0; current < inArray.length; current++)
count[ inArray[current] ]++;
return count;
}
/**
* Counting Sort of typed unsigned short array typed array
* This algorithm is in-place
* @param {Array of unsigned short} inputArray Array of numbers, which must be a typed array of unsigned short
* @return {Array of unsigned short} Sorted array of unsigned short numbers
*/
var SortCountingUInt16 = function(inputArray)
{
var count = HistogramOneWordComponent(inputArray);
var startIndex = 0;
for (var countIndex = 0; countIndex < count.length; countIndex++)
{
inputArray.fill(countIndex, startIndex, startIndex + count[countIndex]);
startIndex += count[countIndex];
}
return inputArray;
}
return {
SortCountingUInt8: SortCountingUInt8,
SortCountingUInt16: SortCountingUInt16
};
})();