美文网首页
OrderedArrayPQ

OrderedArrayPQ

作者: 賈小強 | 来源:发表于2018-03-28 11:04 被阅读0次

简书 賈小強
转载请注明原创出处,谢谢!

package com.lab1.test2;

public class OrderedArrayPQ<Key extends Comparable<Key>> {
    private Key[] a = (Key[]) new Comparable[5];
    private int n;

    private boolean isEmpty() {
        return n == 0;
    }

    private int size() {
        return n;
    }

    private Key delMax() {
        return a[--n];
    }

    private void exch(int i, int j) {
        Key temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    private boolean less(Key v, Key w) {
        return v.compareTo(w) < 0;
    }

    private void put(Key key) {
        int i = n - 1;
        for (; i > 0 && less(key, a[i]); i--) {
            a[i+1] = a[i];
        }
        a[i + 1] = key;
        n++;
    }

    public static void main(String[] args) {
        OrderedArrayPQ<String> pq = new OrderedArrayPQ<>();
        pq.put("bill");
        pq.put("jack");
        pq.put("lucy");
        System.out.println(pq.delMax());
        System.out.println(pq.delMax());
        System.out.println(pq.delMax());
        System.out.println(pq.size());
        System.out.println(pq.isEmpty());
    }
}

输出

lucy
jack
bill
0
true

Happy learning !!

相关文章

  • OrderedArrayPQ

    简书 賈小強转载请注明原创出处,谢谢! 输出 Happy learning !!

网友评论

      本文标题:OrderedArrayPQ

      本文链接:https://www.haomeiwen.com/subject/zfkycftx.html