From f71afaefd356723d8638585b9f2ed8c8578388ad Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 14 Apr 2015 23:38:17 -0400 Subject: [PATCH 01/11] Has an array that holds 100 random elements 1-100 --- test/sorting/heapsort.spec.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/test/sorting/heapsort.spec.js b/test/sorting/heapsort.spec.js index 2546a007..b5d363e7 100644 --- a/test/sorting/heapsort.spec.js +++ b/test/sorting/heapsort.spec.js @@ -1,4 +1,19 @@ -var sortTestCase = require('./sort.testcase.js'); -var heapSort = require('../../src/sorting/heapsort.js').heapSort; +//var sortTestCase = require('./sort.testcase.js'); +//var heapSort = require('../../src/sorting/heapsort.js').heapSort; + +//sortTestCase(heapSort, 'Heap sort'); + + + + +//load('heapsort.js'); + +var arr = []; +var MAX = 100 + +for(var i = 0; i < MAX; ++i){ + arr[i] = Math.floor((Math.random() * 100) + 1); + // print(arr[i]); +} +print(arr); -sortTestCase(heapSort, 'Heap sort'); From 0e69f66cef0abffeba00bf908dce36bc53d2841a Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 14 Apr 2015 23:59:50 -0400 Subject: [PATCH 02/11] Added more to heapsort still doesnt work --- test/sorting/heapsort.spec.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/sorting/heapsort.spec.js b/test/sorting/heapsort.spec.js index b5d363e7..1fd3161f 100644 --- a/test/sorting/heapsort.spec.js +++ b/test/sorting/heapsort.spec.js @@ -1,5 +1,5 @@ //var sortTestCase = require('./sort.testcase.js'); -//var heapSort = require('../../src/sorting/heapsort.js').heapSort; +var heapSort = require('../../src/sorting/heapsort.js'); //sortTestCase(heapSort, 'Heap sort'); @@ -17,3 +17,6 @@ for(var i = 0; i < MAX; ++i){ } print(arr); + + +heapSort(arr); From 2b1b13f241904533ed174835bb7a943f18af2aba Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 15 Apr 2015 13:47:19 -0400 Subject: [PATCH 03/11] Updated to test heapsort, just needs to display array better --- test/sorting/heapsort.spec.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/test/sorting/heapsort.spec.js b/test/sorting/heapsort.spec.js index 1fd3161f..0dd110db 100644 --- a/test/sorting/heapsort.spec.js +++ b/test/sorting/heapsort.spec.js @@ -1,5 +1,5 @@ //var sortTestCase = require('./sort.testcase.js'); -var heapSort = require('../../src/sorting/heapsort.js'); +//var heapSort = require('../../src/sorting/heapsort.js'); //sortTestCase(heapSort, 'Heap sort'); @@ -8,15 +8,33 @@ var heapSort = require('../../src/sorting/heapsort.js'); //load('heapsort.js'); +//var arr = []; +//var MAX = 100 + +//for(var i = 0; i < MAX; ++i){ +// arr[i] = Math.floor((Math.random() * 100) + 1); + // print(arr[i]); +//} +//print(arr); + + + +//heapSort(arr); + + var arr = []; -var MAX = 100 +var MAX = 100; + +var sort = require('./../../src/sorting/heapsort.js').heapSort; +console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ] + for(var i = 0; i < MAX; ++i){ - arr[i] = Math.floor((Math.random() * 100) + 1); - // print(arr[i]); + arr[i] = Math.floor((Math.random() * 100) + 1); + // print(arr[i]); } -print(arr); - +console.log(arr); +sort(arr); +console.log(arr); -heapSort(arr); From e9c018d5b63f9953d78e7b39be1e223e683cfda5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 16 Apr 2015 19:12:42 -0400 Subject: [PATCH 04/11] Updated to have working display and use of heapsort --- test/sorting/heapsort.spec.js | 49 ++++++++++++++++------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/test/sorting/heapsort.spec.js b/test/sorting/heapsort.spec.js index 0dd110db..787b7e19 100644 --- a/test/sorting/heapsort.spec.js +++ b/test/sorting/heapsort.spec.js @@ -1,26 +1,3 @@ -//var sortTestCase = require('./sort.testcase.js'); -//var heapSort = require('../../src/sorting/heapsort.js'); - -//sortTestCase(heapSort, 'Heap sort'); - - - - -//load('heapsort.js'); - -//var arr = []; -//var MAX = 100 - -//for(var i = 0; i < MAX; ++i){ -// arr[i] = Math.floor((Math.random() * 100) + 1); - // print(arr[i]); -//} -//print(arr); - - - -//heapSort(arr); - var arr = []; var MAX = 100; @@ -33,8 +10,28 @@ for(var i = 0; i < MAX; ++i){ arr[i] = Math.floor((Math.random() * 100) + 1); // print(arr[i]); } -console.log(arr); -sort(arr); -console.log(arr); +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"); + } +} + + + + + + +//console.log(arr); +dispArr(arr); +sort(arr); +console.log(""); +//console.log(arr); +dispArr(arr); From bd9927039cfde30e62b5c6561a06b36bc97f189e Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 20 Apr 2015 13:43:25 -0400 Subject: [PATCH 05/11] Includes testHeap.js --- testHeap.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 testHeap.js diff --git a/testHeap.js b/testHeap.js new file mode 100644 index 00000000..057ac7c2 --- /dev/null +++ b/testHeap.js @@ -0,0 +1,24 @@ + 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 a4dbc68e3cc7207551211dbc7fa19e011317734b Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 21 Apr 2015 23:46:56 -0400 Subject: [PATCH 06/11] Updated to run array[40-7-41-13-19-92-51-31] --- test/sorting/heapsort.spec.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/sorting/heapsort.spec.js b/test/sorting/heapsort.spec.js index 787b7e19..383f78b5 100644 --- a/test/sorting/heapsort.spec.js +++ b/test/sorting/heapsort.spec.js @@ -3,13 +3,16 @@ var arr = []; var MAX = 100; var sort = require('./../../src/sorting/heapsort.js').heapSort; -console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ] +//console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ] -for(var i = 0; i < MAX; ++i){ - arr[i] = Math.floor((Math.random() * 100) + 1); +//for(var i = 0; i < MAX; ++i){ + // arr[i] = Math.floor((Math.random() * 100) + 1); // print(arr[i]); -} +//} + +var array = [40,7,41,13,19,92,51,31]; + function dispArr(arr) { for (var i = 0; i < arr.length; ++i) { @@ -29,9 +32,9 @@ function dispArr(arr) { //console.log(arr); -dispArr(arr); -sort(arr); +dispArr(array); +sort(array); //added print statements to it for testing console.log(""); //console.log(arr); -dispArr(arr); +dispArr(array); From d887f98f621b3e43d0e6df10a901a403d3710b24 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 22 Apr 2015 00:08:45 -0400 Subject: [PATCH 07/11] UPdated to have prints for testing heapsort --- src/sorting/heapsort.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/sorting/heapsort.js b/src/sorting/heapsort.js index 765adc3a..7aa93f0e 100644 --- a/src/sorting/heapsort.js +++ b/src/sorting/heapsort.js @@ -18,6 +18,8 @@ * @param {function} cmp Comparison function. */ function heapify(array, index, heapSize, cmp) { +console.log("heapifying"); + var left = 2 * index + 1; var right = 2 * index + 2; var largest = index; @@ -36,6 +38,10 @@ array[largest] = temp; heapify(array, largest, heapSize, cmp); } + +console.log(array); + + } /** @@ -50,6 +56,7 @@ for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) { heapify(array, i, array.length, cmp); } + return array; } @@ -84,6 +91,7 @@ size -= 1; heapify(array, 0, size, cmp); } +console.log(array); return array; }; }()); @@ -91,3 +99,9 @@ exports.heapSort = heapSort; })(typeof window === 'undefined' ? module.exports : window); + + + + + + From 996615bdb249f6e263d498297b63ede4b097cce4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 22 Apr 2015 13:07:46 -0400 Subject: [PATCH 08/11] updated with Readme --- README.txt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 README.txt diff --git a/README.txt b/README.txt new file mode 100644 index 00000000..a6decf90 --- /dev/null +++ b/README.txt @@ -0,0 +1,9 @@ +ANDREW KLUMP + +CMP344 + +Testing heapsort + +uses /src/sorting/heapsort.js + +use node heapsort.spec.js in the folder test/sorting/ From 8ee26c58e58bd198caa75b0b0d8f7dc33ea5e8af Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 4 May 2015 15:43:30 -0400 Subject: [PATCH 09/11] Updated Heapsort.js src code and test code - --- src/sorting/heapsort.js | 7 ++++--- test/sorting/heapsort.spec.js | 24 +++++++++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/sorting/heapsort.js b/src/sorting/heapsort.js index 7aa93f0e..af6323bb 100644 --- a/src/sorting/heapsort.js +++ b/src/sorting/heapsort.js @@ -18,7 +18,7 @@ * @param {function} cmp Comparison function. */ function heapify(array, index, heapSize, cmp) { -console.log("heapifying"); +//console.log("heapifying"); var left = 2 * index + 1; var right = 2 * index + 2; @@ -39,7 +39,7 @@ console.log("heapifying"); heapify(array, largest, heapSize, cmp); } -console.log(array); +//console.log(array); } @@ -55,6 +55,7 @@ console.log(array); function buildMaxHeap(array, cmp) { for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) { heapify(array, i, array.length, cmp); +console.log(array); } return array; @@ -91,7 +92,7 @@ console.log(array); size -= 1; heapify(array, 0, size, cmp); } -console.log(array); +//console.log(array); return array; }; }()); diff --git a/test/sorting/heapsort.spec.js b/test/sorting/heapsort.spec.js index 383f78b5..aac58011 100644 --- a/test/sorting/heapsort.spec.js +++ b/test/sorting/heapsort.spec.js @@ -1,17 +1,17 @@ var arr = []; -var MAX = 100; +var MAX = 15; var sort = require('./../../src/sorting/heapsort.js').heapSort; //console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ] -//for(var i = 0; i < MAX; ++i){ - // arr[i] = Math.floor((Math.random() * 100) + 1); - // print(arr[i]); -//} +for(var i = 0; i < MAX; ++i){ + arr[i] = Math.floor((Math.random() * 50) + 1); + // print(arr[i]); +} -var array = [40,7,41,13,19,92,51,31]; +//var array = [40,7,41,13,19,92,51,31]; function dispArr(arr) { @@ -31,10 +31,16 @@ function dispArr(arr) { + + //console.log(arr); -dispArr(array); -sort(array); //added print statements to it for testing +dispArr(arr); +sort(arr, (function(a,b){ + return b - a; +})); + +//added print statements to it for testing console.log(""); //console.log(arr); -dispArr(array); +dispArr(arr); From f999bca4414c2777776e01e0bc6bfeaf2adfc97e Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 4 May 2015 15:47:36 -0400 Subject: [PATCH 10/11] Updated FINAL AND README!!!!! --- FINAL_TEST_README.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 FINAL_TEST_README.txt diff --git a/FINAL_TEST_README.txt b/FINAL_TEST_README.txt new file mode 100644 index 00000000..09d89c8a --- /dev/null +++ b/FINAL_TEST_README.txt @@ -0,0 +1,12 @@ +Andrew Klump + +Algorithms in Operation + +Problem number 1. + +Algorithm Code - Go into src/sorting/ for heapsort.js algorithm code. Has only the print outs to show what we want of heapifying but not sorting + +RUN Code - Go to test/sorting/heapsort.specs.js + run heapsort.specs.js using node + + From db0be3c4f71d6e3e0cda44c1dcb4db49bda40fca Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 4 May 2015 15:51:47 -0400 Subject: [PATCH 11/11] UPDATED WITH COMMENTS --- src/sorting/heapsort.js | 2 ++ test/sorting/heapsort.spec.js | 1 + 2 files changed, 3 insertions(+) diff --git a/src/sorting/heapsort.js b/src/sorting/heapsort.js index af6323bb..7a558975 100644 --- a/src/sorting/heapsort.js +++ b/src/sorting/heapsort.js @@ -55,6 +55,8 @@ function buildMaxHeap(array, cmp) { for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) { heapify(array, i, array.length, cmp); + +//BUILD MAX HEAP IS WHERE IT BUILDS THE HEAP BUT NOTHING ELSE! console.log(array); } diff --git a/test/sorting/heapsort.spec.js b/test/sorting/heapsort.spec.js index aac58011..af7421aa 100644 --- a/test/sorting/heapsort.spec.js +++ b/test/sorting/heapsort.spec.js @@ -35,6 +35,7 @@ function dispArr(arr) { //console.log(arr); dispArr(arr); +//ADDED anonymous function so now it will be sorted backwards sort(arr, (function(a,b){ return b - a; }));