Heap sort of sorting algorithm (Java version)

Heap sorting uses a so-called data structure for information management. Not only used in heap sorting, it can also construct an effective priority queue. Time complexity O(nlogn)

heap

The (binary) heap is an array, which can be viewed as an approximate complete binary tree. Each node on the book corresponds to an element in the array. Except for the bottom layer, the tree is completely full and filled from left to right.

The binary heap can be divided into two forms, the largest heap and the smallest heap.

  • Maximum heap: The value of the parent node is greater than or equal to the value of its child nodes.
  • Minimal heap: The value of the parent node is less than the value of its child nodes.

Heap sort

We use it in the heap sort algorithm 最大堆. Fill from left to right when filling.
Insert picture description here

ready

  • Count the number of parent nodes after the array to be sorted is filled into the heap structure
  • Knowing the position of the parent node in the array, find the positions of the left and right child nodes.
  • According to the position of any element in the array, calculate the position of its parent node.

According to the above picture, we can draw the law

    /**
     * 根据数组长度计算父节点个数
     *
     * @param pos 数组长度
     * @return 父节点数量
     */
    private static int parent(int len) {
    
    
        return len/2;
    }
    /**
     * 根据子节点所在数组位置,计算父节点位置
     *
     * @param pos 子节点位置
     * @return 父节点位置
     */
    private static int parent(int pos) {
    
    
        return (i-1)/2;
    }
    /**
     * 根据父节点所在数组位置,计算左叶子位置
     *
     * @param pos 父节点位置
     * @return 左子节点位子
     */
    private static int left(int pos) {
    
    
        return 2 * pos + 1;
    }

    /**
     * 根据父节点所在数组位置,计算右叶子位置
     *
     * @param pos 父节点位置
     * @return 右子节点位置
     */
    private static int right(int pos) {
    
    
        return 2 * pos + 2;
    }

Maintain maximum heap properties

Focus on the smallest unit heap (that is, the array has only three elements, the parent node, the left leaf, and the right leaf), and maintain it to maintain its maximum heap nature.

    /**
     * 对堆结构进行排序,将最大值作为根节点
     *
     * @param array 堆结构对应数组
     * @param pos   排序堆根节点所在数组中的位置
     * @param len   数组长度
     */
    private static void maxHeap(int[] array, int pos, int len) {
    
    
        //计算左右叶子位置
        int l = left(pos);
        int r = right(pos);
        int max = pos;

        if (l < len && array[l] > array[max]) {
    
    
            max = l;
        }
        if (r < len && array[r] > array[max]) {
    
    
            max = r;
        }
        if (max != pos) {
    
    
            int temp = array[pos];
            array[pos] = array[max];
            array[max] = temp;
            //将最大位置的堆再排序,此处是比较绕的点。
            //举例说明:我们先声明二维结构描述,(parent、left、right)
            //堆结构如描述(-3,20,14)(20,6,9)(14,7,8)
            //此时 parent:-3需要和20做交换,交换后为(20,-3,14)(-3,6,9)(14,7,8)
            //此时(-3,6,9)的结构是不对的,所以要对(-3,6,9)的堆结构排序
            //结果为(20,9,14)(9,6,-3)(14,7,8)
            //此时如果-3下还有子节点,还需进行排序(所以递归调用)
            maxHeap(array, max, len);
        }
    }

Build a pile

After being able to guarantee the nature of the maximum heap, we started building the heap in a bottom-up manner. That is, find the parent node with the largest element position as the start of the algorithm, and reach the heap vertex when the element position reaches 0.

    /**
     * 将数组构建为最大堆结构
     *
     * @param array 数组
     */
    private static void maxHeapBuild(int[] array) {
    
    
        int parentSize = array.length / 2;//计算包含子节点的父节点结束位置
        for (int i = parentSize; i >= 0; i--) {
    
    
            maxHeap(array, i, array.length);
        }
    }

Heap sort algorithm

Use to maxHeapBuildbuild the maximum heap, where the maximum element of the array is at the vertex, swap the position of the tail element with the vertex element, and then remove the tail element of the heap to create a new heap. At this time, the new heap may not meet the maximum heap properties after swapping positions. It is necessary to maxHeapmaintain the maximum heap, and then exchange with the tail elements of the current heap, in sequence.

   /**
     * 堆排序
     *
     * @param array 排序数组
     */
    private static void heapSort(int[] array) {
    
    
        //构建最大堆结构, 构建后的array数组并不能保证是有序的
        //但是每个父节点肯定是最大的
        maxHeapBuild(array);
        System.out.println("first build head ::: "+Arrays.toString(array));
        //用来控制数组的排序范围,因为下面要将得到的最大的数值依次放到最后
        int len = array.length;
        for (int i = array.length - 1; i > 0; i--) {
    
    
            System.out.println("root value :: " + array[0]);
            int temp = array[0];
            array[0] = array[i];
            array[i] = temp;
            // i 位置被占用,排序数组长度-1
            len--;
            maxHeap(array, 0, len);

            System.out.println(Arrays.toString(array));
        }
    }

Haha, is the algorithm considered a beginner? Come on, the next article "Quick Sort of Sorting Algorithm"

Guess you like

Origin blog.csdn.net/lijie2664989/article/details/83592500