原帖地址: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) );
堆排序算法---《程序员必须知道的10大基础实用算法及其讲解》,布布扣,bubuko.com
堆排序算法---《程序员必须知道的10大基础实用算法及其讲解》
原文地址:http://my.oschina.net/arrowing/blog/292578