diff --git a/algorithm/category.json b/algorithm/category.json index b4788499..a6e1588d 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -30,7 +30,8 @@ "bubble": "Bubble Sort", "quick": "Quicksort", "merge": "Mergesort", - "heap" : "Heap Sort" + "heap" : "Heap Sort", + "radixlsd" : "Radix LSD sort" } }, "string": { diff --git a/algorithm/sorting/radixlsd/basic/code.js b/algorithm/sorting/radixlsd/basic/code.js new file mode 100644 index 00000000..1dc8aba9 --- /dev/null +++ b/algorithm/sorting/radixlsd/basic/code.js @@ -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(', ') + ']'); diff --git a/algorithm/sorting/radixlsd/basic/data.js b/algorithm/sorting/radixlsd/basic/data.js new file mode 100644 index 00000000..26e69e58 --- /dev/null +++ b/algorithm/sorting/radixlsd/basic/data.js @@ -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); diff --git a/algorithm/sorting/radixlsd/desc.json b/algorithm/sorting/radixlsd/desc.json new file mode 100644 index 00000000..2e2bb9c7 --- /dev/null +++ b/algorithm/sorting/radixlsd/desc.json @@ -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": [ + "Wikipedia" + ], + "files": { + "basic": "Basic" + } +} diff --git a/algorithm/sorting/selection/basic/code.js b/algorithm/sorting/selection/basic/code.js index efeefe59..f82b88e8 100644 --- a/algorithm/sorting/selection/basic/code.js +++ b/algorithm/sorting/selection/basic/code.js @@ -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]); @@ -20,4 +21,4 @@ for (var i = 0; i < D.length - 1; i++) { } tracer._deselect(i); } -tracer._print('sorted array = [' + D.join(', ') + ']'); \ No newline at end of file +tracer._print('sorted array = [' + D.join(', ') + ']');