堆排序(java 语言实现)

可以用 数组或者线性表实现 Heap ,关键是理清楚
当前节点的坐标和父节点的坐标以及左右孩子的坐标的关系,比如 当前坐标是 i 其他节点的坐标如何表示。然后就是添加删除的原则。

package com.shan.heapSort;


public class Heap<E extends Comparable<E>> {
    private java.util.ArrayList<E> list = new java.util.ArrayList<E>();

    /** Create a default heap */
    public Heap() {
    }

    /** Create a heap from an array of objects */
    public Heap(E[] objects) {
        for (int i = 0; i < objects.length; i++) {
            add(objects[i]);
        }
    }

    /** Add a new object into the heap */
    public void add(E e) {
        list.add(e); // Append to the heap
        int currentIndex = list.size() - 1;

        while (currentIndex > 0) {
            int parentIndex = (currentIndex - 1) / 2;

            E current = list.get(currentIndex);
            E parent = list.get(parentIndex);

            if (current.compareTo(parent) > 0) {
                list.set(parentIndex, current);
                list.set(currentIndex, parent);
                currentIndex = parentIndex;
            } else {
                break; // the tree is a heap now
            }
        }

        System.out.println(list);
    }

    /** Remove root from the heap */
    public E remove() {
        // if the Heap is empty return null
        if (list.size() == 0)
            return null;

        // cached root(the first element of the list),
        // and then replace it with the last element in the list
        E removedOject = list.get(0);
        list.set(0, list.get(list.size() - 1));
        list.remove(list.size() - 1);

        // find the proper place for the current element
        int currentIndex = 0;
        while (currentIndex < list.size()) {
            int leftChildIndex = 2 * currentIndex + 1;
            int rightChildIndex = 2 * currentIndex + 2;

            // Find the max between the tow child
            if (leftChildIndex >= list.size()) // the tree is a heap
                break;

            int maxIndex = leftChildIndex;
            if (rightChildIndex < list.size()) {
                if (list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0) {
                    maxIndex = rightChildIndex;
                }
            }

            // Swap if the current node is less then the maximum
            E current = list.get(currentIndex);
            E maxChild = list.get(maxIndex);
            if (current.compareTo(maxChild) < 0) {
                list.set(maxIndex, current);
                list.set(currentIndex, maxChild);
                currentIndex = maxIndex;
            } else {
                break;
            }

        }

        return removedOject;
    }

    /** Remove the root from the heap */
    public E remove2() {
        if (list.size() == 0)
            return null;

        E removedObject = list.get(0);
        list.set(0, list.get(list.size() - 1));
        list.remove(list.size() - 1);

        int currentIndex = 0;
        while (currentIndex < list.size()) {
            int leftChildIndex = 2 * currentIndex + 1;
            int rightChildIndex = 2 * currentIndex + 2;

            // Find the maximum between two children
            if (leftChildIndex >= list.size())
                break; // The tree is a heap
            int maxIndex = leftChildIndex;
            if (rightChildIndex < list.size()) {
                if (list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0) {
                    maxIndex = rightChildIndex;
                }
            }

            // Swap if the current node is less than the maximum
            if (list.get(currentIndex).compareTo(list.get(maxIndex)) < 0) {
                E temp = list.get(maxIndex);
                list.set(maxIndex, list.get(currentIndex));
                list.set(currentIndex, temp);
                currentIndex = maxIndex;
            } else
                break; // The tree is a heap
        }

        return removedObject;
    }

    /*public ArrayList<E> heapSort() {
        for (int i = 0; i < list.size(); i++) {
            E temp  = list.get(i);
            list.set(i, this.remove());
        }

        return list;
    }
*/
    /** Get the number of nodes in the tree */
    public int getSize() {
        return list.size();
    }

    public static void main(String[] args) {
        Integer[] list = { 2, 1, 3, 5, 0, 12, 34, 22, 89, 11 };
        Heap<Integer> heap = new Heap<>(list);

        for (int i = list.length - 1; i >=0; i--) {
            //System.out.print(heap.remove() + " ");
            list[i] = heap.remove();
        }

        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值