Implemented counting sort · java66liu/AlgorithmVisualizer@cdb6d65 · GitHub
Skip to content

Commit cdb6d65

Browse files
committed
Implemented counting sort
1 parent bacbf0f commit cdb6d65

8 files changed

Lines changed: 69 additions & 7 deletions

File tree

algorithm/category.json

Lines changed: 1 addition & 0 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//set counts values
2+
for (let i = 0; i < A.length; i++) {
3+
tracer._select(0, i)._wait();
4+
counts[A[i]]++;
5+
tracer._notify(1, A[i], D[1][A[i]])._wait();
6+
tracer._deselect(0, i);
7+
tracer._denotify(1, A[i], D[1][A[i]])._wait();
8+
}
9+
10+
//sort
11+
var i = 0;
12+
for (var j = 0; j <= maxValue; j++) {
13+
while (counts[j] > 0) {
14+
tracer._select(1, j)._wait();
15+
sortedA[i] = j;
16+
counts[j]--;
17+
tracer._notify(1, j, D[1][j]);
18+
tracer._notify(2, i, D[2][i])._wait();
19+
tracer._deselect(1, j);
20+
tracer._denotify(1, j, D[1][j]);
21+
tracer._denotify(2, i, D[2][i])._wait();
22+
i++;
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var maxValue = 9;
2+
var arrSize = 10;
3+
4+
//initialize array values
5+
var A = Array1D.random(arrSize, 0, maxValue);
6+
var counts = [];
7+
var sortedA = [];
8+
for (let i = 0; i <= maxValue; i++) {
9+
counts[i] = 0;
10+
if (i < arrSize) sortedA[i] = 0;
11+
}
12+
var D = [
13+
A,
14+
counts,
15+
sortedA
16+
];
17+
18+
var tracer = new Array2DTracer();
19+
tracer._setData(D);
Lines changed: 13 additions & 0 deletions

public/algorithm_visualizer.js

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/algorithm_visualizer.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/algorithm_visualizer.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/algorithm_visualizer.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)