"use strict"; /* * compare heap sort with build to heap sort with sequence of inserts * the different is slight with random input values ( genRandomElts ) * the difference is larger with bad data... unfortunate, worst case input sequences * ( genOrderedElts ) * * bubble sort included for comparison */ myMain(); var heapFillTime ; var totSortTime; function myMain() { //var nums= [12,3,51,13,-8.5,25,101,4.3,44,-10,25.3, -17,0,0.01,551,308,17,2]; //var nums= [21,32,-19,4,71,-3,17,0,8,11,54]; //var N=3770000, MAX=900000000; var nums; var st, et, sortedNums; var N=120000, MAX=900000000; // for bubblesort use small... 100000 // pick your data... random or "bad" ordered //var BAD_DATA = true; var BAD_DATA = false; // bubble sort or heap sort? var BUBBLE_SORT = true; // and so dont do heap sort //var BUBBLE_SORT = false; // and so DO heap sort // for heap sort, pick a heap build method //var MAGIC_BUILD = true; var MAGIC_BUILD = false; // now gen the data to sort if (BAD_DATA) { alert("gen ordered data"); nums=genOrderedElts(N); } else { alert("gen random data"); nums=genRandomElts(N,MAX); } // either run bubble sort or heap sort if (BUBBLE_SORT) { // do bubble sort and end if (N>80000) { if ( !confirm(N+ " too big, bubble sort too long, continue anyway?") ) { return ; } } alert(N+" elts, Bubble sorting demo... "); st = (new Date()).getTime(); sortedNums = bubbleSort(nums); // dont do this on more than // 100,000 items or you will et = (new Date()).getTime(); alert(et-st + " msec total sort"); return; } else { // do heap sorts and end alert(N+" elts, Heap Sorting demo... "); st = (new Date()).getTime(); if (MAGIC_BUILD) { sortedNums = heapishSortBuild(nums); } else { sortedNums = heapishSortInsert(nums); } et = (new Date()).getTime(); alert(et-st + " msec total sort"); alert("heap fill: "+ heapFillTime); return; } } function heapishSortBuild(vals) { alert("doing magic build"); var hh = makeNewHeap(); var st = (new Date()).getTime(); hh.build(vals); var et = (new Date()).getTime(); heapFillTime = et-st; //hh.print(); var sVals = []; var s = hh.size(); for (var k=0; k0; k--) { // bubble down, like in delete var temp, done=false; var n=k, c=0; while(!done) { if (this.isLeaf(n)) { done=true; } else { if (this.hasOnlyLC(n)) { c = this.LC(n); } else { c = (this.LCV(n) < this.RCV(n)) ? this.LC(n) : this.RC(n) ; } if (this.elts[n]>this.elts[c]) { temp=this.elts[n]; this.elts[n]=this.elts[c]; this.elts[c]=temp; n = c; } else { done=true; } } } } }, insert: function (val) { this.last++; this.elts[this.last] = val; var n=this.last; var p=this.PN(n); var temp; while ( (p!==0) && (valthis.elts[c]) { temp=this.elts[c]; this.elts[c]=this.elts[n]; this.elts[n]=temp; n = c; } else { done=true; } } } }, LC: function (n) { return 2*n; }, // slot num RC: function (n) { return (2*n)+1; }, // slot num PN: function (n) { return Math.floor(n/2); }, // slot num LCV: function (n) { return this.elts[this.LC(n)]; }, // element val RCV: function (n) { return this.elts[this.RC(n)]; }, // element val PNV: function (n) { return this.elts[this.PN(n)]; }, // element val isLeaf: function (n) { return ( this.LC(n)>this.last && this.RC(n)>this.last ); }, hasOnlyLC: function (n) { return ( this.RC(n)>this.last && this.LC(n)<=this.last ); }, print: function ( ) { alert("heap elts: "+ this.elts.slice(1)); }, } return heap; } //================================================================================== // // bubble sort // //================================================================================== function bubbleUp ( vals ) { var temp; for (var i=0; ivals[i+1]) { temp=vals[i]; vals[i]=vals[i+1]; vals[i+1]=temp; } } return vals; } function bubbleSort ( vals ) { for (var i=0; i