排序算法---堆排序

算法设计:
堆排序使用的是二叉堆。它首先将所有元素添加到一个堆上,然后不断移除最大元素以获得一个排好序的线性表。算法时间复杂度为O(nlogn)
二叉堆:
    *形状属性:一棵完全二叉树
    *堆属性:每个节点大于或等于它的任意一个孩子
代码实现:
实现二叉堆:
public class Heap<E extends Comparable<E>> {
    private ArrayList<E> list = new ArrayList<>();

    // 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 newObject) {
        list.add(newObject);
        int currentIndex = list.size() - 1;
        // swap if the current object is greater than its parents
        while (currentIndex > 0) {
            int parentIndex = (currentIndex - 1) / 2;
            if (list.get(currentIndex).compareTo(list.get(parentIndex)) > 0) {
                E temp = list.get(currentIndex);
                list.set(currentIndex, list.get(parentIndex));
                list.set(parentIndex, temp);
            } else
                break;
            currentIndex = parentIndex;
        }
    }

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

        E removeObject = 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;// 此时已经在堆的最底层
            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 removeObject;
    }

    public int getSize() {
        return list.size();
    }
使用heap类进行排序:
public class HeapSort {

    public static void main(String[] args) {
        Integer[] list = { -44, -5, -3, 3, 3, 1, -4, 0, 1, 2, 4, 5, 53 };
        heapSort(list);
        System.out.print("{-44,-5,-3,3,3,1,-4,0,1,2,4,5,53}" + " by HeapSort is\n{");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + ",");
        }
        System.out.println("}");
    }

    public static <E extends Comparable<E>> void heapSort(E[] list) {
        // Create a heap of integers
        Heap<E> heap = new Heap<>();

        // Add elements to the heap
        for (int i = 0; i < list.length; i++)
            heap.add(list[i]);

        for (int i = list.length - 1; i >= 0; i--) {
            list[i] = heap.remove();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值