<script type="text/javascript">
var arr = [22, 31, 1, 9, 99, 68, 55, 30];
function heap_adjust(arr, start, end){
var temp = arr[start],
j = start*2;
for(;j < end; j *= 2){
if(arr[j] < arr[j+1]){
j++;
}
if(temp > arr[j]){
break;
}
arr[start] = arr[j];
start = j;
}
arr[start] = temp;
}
function heap_sort(arr){
var len = arr.length;
for(var i = len/2; i >= 0; i--){
heap_adjust(arr, i, len);
}
for(var i = len; i > 0; i--){
swap(arr, 0, i -1);
heap_adjust(arr, 0, i - 2);
}
}
function swap(arr, i, j){
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
heap_sort(arr)
console.log(arr);
</script>
原文:http://www.cnblogs.com/fengfengtk/p/5041156.html