最大优先序列的java实现

public class Priority_Queue {
    private static int heap_size = 0;

    public static int Parent(int i){
        return (i-1)/2;
    }

    public static void Max_Heapify(double[] A, int i){
        int l = 2*i + 1;
        int r = 2*i + 2;
        int largest;
        if (l <= heap_size - 1 && A[l] > A[i]){
            largest = l;
        }else {
            largest = i;
        }
        if (r <= heap_size - 1 && A[r] > A[largest]){
            largest = r;
        }
        if (largest != i){
            double x = A[i];
            A[i] = A[largest];
            A[largest] = x;
            Max_Heapify(A, largest);
        }
    }

    public static void Build_Max_Heapify(double[] A){
        heap_size = A.length;
        for (int i = A.length/2; i >= 0; i--){
            Max_Heapify(A, i);
        }
    }

    /**
     * 堆排序
     * @param A
     */
    public static void Heap_Sort(double[] A){
        heap_size = A.length;
        Build_Max_Heapify(A);
        for (int i = A.length - 1; i >= 1; i--){
            double x = A[0];
            A[0] = A[i];
            A[i] = x;
            heap_size--;
            Max_Heapify(A, 0);
        }
    }

    /**
     * 返回堆A中具有最大关键字的元素
     * @param A A必须已经是最大堆
     * @return
     */
    public static double Heap_Maximum(double[] A){
        return A[0];
    }

    /**
     * 去掉并返回A中的具有最大关键字的元素
     * @param A A必须已经是最大堆
     * @return
     * @throws Exception
     */
    public static double Heap_Extract_Max(double[] A) throws Exception{
        if (heap_size < 1){
            throw new Exception("heap underflow");
        }
        double max = A[0];
        A[0] = A[heap_size - 1];
        heap_size--;
        Max_Heapify(A,0);
        return max;
    }

    /**
     * 将元素A[i]的关键字增加到key
     * @param A A必须已经是最大堆
     * @param i
     * @param key
     * @throws Exception
     */
    public static void Heap_Increase_Key(double[] A, int i, double key) throws Exception{
        if (key < A[i]){
            throw new Exception("new key is smaller than current key");
        }
        A[i] = key;
        while (i > 0 && A[Parent(i)] < A[i]){
            double x = A[i];
            A[i] = A[Parent(i)];
            A[Parent(i)] = x;
            i = Parent(i);
        }
    }

    /**
     * 把元素key插入到最大堆A中
     * @param A A必须已经是最大堆
     * @param key
     * @throws Exception
     */
    public static void Max_Heap_Insert(double[] A, double key) throws Exception{
        heap_size--;
        A[heap_size] = -10000000;
        Heap_Increase_Key(A, heap_size, key);
    }

    public static void print(double[] A){
        for (int i = 0; i < A.length; i++){
            System.out.print(A[i] + " ");
        }
        System.out.println();
    }

    public static double[] copy_array(double[] A){
        double[] B = new double[A.length];
        for (int i = 0; i < A.length; i++){
            B[i] = A[i];
        }
        return B;
    }

    public static void main(String[] args) throws Exception{
        double[] A = {12, 51, 54, 2, 51, 32, 35, 26, 78, 96, 20, 34, 56, 41, 72, 100, 34, 8, 61, 94, 52, 10};
        double[] B = copy_array(A);
        Heap_Sort(B);
        System.out.print("Insertion_Sort: ");
        print(B);
        B = copy_array(A);
        Build_Max_Heapify(B);
        print(B);
        System.out.println(Heap_Maximum(B));
        System.out.println(Heap_Extract_Max(B));
        print(B);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值