Java implementation-binary tree of data structure (four): heap sort

Data structure of the binary tree (four): heap sort

Basic introduction :

Heap sort is a sort of selection , its worst, best, average practical complexity is O (nlogn), it is also unstable sort .

Heap introduction:
Heap is a complete binary tree with special properties :
big top heap :
the value of each node is greater than or equal to the value of its left and right child nodes.
Small top heap :
The value of each node is less than or equal to the value of its left and right child nodes.

Schematic diagram of the big top heap :
Insert picture description here
we number the nodes in the heap by layer, and map to the array is arr[50,45,40,20,25,35,30,10,15];
that is, arr[i]>=arr [2 i+1]&&arr[i]>=arr[2 i+2]; (i is the corresponding node number)
(Note: the size of the top heap does not require the size relationship between the left and right child nodes).

Schematic diagram of small top pile :
Insert picture description here
arr[i]<=arr[2 i+1]&&arr[i]<=arr[2 i+2].

Heap sorting:
Generally, ascending order adopts large top heap, and descending order adopts small top heap.

The basic idea of ​​heap sorting:
1. First construct the sequence to be sorted into a large top heap.
2. At this time, the maximum value of the entire sequence is the root node of the large top pile.
3. Exchange the value of this root node with the end value of the sequence to be sorted, this time the end is the maximum value.
4. Then reconstruct the remaining n-1 trees into a heap, so that you will get the maximum value of n-1 elements, and then exchange this with the last element of n-1 elements.
5. Repeatedly execute this way to get an ascending ordered sequence.

Graphical example:
First, we define an array:
arr[4,5,8,5,9]
and construct it into a tree as shown in the figure:
Insert picture description here
According to the idea, the next thing to do is to construct it into a large top pile.
The idea of ​​constructing it into a large top pile is as follows:
1. First, we first find the last non-leaf node (available from arr.length/2-1) of the tree (that is, node 6), and let 6 nodes and their left and right children Nodes are compared, and the maximum of the three is exchanged with node 6.
The result of the exchange is as follows:
Insert picture description here
So the array corresponding to the tree becomes
arr[4,9,8,5,6]

2. Then adjust from left to right, from bottom to top,
and then find the second non-leaf node (that is, 4 nodes) and perform the same comparison and exchange, and get the following:
Insert picture description here
3. At this time, it is found that the replaced node 4 is still more than its left and right. The child node is small, exchange again, and finally get the following:
Insert picture description here
At this time, get the array
arr[9,6,8,5,4] and
then exchange the root node (9 nodes) with the end, and get the following:
Insert picture description here
finally and so on, keep going Comparing , swapping , setting , and finally getting an ascending array.

The java reference code is as follows:

import java.util.Arrays;

public class HeapSort {
    
    
	public static void main(String[] args) {
    
    
		//升序排列
		int arr[]= {
    
    4,6,8,5,9};
		heapSort(arr);
		System.out.println(Arrays.toString(arr));
	}
	//堆排序方法
	public static void heapSort(int[] arr) {
    
    
		int temp=0;
		for(int i=arr.length/2-1;i>=0;i--) {
    
    
			makeBigHeap(arr,i,arr.length);
		}
		for(int j=arr.length-1;j>0;j--) {
    
    
			temp=arr[j];
			arr[j]=arr[0];
			arr[0]=temp;
			makeBigHeap(arr,0,j);
		}
		
	}
	
	/*
	 * 将数组调整成大顶堆的方法:将以i对应的非叶子节点树调整成大顶堆
	 * arr:待调整数组
	 * i:非叶子节点在数组中索引
	 * length:调整数组的长度
	 */
	public static void makeBigHeap(int arr[],int i,int length) {
    
    
		int temp=arr[i];//取出当前元素,保存在临时变量
		//k=i*2+1,k是i节点的左子节点
		for(int k=i*2+1;k<length;k=k*2+1) {
    
    
			if(k+1<length&&arr[k]<arr[k+1]) {
    
    
				k++;//k指向右子节点
			}
			if(arr[k]>temp) {
    
    //如果子节点大于父节点
				arr[i]=arr[k];//把较大的值赋值给当前节点
				i=k;//继续循环比较
			}else {
    
    
				break;
			}
		}
		arr[i]=temp;//将temp赋值放到调整后的位置

	}
}

Guess you like

Origin blog.csdn.net/qq_45273552/article/details/109101049