001package algs24;
002import stdlib.*;
003import java.util.Arrays;
004
005public class FixedPQSortedDecreasing implements PQ {
006        private int N;
007        private double[] a;
008        
009        public FixedPQSortedDecreasing(int capacity){ N = 0; a = new double[capacity+1]; }      
010        public void toGraphviz()          { }
011        public String toString ()         { return Arrays.toString(a).replaceAll(" 0\\.0", " ").replaceAll("\\[0\\.0", "[ ").replaceAll("\\.0", ""); }
012        public int size()                 { return N; }
013        public boolean isEmpty()          { return N == 0; }
014        
015        public void insert(double x) {
016                if (x == 0) throw new Error ("No zeroes allowed");
017                if (N >= a.length-1) throw new Error("Overflow");
018                N++;
019                a[N] = x;
020                for (int j = N; j > 1 && a[j] > a[j-1]; j--)
021                        exch2(j, j-1);
022        }
023        public double min() {
024                return a[N];
025        }
026        public double delMin() {
027                double result = a[N]; 
028                a[N] = 0.0;
029                N--;
030                return result;
031        }
032        private void exch2(int i, int j) {
033                double oldi = a[i];
034                double oldj = a[j];
035                if (TestPQ.SHOW_STEPS) { StdOut.format("  exch(%2.0f,%2.0f)> %s\n", oldi, oldj, this); toGraphviz(); }
036                a[i] = oldj;
037                a[j] = oldi;
038        }
039}