HeapSort


title: Remember a heap sort
date: 2018-4-5 10:13:40
categories:
- algorithm
tags:

- algorithm

Summary

As an improved version of selection sort, heap sort can save the comparison results of each element, so that we can make corresponding adjustments to the elements that have been compared when selecting the smallest/larger elements.
Heap sort is a tree selection sort. In the sorting process, elements can be regarded as a complete binary tree. Each node is larger (smaller) than its two child nodes. When each node is greater than or equal to its two When each node is less than or equal to its two child nodes, it is called a large top heap, also known as heap order; when each node is less than or equal to its two child nodes, it is called a small top heap.
The following is the maximum heap, the minimum heap

heap initialization

Construct the array into an initial heap (if you want ascending order, build a large root heap, if you want descending order, build a small root heap) and adjust from the last node to get the initial heap.

代码很简单

// 模拟堆的构造
for (int i = len/2 - 1; i >= 0; i--) {
    headAdjust(arr, i, len);
}

Then every time the top element and the tail element of the heap are exchanged, of course, after each exchange, it may cause the imbalance of the heap, so the heap is adjusted every time.

// 堆的调整算法
    public static void headAdjust(int[] arr, int i, int len) {
        // i 为非叶子节点的索引
        int left;
        int right;
        int j;
        while ((left = 2*i + 1) <= len) { // 判断当前节点有无 左节点,left为左节点
            right = left + 1; // 右节点
            j = left; // j ---> left
            if (j < len) { // 上面假设的右节点存在的话
                if (arr[left] < arr[right]) {
                    j++; // j---> right 指向右节点
                }
            }
            if (arr[i] < arr[j]) {
                swap(arr, i, j);
            }
            else {
                break; // 父节点,比左右孩子节点都大,不需要比较了
            }

            // 这个 i = j, 会循环调整堆
            i = j;
        }
    }


Finally, attach all your own code

package someSortAlgorithm;
/**
* @author 作者 : coderlong
* @version 创建时间:2018年1月3日 上午10:41:58
* 类说明: 堆排序和 归并排序一样都是时间复杂度为 nlog(n),同事又和插入排序一样都是 就地排序
*  不需要额外的空间
*/
public class HeapSort {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arr = {20,50,10,40,70,10,80,30,60};
        System.out.println("堆排序之前");
        for (int i : arr) {
            System.out.print(i + " ");
        }
        heapSort(arr);
        System.out.println("\n 堆排序之后");
        for (int i : arr) {
            System.out.print(i + " ");
        }


    }
    // 堆排序算法, 最大堆
    public static void heapSort(int[] arr) {
        int len = arr.length - 1;
        // 模拟堆的构造
        for (int i = len/2 - 1; i >= 0; i--) {
            headAdjust(arr, i, len);
        }

        while (len >= 0) {
            swap(arr, 0, len); // 堆顶元素,和尾节点交换之后,长度减一,最大元素已经到了堆的最后
            len--; // 堆长度减一
            headAdjust(arr, 0, len); // 再次对堆进行调整 
        }
    }

    // 堆的调整算法, 堆排序的关键之处
    public static void headAdjust(int[] arr, int i, int len) {
        // i 为非叶子节点的索引
        int left;
        int right;
        int j;
        while ((left = 2*i + 1) <= len) { // 判断当前节点有无 左节点,left为左节点
            right = left + 1; // 右节点
            j = left; // j ---> left
            if (j < len) { // 上面假设的右节点存在的话
                if (arr[left] < arr[right]) {
                    j++; // j---> right 指向右节点
                }
            }
            if (arr[i] < arr[j]) {
                swap(arr, i, j);
            }
            else {
                break; // 父节点,比左右孩子节点都大,不需要比较了
            }

            // 这个 i = j, 会循环调整堆
            i = j;
        }
    }

    // 交换,堆中两个节点的值, 其实就是每次调整完之后,把堆顶的最大元素放到尾部,就好比进行了一次冒泡
    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}

result :

堆排序之前
20 50 10 40 70 10 80 30 60 
 堆排序之后
10 10 20 30 40 50 60 70 80 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481264&siteId=291194637
Recommended