> {private ..._多链表找topk">
赞
踩
/** * 基于有序数组的优先序列 * @author XY * */ @SuppressWarnings("unchecked") public class PriorityB<Key extends Comparable<Key>> { private final int len_default=8; private Key[] pq; private int len; private int size; public PriorityB(){ this.len=len_default+1; pq=(Key[])new Comparable[len]; size=0; } public PriorityB(int len){ this.len=len+1; pq=(Key[])new Comparable[len]; size=0; } public PriorityB(Key[] key){ this.len=key.length+1; pq=(Key[])new Comparable[len]; for (int i = 0; i < key.length; i++) { insert(key[i]); } } public void insert(Key a){ pq[++size]=a; Key temp; int i=size; while(i>1 && less(pq[i], pq[i-1]) ){ //数组越界的约束应该在任何条件的前面 temp=pq[i]; pq[i]=pq[i-1]; pq[i-1]=temp; i--; } } public Key max(){ return pq[size--]; } public Key delMax(){ if (size==0)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。