Deadly data structure and algorithm (sort java)-heap sort. Ability to learn, if there are mistakes, correct them in time

Eight sorting algorithms

1. Bubble sort algorithm

2. Choose a sorting algorithm

3. Insertion sort bubble algorithm

4. Hill sort bubble algorithm

5. Quickly insert bubble algorithm

6. Merge sort bubble algorithm

7. Cardinal sorting bubble algorithm

8. Heap Sorting Algorithm

Heap sort

1. Concepts and ideas

1. What is a heap?

The heap is usually an array object that can be seen as a tree. The heap always satisfies the following properties:
the value of a node in the heap is always no greater than or no less than the value of its parent node.
Heap is always a complete binary tree.
The heap with the largest root node is called the big top heap. The heap with the smallest root node is called the small top heap.
Schematic diagram of large top pile:
Insert picture description here

Schematic diagram of small top pile:
Insert picture description here

2. What is heap sort?

Heap sorting is a sorting algorithm designed by using the data structure of heap. Heap sorting is a selective sorting. Its worst, best, average time complexity is O(nlogn), which is unstable sorting.

2. Graphical process

Basic idea :

  1. Construct the array to be sorted into a big top heap
  2. Exchange the element at the top of the heap with the end element, and the end element is the maximum
  3. Reconstitute the remaining n-1 elements into one, so that the second smallest value of n elements is obtained. Repeat operations 1, 2 until the length of the element to be executed is 1. For
    detailed operations, see the comments in the code .

3. Sample code

package Tree;

import java.util.Arrays;

public class heapSort {
    public static void main(String[] args) {
        int[] arr = {4,8,5,9,13,6,3,1};
        heapSort( arr );
        System.out.println( Arrays.toString(arr));
    }

    /**
     * 堆排序
     * 1. 将要排序的数组构造成一个大顶堆
     * 2. 将堆顶的元素与末尾元素进行交换,此时末尾元素就是最大值
     * 3. 将剩余n-1个元素重新构成一个,这样得到n个元素的次小值。反复执行1、2操作,直到要执行的元素长度为1.
     * @param arr 要进行排序的数组
     */
    public static void heapSort(int[] arr){
        //1. 第一次构建一个大顶堆
        for (int i = arr.length/2-1; i >= 0; i--) {
            //从第一个非叶子节点从下至上,从右到左调整结构
            adjustHeap( arr, i, arr.length );
        }

        for (int j = arr.length - 1; j > 0 ; j--) {
            //将堆顶元素与末尾元素进行交换
            int temp = arr[0];
            arr[0] = arr[j];
            arr[j] = temp;

            //对堆进行调整,此时从堆顶开始做一次调整就可以得到一个大顶堆
            //因为经过第一次调整之后,父节点的子节点都已经比它的子节点要大了。只有交换之后的根节点不满足要求,所以此时只需要i=0做一次调整即可。
            adjustHeap( arr, 0, j );
        }
    }
    /**
     * 该方法用来
     * @param arr 要调整的数组(树)
     * @param i   开始调整的叶子节点的下标
     * @param length  调整的长度
     */
    public static void adjustHeap (int[] arr, int i, int length){
        int temp = arr[i];  //取出当前元素i
        //对节点下面的元素进行遍历
        //从i的左子节点进行循环,也就是i*2+1处开始
        for (int k = i * 2 + 1; k < length; k++) {
            if(k + 1 < length && arr[k] < arr[k + 1]){
                k++;  //对下一个节点做判断
            }
            //如果子节点大于父节点,把子节点的值给父节点
            if(arr[k] > temp){
                arr[i] = arr[k];  //把较大的值,赋值给当前节点
                i = k;  //把i指向k,继续循环比较
            }else {
                break;
            }
        }
        arr[i] = temp;  //temp放入最终的位置
    }
}

to sum up

This article uses java to complete a simple heap sorting algorithm. And introduced the related concepts of heap sort.

Guess you like

Origin blog.csdn.net/qq_41497756/article/details/109254375