Two ways to write heap sort

Project Introduction

  • This project decomposes the common written interview questions of major factories, traces the source to the underlying implementation principles of data structures and algorithms, and knows what they are.
  • Establish a knowledge structure system for easy search, welcome more like-minded friends to join the project AlgorithmPractice, (issues and pull requests are welcome).

What is heap sort

  • The array is stored according to the tree structure, that is, if the left child of the array exists, it is stored in 2 i+1 bits, and if the right child exists, it is stored in 2 i+2 bits.
  • A heap is a complete binary tree with the following properties: the value of each node is greater than or equal to the value of its left and right child nodes, which is called a big top heap; or the value of each node is less than or equal to the value of its left and right child nodes Value, called small top heap

Two solutions

Start of text

1. The whole pile method

  • Code implementation : HeapSort , test case: HeapSortTest
  • Design ideas :
    • First, start from n/2 of the array and traverse back to 0,
    • For each traversed element, you need to compare its left and right children in turn, and exchange the ones with larger values
    • In the case of exchange, recursive comparisons need to be performed in turn
  • Main code :
建堆和整堆
public void sortMethod(int[] heap) {
    
    
    int temp;
    //输入检查
    if (!check(heap)) {
    
    
        return ;
    }
    //初试化建堆
    for (int i = (heap.length - 1) / 2; i >= 0 ; i--) {
    
    
        heapify_big(heap, i, heap.length - 1);
    }
    //交换堆顶和数组末尾元素,循环整堆,注意边界值
    for (int i = heap.length - 1; i > 0; i--) {
    
    
        temp = heap[0];
        heap[0] = heap[i];
        heap[i] = temp;
        heapify_big(heap, 0, i-1);
    }
}
整堆的细节
//整堆函数——大顶堆
public void heapify_big(int[] heap, int parent, int border){
    
    
    //左孩子,最大值标记
    int flag = parent * 2 + 1;
    //越界判断:如果左孩子存在
    if(flag > border){
    
    
        return ;
    }
    //如果右孩子存在
    if(flag + 1 <= border){
    
    
        //左右孩子对比,找最大值
        flag = heap[flag] > heap[flag + 1] ? flag : flag + 1;
    }
    //对比父节点和孩子结点,找最大值,发生交换,并递归其最大值孩子结点
    if(heap[flag] > heap[parent]){
    
    
        int temp = heap[flag];
        heap[flag] = heap[parent];
        heap[parent] = temp;
        heapify_big(heap, flag, border);
    }
}
  • Note :

2. Direct whole pile method

  • Code implementation : HeapSort2 , test case: HeapSort2Test
  • Design ideas :
    • Every time the whole pile is completed, array[0] will be rounded out, so it is sorted by swapping it
    • The entire internal heap starts at n/2 and continues to loop to 0
  • Main code :
public void sortMethod(int[] array) {
    
    
    //输入检查
    if (!check(array)) {
    
    
        return;
    }
    int length = array.length - 1;
    for (int i = 0; i < length; i++) {
    
    
        heapify_big(array, length - i);
        int temp = array[0];
        array[0] = array[length - i];
        array[length - i] = temp;
    }
}
public void heapify_big(int[] array, int bound) {
    
    
	//数组从0开始,因此中间值是 (bound - 1) / 2
	int mid = (bound - 1) / 2;
	int temp;
	for (int i = mid; i >= 0; i--) {
    
    
		int flag = 2 * i + 1;
		if (flag + 1 <= bound) {
    
    
		flag = array[flag] > array[flag + 1] ? flag : flag + 1;
		}
	if (flag <= bound && array[flag] > array[i]) {
    
    
		temp = array[flag];
		array[flag] = array[i];
		array[i] = temp;
		}
	}
}
  • Note :
    • The whole stack method is to build the stack first and then maintain it. During maintenance, there will be recursive operations.
    • The direct whole-stack method is implemented through a two-layer loop without recursion.

Guess you like

Origin blog.csdn.net/ljfirst/article/details/106909921