Skip to content

Commit 8ee26c5

Browse files
committed
Updated Heapsort.js src code and test code -
1 parent 996615b commit 8ee26c5

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/sorting/heapsort.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @param {function} cmp Comparison function.
1919
*/
2020
function heapify(array, index, heapSize, cmp) {
21-
console.log("heapifying");
21+
//console.log("heapifying");
2222

2323
var left = 2 * index + 1;
2424
var right = 2 * index + 2;
@@ -39,7 +39,7 @@ console.log("heapifying");
3939
heapify(array, largest, heapSize, cmp);
4040
}
4141

42-
console.log(array);
42+
//console.log(array);
4343

4444

4545
}
@@ -55,6 +55,7 @@ console.log(array);
5555
function buildMaxHeap(array, cmp) {
5656
for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) {
5757
heapify(array, i, array.length, cmp);
58+
console.log(array);
5859
}
5960

6061
return array;
@@ -91,7 +92,7 @@ console.log(array);
9192
size -= 1;
9293
heapify(array, 0, size, cmp);
9394
}
94-
console.log(array);
95+
//console.log(array);
9596
return array;
9697
};
9798
}());

test/sorting/heapsort.spec.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

22
var arr = [];
3-
var MAX = 100;
3+
var MAX = 15;
44

55
var sort = require('./../../src/sorting/heapsort.js').heapSort;
66
//console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ]
77

88

9-
//for(var i = 0; i < MAX; ++i){
10-
// arr[i] = Math.floor((Math.random() * 100) + 1);
11-
// print(arr[i]);
12-
//}
9+
for(var i = 0; i < MAX; ++i){
10+
arr[i] = Math.floor((Math.random() * 50) + 1);
11+
// print(arr[i]);
12+
}
1313

14-
var array = [40,7,41,13,19,92,51,31];
14+
//var array = [40,7,41,13,19,92,51,31];
1515

1616

1717
function dispArr(arr) {
@@ -31,10 +31,16 @@ function dispArr(arr) {
3131

3232

3333

34+
35+
3436
//console.log(arr);
35-
dispArr(array);
36-
sort(array); //added print statements to it for testing
37+
dispArr(arr);
38+
sort(arr, (function(a,b){
39+
return b - a;
40+
}));
41+
42+
//added print statements to it for testing
3743
console.log("");
3844
//console.log(arr);
39-
dispArr(array);
45+
dispArr(arr);
4046

0 commit comments

Comments
 (0)