Algorithm - Heap Sort(Java)

44 篇文章 1 订阅
35 篇文章 2 订阅

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程大家好!欢迎来到我的网站! 人工智能被认为是一种拯救世界、终结世界的技术。毋庸置疑,人工智能时代就要来临了,科… 继续阅读 前言icon-default.png?t=O83Ahttps://www.captainai.net/

package live.every.day.Algorithm.Sort;

import java.util.Arrays;

/**
 * @author LiveEveryDay
 *
 * Introduction:
 * Heap Sort is a kind of selection sort.
 *
 * Basic Thought:
 * The trait of Heap Sort is:
 * In the sorting process,
 * treat the to-be-ordered sequence as the sequential storage structure
 * of a complete binary tree.
 * Take advantage of the inner relationships
 * of parent nodes and child nodes in the complete binary tree,
 * select the biggest (or smallest) record
 * from the current unordered sequence.
 *
 * Algorithm:
 * 1. Adjust the to-be-ordered sequence to max root heap.
 * The top element is the biggest element in the max root heap.
 * 2. Swap the max root heap top element
 * with the last element of the unordered sequence,
 * and enlist the last position of unordered sequence
 * into the ordered sequence,
 * and then adjust the new unordered sequence to max root heap.
 * 3. Repeat the process until unordered sequence disappears.
 *
 * Note:
 * At the beginning, all the sequence are unordered.
 * Every swapping, is swapping
 * the top element of max root heap into ordered sequence,
 * to guarantee the ordered sequence is ordered.
 *
 * Complexity:
 * The time complexity of Heap Sort is O(nlog2n).
 */
public class HeapSort {

    /**
     * Heap Sort
     *
     * @param a The array to be sorted
     */
    public static void heapSort(int[] a) {
        if (a == null || a.length == 0) {
            return;
        }

        // Build max heap
        buildMaxHeap(a);

        // Print max heap
        System.out.println("Build max heap:");
        System.out.println(Arrays.toString(a));

        System.out.println("Each round of heap sorting:");
        for (int i = a.length - 1; i > 0; i--) {

            // Swap the max root heap top element with the last element of the unordered sequence
            int t = a[0];
            a[0] = a[i];
            a[i] = t;

            // Adjust the new unordered sequence to max root heap
            maxHeaping(a, 0, i);

            // Print max root heap after each sorting
            System.out.println(Arrays.toString(a));
        }
    }

    /**
     * Build max heap from bottom to top.
     * By the characteristic of complete binary tree,
     * the child node's subscript starts from index = a.length / 2,
     * so adjust max heap from subscript = (a.length / 2) - 1.
     *
     * @param a The array to be sorted
     */
    private static void buildMaxHeap(int[] a) {
        for (int i = (a.length >>> 1) - 1; i >= 0; i--) {
            maxHeaping(a, i, a.length);
        }
    }

    /**
     * Adjust the specified node to heap
     *
     * @param a        The array to be sorted
     * @param i        The subscript of the specified node
     * @param heapSize The heap size, also the length of unordered sequence.
     */
    private static void maxHeaping(int[] a, int i, int heapSize) {

        // Left child node
        int left = (2 * i) + 1;

        // Right child node
        int right = 2 * (i + 1);

        // Temp variable, stores the large node.
        int large = i;

        // Comparing left child node
        if (left < heapSize && a[left] > a[large]) {
            large = left;
        }

        // Comparing right child node
        if (right < heapSize && a[right] > a[large]) {
            large = right;
        }

        // Swapping if child node is bigger than self, to make bigger element move upward.
        // And make the bigger element adjusted as heap to keep heap nature.
        if (large != i) {
            int t = a[large];
            a[large] = a[i];
            a[i] = t;

            maxHeaping(a, large, heapSize);
        }
    }

    /**
     * Test program
     *
     * @param args The arguments
     */
    public static void main(String[] args) {
        int[] a = {11, 16, 22, -3, 99, 0, 16, 6, -1, 99};

        System.out.println("Before heap sort:");
        System.out.println(Arrays.toString(a));

        System.out.println("In heap sort:");
        HeapSort.heapSort(a);

        System.out.println("After heap sort:");
        System.out.println(Arrays.toString(a));
    }

}

/* ------ Output ------
Before heap sort:
[11, 16, 22, -3, 99, 0, 16, 6, -1, 99]
In heap sort:
Build max heap:
[99, 99, 22, 6, 16, 0, 16, -3, -1, 11]
Each round of heap sorting:
[99, 16, 22, 6, 11, 0, 16, -3, -1, 99]
[22, 16, 16, 6, 11, 0, -1, -3, 99, 99]
[16, 11, 16, 6, -3, 0, -1, 22, 99, 99]
[16, 11, 0, 6, -3, -1, 16, 22, 99, 99]
[11, 6, 0, -1, -3, 16, 16, 22, 99, 99]
[6, -1, 0, -3, 11, 16, 16, 22, 99, 99]
[0, -1, -3, 6, 11, 16, 16, 22, 99, 99]
[-1, -3, 0, 6, 11, 16, 16, 22, 99, 99]
[-3, -1, 0, 6, 11, 16, 16, 22, 99, 99]
After heap sort:
[-3, -1, 0, 6, 11, 16, 16, 22, 99, 99]
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值