標籤:http os io art for cti
>
原帖地址:http://www.oschina.net/question/1397765_159365
快速排序演算法的基本特性:
時間複雜度:O(N * logN)
堆排序為不穩定排序,不適合記錄較少的排序。
var arr = [],count = 100,i = 0,parentIndex,exeCount = 0,startTime = + new Date(),stackSort = function(a){if(a.length === 1) return a;var sorted,len,tmpLen;// 構建小頂堆for(len = tmpLen = a.length;len--;){parentIndex = len % 2 === 0 ? len / 2 - 1 : //右子節點(len - 1) / 2; //左子節點parentIndex < 0 && ( parentIndex = 0 );if(a[len] < a[parentIndex]){a[parentIndex] = [ a[len], a[len] = a[parentIndex] ] [0]; // 交換數組中2個數}}sorted = [ a.shift() ];exeCount++;return sorted.concat( stackSort(a) );};// 構建隨機數組for(;i<count;i++){arr.push( Math.round(Math.random() * count) ); // 0 - count}console.log(‘原來的數組:‘, arr);arr = stackSort(arr);console.log(‘排序後的數組:‘, arr);console.log(‘計算的次數:‘ + exeCount );console.log(‘花費的毫秒數:‘ + (new Date() - startTime) );