【数据结构】堆的应用

public class Heap {

    private int n = 0;
    private int count = 0;
    int[] a;// 从角标1开始存储

    public Heap(int n) {
        this.n = n;
        this.count = 0;
        a = new int[n];

    }

    /**
     * 插入操作的
     */
    public void insert(int value) {

        if (count >= n) {
            Log.d("zpb", "堆已经满了");
            return;
        }
        count++;
        a[count] = value;
        // 插入后,从节点开始从下往上开始堆化,插入 自下往上堆化
        int i = count;
        while (i / 2 > 0 && a[i] > a[i / 2]) {
            int temp = a[i / 2];
            a[i / 2] = a[i];
            a[i] = temp;
            i = i / 2;

        }

    }

    public int removeMax() {
        if (count == 0) {
            return -1;
        }
        // 最大值位于栈顶
        int max = a[1];
        // 然后继续堆化,自上向下堆化,最后一个元素和顶部元素交换后开始堆化

        a[1] = a[count];
        count--;
        heapify(a, 1, count);
        return max;
    }

    /**
     * 从 i开始到n,进行堆化
     * @param a
     * @param i
     * @param n
     */
    public void heapify(int a[], int i, int n) {

        while (true) {
            // 当前位置
            int maxPos = i;
            if (i * 2 <= n && a[i * 2] > a[i]) {
                int temp = a[i * 2];
                a[i * 2] = a[i];
                a[i] = temp;
                i = i * 2;
            }

            if (i * 2 + 1 <= n && a[i * 2 + 1] > a[i]) {
                int temp = a[i * 2 + 1];
                a[i * 2 + 1] = a[i];
                a[i] = temp;
                i = i * 2 + 1;

            }
            if (maxPos == i) {
                break;
            }

        }

    }

}

堆的实现

 

2.堆排序

public class HeapUtils {
    /**
     * 从 i开始到n,进行堆化
     * @param a
     * @param i
     * @param n
     */
    public static void heapify(int a[], int i, int n) {

        while (true) {
            // 当前位置
            int maxPos = i;
            if (i * 2 <= n && a[i * 2] > a[i]) {
                int temp = a[i * 2];
                a[i * 2] = a[i];
                a[i] = temp;
                i = i * 2;
            }

            if (i * 2 + 1 <= n && a[i * 2 + 1] > a[i]) {
                int temp = a[i * 2 + 1];
                a[i * 2 + 1] = a[i];
                a[i] = temp;
                i = i * 2 + 1;

            }
            if (maxPos == i) {
                break;
            }

        }

    }

    /**
     * 自底部开始向下堆化
     * @param a
     * @param n
     */
    public static void buildHeap(int[] a, int n) {
        for (int i = n / 2; i >= 0; i--) {
            heapify(a, i, n);
        }

    }

    public static void sort(int[] a, int n) {
        printData(a);
        buildHeap(a, n);
        int k = n;
        while (k >= 0) {
            int temp = a[0];
            a[0] = a[k];
            a[k] = temp;
            k--;
            // 交互后继续堆化
            heapify(a, 0, k);
        }
        printData(a);

    }

    /**
     *输出数据
     * @param a
     */
    private static void printData(int[] a) {
        for (int i = 0; i < a.length; i++) {
            Log.d("zpb", a[i] + ",");

        }
        Log.d("zpb","===========");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值