数据结构与算法——Java实现 33.堆排序

刻意去找的东西,往往是找不到的。

天下万物的来和去,都有它的时间。

                                        —— 24.10.10

使用堆进行排序

算法描述

1.heapify 建立大顶堆(所有结点的父元素大于子元素

2.将堆顶与堆底交换(最大元素被交换到堆底),缩小并下潜调整堆

3.重复第二步直至堆里剩一个元素

代码实现

堆的各方法

import java.util.Arrays;

public class MaxHeap {
    int[] heapArray;
    int size;

    public MaxHeap(int capacity) {
        heapArray = new int[capacity];
    }

    public MaxHeap(int[] Array) {
        heapArray = new int[Array.length];
        System.arraycopy(Array, 0, heapArray, 0, Array.length);
        size = Array.length;
        buildHeap();
    }

    private void buildHeap() {
        for (int i = size / 2 - 1; i >= 0; i--) {
            heapifyDown(i);
        }
    }

    public void heapifyDown(int index) {
        int leftChildIndex = 2 * index + 1;
        int rightChildIndex = leftChildIndex + 1;
        int largestIndex = index;

        if (leftChildIndex < size && heapArray[leftChildIndex] > heapArray[largestIndex]) {
            largestIndex = leftChildIndex;
        }

        if (rightChildIndex < size && heapArray[rightChildIndex] > heapArray[largestIndex]) {
            largestIndex = rightChildIndex;
        }

        if (largestIndex!= index) {
            swap(index, largestIndex);
            heapifyDown(largestIndex);
        }
    }

    public void swap(int i, int j) {
        int temp = heapArray[i];
        heapArray[i] = heapArray[j];
        heapArray[j] = temp;
    }

    public int peek() {
        if (size == 0) {
            return -1;
        }
        return heapArray[0];
    }

    public int poll() {
        if (size == 0) {
            return -1;
        }
        int maxValue = heapArray[0];
        heapArray[0] = heapArray[size - 1];
        size--;
        heapifyDown(0);
        return maxValue;
    }

    public int poll(int index) {
        if (size == 0 || index >= size) {
            return -1;
        }
        int removedValue = heapArray[index];
        heapArray[index] = heapArray[size - 1];
        size--;
        heapifyDown(index);
        return removedValue;
    }

    public void replace(int newValue) {
        if (size == 0) {
            return;
        }
        heapArray[0] = newValue;
        heapifyDown(0);
    }

    public boolean offer(int value) {
        if (size == heapArray.length) {
            return false;
        }
        heapArray[size] = value;
        heapifyUp(size);
        size++;
        return true;
    }

    private void heapifyUp(int index) {
        int parentIndex = (index - 1) / 2;
        while (index > 0 && heapArray[index] > heapArray[parentIndex]) {
            swap(index, parentIndex);
            index = parentIndex;
            parentIndex = (index - 1) / 2;
        }
    }

    public static void main(String[] args) {
        int[] Arr = {1, 2, 3, 4, 5, 6, 7};
        MaxHeap maxHeap = new MaxHeap(Arr);
        System.out.println(Arrays.toString(maxHeap.heapArray));
        // [7, 5, 6, 4, 2, 1, 3]

        maxHeap.replace(5);
        System.out.println(Arrays.toString(maxHeap.heapArray));
        // [6, 5, 5, 4, 2, 1, 3]

        maxHeap.poll(2);
        System.out.println(Arrays.toString(maxHeap.heapArray));
        // [6, 5, 3, 4, 2, 1, 3]

        System.out.println(maxHeap.peek());
        // 6

        maxHeap.poll();
        System.out.println(Arrays.toString(maxHeap.heapArray));
        // [5, 4, 3, 1, 2, 1, 3]

        System.out.println(maxHeap.offer(5));
        // true
        System.out.println(Arrays.toString(maxHeap.heapArray));
        // [5, 4, 5, 1, 2, 3, 3]

        maxHeap.poll();
        System.out.println(Arrays.toString(maxHeap.heapArray));
        // [5, 4, 3, 1, 2, 3, 3]

        maxHeap.offer(9);
        System.out.println(Arrays.toString(maxHeap.heapArray));
        // [9, 4, 5, 1, 2, 3, 3]
    }
}

堆排序的实现

调用堆中方法,按照给出的堆排序算法思路,进行排序

import java.util.Arrays;

public class Sort {
    public static void main(String[] args) {
        int[] arr = {2, 3, 1, 7, 6, 5, 4, 8, 9};
        MaxHeap maxHeap = new MaxHeap(arr);
        System.out.println(Arrays.toString(arr));

        while(maxHeap.size > 1){
            maxHeap.swap(0, maxHeap.size-1);
            maxHeap.size--;
            maxHeap.heapifyDown(0);
        }
        System.out.println(Arrays.toString(maxHeap.heapArray));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值