From 0a92acee35c8d4316b0bb5c57e4ad6240e0c14c9 Mon Sep 17 00:00:00 2001 From: Brian Capouch Date: Fri, 17 Apr 2015 13:50:01 -0400 Subject: [PATCH 1/6] Call heapsort to show how Node does it --- testHeap.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 testHeap.js diff --git a/testHeap.js b/testHeap.js new file mode 100644 index 00000000..880ce4d8 --- /dev/null +++ b/testHeap.js @@ -0,0 +1,25 @@ + function dispArr(arr) { + for (var i = 0; i < arr.length; ++i) { + process.stdout.write(arr[i] + " "); + if (i % 10 == 9) { + process.stdout.write("\n"); + } + } + if (i % 10 != 0) { + process.stdout.write("\n"); + } + } + +var MAX = 50 +var sort = require('./src/sorting/heapsort.js').heapSort; +var values = []; +for (var i = 0; i < MAX; ++i) { + values[i] = Math.floor(Math.random() * MAX+1); + } + +console.log('\nBefore: '); +dispArr(values); +console.log('\nAfter: '); + +dispArr(sort(values)); + From 2c2188eea8833be307e44e81eafa9185f517eb84 Mon Sep 17 00:00:00 2001 From: Brian Capouch Date: Sat, 25 Apr 2015 05:16:23 -0400 Subject: [PATCH 2/6] Latest class-time changes --- src/sorting/heapsort.js | 23 ++++++++++++++++++----- testHeap.js | 19 ++++++++++++------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/sorting/heapsort.js b/src/sorting/heapsort.js index 765adc3a..ba7636d0 100644 --- a/src/sorting/heapsort.js +++ b/src/sorting/heapsort.js @@ -17,7 +17,7 @@ * @param {Number} heapSize Size of the heap. * @param {function} cmp Comparison function. */ - function heapify(array, index, heapSize, cmp) { + function heapify(array, index, heapSize, cmp, debug) { var left = 2 * index + 1; var right = 2 * index + 2; var largest = index; @@ -31,9 +31,13 @@ } if (largest !== index) { + // DEBUG + if (debug) + console.log('Swapping ' + array[largest] + ' and ' + array[index]); var temp = array[index]; array[index] = array[largest]; array[largest] = temp; +//console.log('Calling heapify recursively'); heapify(array, largest, heapSize, cmp); } } @@ -46,8 +50,10 @@ * @param {function} cmp Comparison function. * @return {Array} array Array turned into max heap. */ - function buildMaxHeap(array, cmp) { + function buildMaxHeap(array, cmp, debug) { +console.log('Value of debug is ' + debug); for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) { +//console.log('Calling heapify from for loop'); heapify(array, i, array.length, cmp); } return array; @@ -72,17 +78,24 @@ * zero, or positive value, depending on the arguments. * @return {Array} Sorted array. */ - return function (array, cmp) { + return function (array, cmp, debug) { +console.log('Swaps during initial heap construction:'); cmp = cmp || comparator; +// debug = debug || false; +// value is set to the parameter which was passed, or if there was none +// then set it to the RHS of the | var size = array.length; var temp; - buildMaxHeap(array, cmp); + buildMaxHeap(array, cmp, debug); + +console.log('Heap is built; calls below are adjusting:'); for (var i = array.length - 1; i > 0; i -= 1) { temp = array[0]; +// console.log('Pulling ' + temp); array[0] = array[i]; array[i] = temp; size -= 1; - heapify(array, 0, size, cmp); + heapify(array, 0, size, cmp, debug); } return array; }; diff --git a/testHeap.js b/testHeap.js index 880ce4d8..9d404d99 100644 --- a/testHeap.js +++ b/testHeap.js @@ -10,16 +10,21 @@ } } -var MAX = 50 +//var MAX = 50 var sort = require('./src/sorting/heapsort.js').heapSort; -var values = []; -for (var i = 0; i < MAX; ++i) { - values[i] = Math.floor(Math.random() * MAX+1); - } +//var values = []; +//for (var i = 0; i < MAX; ++i) { +// values[i] = Math.floor(Math.random() * MAX+1); +// } +// +values = [19,4,38,26,14,73,3,83,42,6]; console.log('\nBefore: '); dispArr(values); -console.log('\nAfter: '); -dispArr(sort(values)); +console.log('\nAfter: '); +dispArr(sort(values, function(a,b) { + return b - a; +} +),true); From cbaa14f03a6602b327353065ca7df5290dd12b3b Mon Sep 17 00:00:00 2001 From: Brian Capouch Date: Sat, 25 Apr 2015 05:19:11 -0400 Subject: [PATCH 3/6] Fix location of debug parameter --- testHeap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testHeap.js b/testHeap.js index 9d404d99..2e8b325d 100644 --- a/testHeap.js +++ b/testHeap.js @@ -26,5 +26,5 @@ console.log('\nAfter: '); dispArr(sort(values, function(a,b) { return b - a; } -),true); +,true)); From 78ad63198a2c92ddc74659bf8ce6d86416da50ea Mon Sep 17 00:00:00 2001 From: Brian Capouch Date: Sat, 25 Apr 2015 14:19:37 -0400 Subject: [PATCH 4/6] Fully implement debug flag --- src/sorting/heapsort.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/sorting/heapsort.js b/src/sorting/heapsort.js index ba7636d0..bcc5d9e3 100644 --- a/src/sorting/heapsort.js +++ b/src/sorting/heapsort.js @@ -37,7 +37,8 @@ var temp = array[index]; array[index] = array[largest]; array[largest] = temp; -//console.log('Calling heapify recursively'); + //if(debug) + // console.log('Calling heapify recursively'); heapify(array, largest, heapSize, cmp); } } @@ -51,9 +52,9 @@ * @return {Array} array Array turned into max heap. */ function buildMaxHeap(array, cmp, debug) { -console.log('Value of debug is ' + debug); for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) { -//console.log('Calling heapify from for loop'); + if (debug) + console.log('Calling heapify from for loop'); heapify(array, i, array.length, cmp); } return array; @@ -79,19 +80,18 @@ console.log('Value of debug is ' + debug); * @return {Array} Sorted array. */ return function (array, cmp, debug) { -console.log('Swaps during initial heap construction:'); + if (debug) + console.log('Swaps during initial heap construction:'); cmp = cmp || comparator; -// debug = debug || false; -// value is set to the parameter which was passed, or if there was none -// then set it to the RHS of the | + debug = debug || false; var size = array.length; var temp; buildMaxHeap(array, cmp, debug); -console.log('Heap is built; calls below are adjusting:'); + if (debug) + console.log('Heap is built; calls below are adjusting:'); for (var i = array.length - 1; i > 0; i -= 1) { temp = array[0]; -// console.log('Pulling ' + temp); array[0] = array[i]; array[i] = temp; size -= 1; From fa91a722d630351f40b40c2d6cf0e4fe3880476e Mon Sep 17 00:00:00 2001 From: Brian Capouch Date: Sun, 3 May 2015 19:49:06 -0400 Subject: [PATCH 5/6] Fix misplaced use of debug param --- src/sorting/heapsort.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sorting/heapsort.js b/src/sorting/heapsort.js index bcc5d9e3..e68cd0ab 100644 --- a/src/sorting/heapsort.js +++ b/src/sorting/heapsort.js @@ -80,10 +80,10 @@ * @return {Array} Sorted array. */ return function (array, cmp, debug) { - if (debug) - console.log('Swaps during initial heap construction:'); cmp = cmp || comparator; debug = debug || false; + if (debug) + console.log('Swaps during initial heap construction:'); var size = array.length; var temp; buildMaxHeap(array, cmp, debug); From 73651f48d1a2cec20be73c8179c2caa256dbf1e3 Mon Sep 17 00:00:00 2001 From: Brian Capouch Date: Mon, 4 May 2015 02:28:57 -0400 Subject: [PATCH 6/6] Revert back to original operation --- testHeap.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/testHeap.js b/testHeap.js index 2e8b325d..fd68579d 100644 --- a/testHeap.js +++ b/testHeap.js @@ -10,21 +10,18 @@ } } -//var MAX = 50 +var MAX = 50 var sort = require('./src/sorting/heapsort.js').heapSort; -//var values = []; -//for (var i = 0; i < MAX; ++i) { -// values[i] = Math.floor(Math.random() * MAX+1); -// } -// -values = [19,4,38,26,14,73,3,83,42,6]; +var values = []; +for (var i = 0; i < MAX; ++i) { + values[i] = Math.floor(Math.random() * MAX+1); + } + +//values = [19,4,38,26,14,73,3,83,42,6]; console.log('\nBefore: '); dispArr(values); console.log('\nAfter: '); -dispArr(sort(values, function(a,b) { - return b - a; -} -,true)); +dispArr(sort(values));