From 473d64fa2ba9e5709d5e8e4001c2f0245e7f7d83 Mon Sep 17 00:00:00 2001 From: alpgc Date: Tue, 24 May 2016 17:47:48 -0300 Subject: [PATCH 1/3] Adding a Radix LSB --- algorithm/category.json | 3 +- algorithm/category.json~ | 49 ++++++++++++++++++++++++ algorithm/sorting/radixlsb/basic/code.js | 40 +++++++++++++++++++ algorithm/sorting/radixlsb/basic/data.js | 8 ++++ algorithm/sorting/radixlsb/desc.json | 13 +++++++ 5 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 algorithm/category.json~ create mode 100644 algorithm/sorting/radixlsb/basic/code.js create mode 100644 algorithm/sorting/radixlsb/basic/data.js create mode 100644 algorithm/sorting/radixlsb/desc.json diff --git a/algorithm/category.json b/algorithm/category.json index b4788499..ebc825e3 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", + "radixlsb" : "Radix LSB sort" } }, "string": { diff --git a/algorithm/category.json~ b/algorithm/category.json~ new file mode 100644 index 00000000..b4788499 --- /dev/null +++ b/algorithm/category.json~ @@ -0,0 +1,49 @@ +{ + "graph_search": { + "name": "Graph Search", + "list": { + "dfs": "DFS", + "bfs": "BFS", + "dijkstra": "Dijkstra", + "bellman_ford": "Bellman-Ford", + "floyd_warshall": "Floyd-Warshall", + "topological_sort": "Topological-Sort" + } + }, + "mst": { + "name": "Minimum Spanning Tree", + "list": { + "prim": "Prim's Algorithm" + } + }, + "search": { + "name": "Search", + "list": { + "binary_search": "Binary Search" + } + }, + "sorting": { + "name": "Sorting", + "list": { + "insertion": "Insertion Sort", + "selection": "Selection Sort", + "bubble": "Bubble Sort", + "quick": "Quicksort", + "merge": "Mergesort", + "heap" : "Heap Sort" + } + }, + "string": { + "name": "String", + "list": { + "edit_distance": "Edit Distance" + } + }, + "etc": { + "name": "Uncategorized", + "list": { + "dp": "Dynamic Programming", + "scratch_paper": " Scratch Paper" + } + } +} diff --git a/algorithm/sorting/radixlsb/basic/code.js b/algorithm/sorting/radixlsb/basic/code.js new file mode 100644 index 00000000..634e1f4a --- /dev/null +++ b/algorithm/sorting/radixlsb/basic/code.js @@ -0,0 +1,40 @@ +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 < 8;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(2, exp) % 2) ] += 1; + tracer._notify(2, parseInt( D[0][i] / pow(2, exp) % 2) ); + tracer._deselect(0, i); + } + D[2][1] += D[2][0]; + tracer._notify(2, 1); + for(var i = D[0].length - 1; i >= 0; i--){ + tracer._select(0, i); + D[2][parseInt( D[0][i] / pow(2, exp) % 2) ] -= 1; + tracer._notify(2, parseInt( D[0][i] / pow(2, exp) % 2) ); + D[1][ D[2][ parseInt( D[0][i] / pow(2, exp) % 2) ] ] = D[0][i]; + tracer._notify(1, D[2][ parseInt( D[0][i] / pow(2, exp) % 2) ] ); + 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); + } + D[2][0] = 0; + D[2][1] = 0; + tracer._notify(2, 0); + tracer._notify(2, 1); +} +tracer._print('sorted array = [' + D[0].join(', ') + ']'); diff --git a/algorithm/sorting/radixlsb/basic/data.js b/algorithm/sorting/radixlsb/basic/data.js new file mode 100644 index 00000000..813aa3a5 --- /dev/null +++ b/algorithm/sorting/radixlsb/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] +]; +tracer._setData(D); diff --git a/algorithm/sorting/radixlsb/desc.json b/algorithm/sorting/radixlsb/desc.json new file mode 100644 index 00000000..18b9437a --- /dev/null +++ b/algorithm/sorting/radixlsb/desc.json @@ -0,0 +1,13 @@ +{ + "Radix LSB Sort": "Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual bits 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" + } +} From d252a9a8553e5bdf3800f79854be461554daab44 Mon Sep 17 00:00:00 2001 From: alpgc Date: Tue, 24 May 2016 18:03:52 -0300 Subject: [PATCH 2/3] Fixing selection sort visualization problem --- algorithm/sorting/selection/basic/code.js | 7 ++++--- algorithm/sorting/selection/basic/code.js~ | 24 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 algorithm/sorting/selection/basic/code.js~ 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(', ') + ']'); diff --git a/algorithm/sorting/selection/basic/code.js~ b/algorithm/sorting/selection/basic/code.js~ new file mode 100644 index 00000000..8afe889a --- /dev/null +++ b/algorithm/sorting/selection/basic/code.js~ @@ -0,0 +1,24 @@ +tracer._print('original array = [' + D.join(', ') + ']'); +tracer._sleep(1000); +tracer._pace(500); +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.notify(j); + minJ = j; + } + tracer._deselect(j); + } + if (minJ != i) { + tracer._print('swap ' + D[i] + ' and ' + D[minJ]); + var temp = D[i]; + D[i] = D[minJ]; + D[minJ] = temp; + tracer._notify(i, minJ); + } + tracer._deselect(i); +} +tracer._print('sorted array = [' + D.join(', ') + ']'); From 90e1c719a12c87f04dd84c0f36a9d7f1e5797318 Mon Sep 17 00:00:00 2001 From: alpgc Date: Tue, 24 May 2016 18:54:46 -0300 Subject: [PATCH 3/3] Now, radix LSD, plus selction changed in a bit of visualz --- algorithm/category.json | 2 +- algorithm/category.json~ | 49 ------------------- algorithm/sorting/radixlsb/basic/code.js | 40 --------------- algorithm/sorting/radixlsd/basic/code.js | 44 +++++++++++++++++ .../{radixlsb => radixlsd}/basic/data.js | 2 +- .../sorting/{radixlsb => radixlsd}/desc.json | 2 +- algorithm/sorting/selection/basic/code.js~ | 24 --------- 7 files changed, 47 insertions(+), 116 deletions(-) delete mode 100644 algorithm/category.json~ delete mode 100644 algorithm/sorting/radixlsb/basic/code.js create mode 100644 algorithm/sorting/radixlsd/basic/code.js rename algorithm/sorting/{radixlsb => radixlsd}/basic/data.js (84%) rename algorithm/sorting/{radixlsb => radixlsd}/desc.json (67%) delete mode 100644 algorithm/sorting/selection/basic/code.js~ diff --git a/algorithm/category.json b/algorithm/category.json index ebc825e3..a6e1588d 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -31,7 +31,7 @@ "quick": "Quicksort", "merge": "Mergesort", "heap" : "Heap Sort", - "radixlsb" : "Radix LSB sort" + "radixlsd" : "Radix LSD sort" } }, "string": { diff --git a/algorithm/category.json~ b/algorithm/category.json~ deleted file mode 100644 index b4788499..00000000 --- a/algorithm/category.json~ +++ /dev/null @@ -1,49 +0,0 @@ -{ - "graph_search": { - "name": "Graph Search", - "list": { - "dfs": "DFS", - "bfs": "BFS", - "dijkstra": "Dijkstra", - "bellman_ford": "Bellman-Ford", - "floyd_warshall": "Floyd-Warshall", - "topological_sort": "Topological-Sort" - } - }, - "mst": { - "name": "Minimum Spanning Tree", - "list": { - "prim": "Prim's Algorithm" - } - }, - "search": { - "name": "Search", - "list": { - "binary_search": "Binary Search" - } - }, - "sorting": { - "name": "Sorting", - "list": { - "insertion": "Insertion Sort", - "selection": "Selection Sort", - "bubble": "Bubble Sort", - "quick": "Quicksort", - "merge": "Mergesort", - "heap" : "Heap Sort" - } - }, - "string": { - "name": "String", - "list": { - "edit_distance": "Edit Distance" - } - }, - "etc": { - "name": "Uncategorized", - "list": { - "dp": "Dynamic Programming", - "scratch_paper": " Scratch Paper" - } - } -} diff --git a/algorithm/sorting/radixlsb/basic/code.js b/algorithm/sorting/radixlsb/basic/code.js deleted file mode 100644 index 634e1f4a..00000000 --- a/algorithm/sorting/radixlsb/basic/code.js +++ /dev/null @@ -1,40 +0,0 @@ -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 < 8;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(2, exp) % 2) ] += 1; - tracer._notify(2, parseInt( D[0][i] / pow(2, exp) % 2) ); - tracer._deselect(0, i); - } - D[2][1] += D[2][0]; - tracer._notify(2, 1); - for(var i = D[0].length - 1; i >= 0; i--){ - tracer._select(0, i); - D[2][parseInt( D[0][i] / pow(2, exp) % 2) ] -= 1; - tracer._notify(2, parseInt( D[0][i] / pow(2, exp) % 2) ); - D[1][ D[2][ parseInt( D[0][i] / pow(2, exp) % 2) ] ] = D[0][i]; - tracer._notify(1, D[2][ parseInt( D[0][i] / pow(2, exp) % 2) ] ); - 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); - } - D[2][0] = 0; - D[2][1] = 0; - tracer._notify(2, 0); - tracer._notify(2, 1); -} -tracer._print('sorted array = [' + D[0].join(', ') + ']'); 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/radixlsb/basic/data.js b/algorithm/sorting/radixlsd/basic/data.js similarity index 84% rename from algorithm/sorting/radixlsb/basic/data.js rename to algorithm/sorting/radixlsd/basic/data.js index 813aa3a5..26e69e58 100644 --- a/algorithm/sorting/radixlsb/basic/data.js +++ b/algorithm/sorting/radixlsd/basic/data.js @@ -3,6 +3,6 @@ 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,0,0] ]; tracer._setData(D); diff --git a/algorithm/sorting/radixlsb/desc.json b/algorithm/sorting/radixlsd/desc.json similarity index 67% rename from algorithm/sorting/radixlsb/desc.json rename to algorithm/sorting/radixlsd/desc.json index 18b9437a..2e2bb9c7 100644 --- a/algorithm/sorting/radixlsb/desc.json +++ b/algorithm/sorting/radixlsd/desc.json @@ -1,5 +1,5 @@ { - "Radix LSB Sort": "Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual bits which share the same significant position and value.", + "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)" diff --git a/algorithm/sorting/selection/basic/code.js~ b/algorithm/sorting/selection/basic/code.js~ deleted file mode 100644 index 8afe889a..00000000 --- a/algorithm/sorting/selection/basic/code.js~ +++ /dev/null @@ -1,24 +0,0 @@ -tracer._print('original array = [' + D.join(', ') + ']'); -tracer._sleep(1000); -tracer._pace(500); -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.notify(j); - minJ = j; - } - tracer._deselect(j); - } - if (minJ != i) { - tracer._print('swap ' + D[i] + ' and ' + D[minJ]); - var temp = D[i]; - D[i] = D[minJ]; - D[minJ] = temp; - tracer._notify(i, minJ); - } - tracer._deselect(i); -} -tracer._print('sorted array = [' + D.join(', ') + ']');