Skip to content

Adding a Radix LSD #43

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

Merged
merged 3 commits into from
May 24, 2016
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
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"bubble": "Bubble Sort",
"quick": "Quicksort",
"merge": "Mergesort",
"heap" : "Heap Sort"
"heap" : "Heap Sort",
"radixlsd" : "Radix LSD sort"
}
},
"string": {
Expand Down
44 changes: 44 additions & 0 deletions algorithm/sorting/radixlsd/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
tracer._print('original array = [' + D[0].join(', ') + ']');
tracer._sleep(1000);
tracer._pace(300);
function pow(base, expo){
var ans = 1;
for(var i = 0; i < expo;i++){
ans *= base;
}
return ans;
}
for(var exp = 0; exp < 6;exp ++){
tracer._print("Bit "+exp);
for(var i = 0; i < D[0].length; i++){
tracer._select(0, i);
D[2][ parseInt( D[0][i] / pow(10, exp) % 10) ] += 1;
tracer._notify(2, parseInt( D[0][i] / pow(10, exp) % 10) );
tracer._deselect(0, i);
}
for(var i = 1; i < 10; i++){
tracer._select(2, i - 1);
D[2][i] += D[2][i - 1];
tracer._notify(2, i);
tracer._deselect(2, i - 1);
}
for(var i = D[0].length - 1; i >= 0; i--){
tracer._select(0, i);
D[2][parseInt( D[0][i] / pow(10, exp) % 10) ] -= 1;
tracer._notify(2, parseInt( D[0][i] / pow(10, exp) % 10) );
D[1][ D[2][ parseInt( D[0][i] / pow(10, exp) % 10) ] ] = D[0][i];
tracer._notify(1, D[2][ parseInt( D[0][i] / pow(10, exp) % 10) ] );
tracer._deselect(0, i);
}
for(var i = 0; i < D[0].length; i++){
tracer._select(1, i);
D[0][i] = D[1][i];
tracer._notify(0, i);
tracer._deselect(1, i);
}
for(var i = 0; i < 10; i++){
D[2][i] = 0;
tracer._notify(2, i);
}
}
tracer._print('sorted array = [' + D[0].join(', ') + ']');
8 changes: 8 additions & 0 deletions algorithm/sorting/radixlsd/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var tracer = new Array2DTracer();
var k = [5,4,3,5,7,5,6,9];
var D = [
k,
Array1D.random(k.length),
[0,0,0,0,0,0,0,0,0,0]
];
tracer._setData(D);
13 changes: 13 additions & 0 deletions algorithm/sorting/radixlsd/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Radix LSD Sort": "Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.",
"Complexity": {
"time": "worst O(n), best O(n), average O(n)",
"space": "always O(n)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Radix_sort'>Wikipedia</a>"
],
"files": {
"basic": "Basic"
}
}
7 changes: 4 additions & 3 deletions algorithm/sorting/selection/basic/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ for (var i = 0; i < D.length - 1; i++) {
var minJ = i;
tracer._select(i);
for (var j = i + 1; j < D.length; j++) {
tracer._select(j);
if (D[j] < D[minJ]) {
tracer._select(j);
tracer._notify(j);
minJ = j;
tracer._deselect(j);
}
tracer._deselect(j);
}
if (minJ != i) {
tracer._print('swap ' + D[i] + ' and ' + D[minJ]);
Expand All @@ -20,4 +21,4 @@ for (var i = 0; i < D.length - 1; i++) {
}
tracer._deselect(i);
}
tracer._print('sorted array = [' + D.join(', ') + ']');
tracer._print('sorted array = [' + D.join(', ') + ']');