Heap sorting algorithm implementation (javascript language)

Heap sorting algorithm implementation (javascript language)

1. The nature of the binary heap
2. The principle of heap sorting
3. The javascript code:

//堆中某节点按升序或者降序递归下沉
//author:	Hengda
//arr: 	待排序数组
//nodeNo: 	二叉树中指定节点的序号/堆数组中的下标
//heapLen:	堆的长度
//mode: 	true 大的下沉,false 小的下沉
function heapNodeSink( arr, nodeNo, heapLen, mode ){
    
    

	var leftChild = ( nodeNo + 1 ) * 2 - 1;	//做孩子
	var rightChild = leftChild + 1;			//右孩子
	var maxminNo = nodeNo;					//最大值的序号
	var temp;								//用户变量值得交换

	if( mode ){
    
    
		//
		if( heapLen > leftChild && arr[ maxminNo ] > arr[ leftChild ] ){
    
    
			maxminNo = leftChild;//更新最大节点序号
		}
		if( heapLen > rightChild && arr[ maxminNo ] > arr[ rightChild ] ){
    
    
			maxminNo = rightChild;//更新最大节点序号
		}
	}else{
    
    
		if( heapLen > leftChild && arr[ maxminNo ] < arr[ leftChild ] ){
    
    
			maxminNo = leftChild;//更新最大节点序号
		}
		if( heapLen > rightChild && arr[ maxminNo ] < arr[ rightChild ] ){
    
    
			maxminNo = rightChild;//更新最大节点序号
		}
	}

	//最大值所在节点有变化,则交换
	if( maxminNo != nodeNo ){
    
    

		//交换
		temp = arr[ maxminNo ];
		arr[ maxminNo ] = arr[ nodeNo ];
		arr[ nodeNo ] = temp;

		//继续下沉操作
		heapNodeSink( arr, maxminNo, heapLen, mode );
	}
}

//功能:		堆排序
//author: 	Hengda
//arr: 	待排序数组
//mode: 	true 从大到小排序,false 从小到大排序
function heapSort( arr, mode ){
    
    
	var len = arr.length;	//数组的长度
	var temp;				//用于交换节点值
	var endHeapNodeNo;		//堆末尾节点在数组中的下标

	//将数组调整为二叉堆
	for( var i = Math.floor( len / 2 ) - 1; i >= 0; i-- ){
    
    
		heapNodeSink( arr, i, len, mode );
	}

	for( var heapLen = len; heapLen > 0; heapLen-- ){
    
    
		endHeapNodeNo = heapLen - 1;//堆的最后一个节点的序号

		//交换堆顶和堆尾元素
		temp = arr[ endHeapNodeNo ];
		arr[ endHeapNodeNo ] = arr[ 0 ];
		arr[ 0 ] = temp;

		//对除了堆尾元素组成的堆进行堆顶下沉操作
		heapNodeSink( arr, 0,  heapLen - 1, mode );
	}
	return arr;
}

Note: The
heap is a complete binary tree whose parent node is always greater than or equal to or less than or equal to the child node;
deleting a node from the heap is an operation from the top of the heap;
inserting a node is an operation from the end of the heap;
after inserting an element, you need to float the newly inserted element to make It satisfies the nature of the
pile again; after the top element is replaced, the top element needs to be sinked until the nature of the pile is satisfied.

Test to sort 1 million random numbers

//测试数据生成函数
//author:Hengda
//n是需要生成的个数
//随机生成有n个元素的没有顺序的大小顺序的数组
function makeData( n ){
    
    
	var dt = [];
	var i = n;
	while( i-- ){
    
    
		dt.push( Math.floor( Math.random() * n ) );
	}
	return dt;
}

//测试

//生成100万个随机数数据
arr = makeData(1000000);

console.log("100万条数据:");
console.log(arr);
arrJsonStr = JSON.stringify( arr );

//升序排序
arr1 = JSON.parse( arrJsonStr );//复制数据到arr1
console.time( '堆排序_升序_耗时' );
arr1 = heapSort( arr1, false );
console.timeEnd( '堆排序_升序_耗时' );
console.log( arr1.toString() );

//降序排序
arr2 = JSON.parse( arrJsonStr );//复制数据到arr2
console.time( '堆排序_降序_耗时' )
arr2 = heapSort( arr2, true );
console.timeEnd( '堆排序_降序_耗时' );
console.log( arr2.toString() );

Insert picture description here

Guess you like

Origin blog.csdn.net/one312/article/details/113066048