title: Sorted vs. Unsorted Data in a priority queue Why does un sort automatically and sorted wait?
body: <p>I understand that a min heap is the structure that the class uses because of its efficiency.  It seems to me that when unsorted data is given to a PQ it sorts it into a heap.  </p>
  
  <p>BUT when it is fed ascending elements according to the compareTo method it waits to sort it into a heap after the first action is preformed on the PQ.  </p>
  
  <p>Do you know why this is?  I don’t understand why it does not sort it automatically as is the case with unordered data.</p>
  
  <p>I have attached a program that I think demonstrates my issue.</p>
  
  <p>output:</p>
  
  <p>Unsorted Data:</p>
  
  <blockquote>
    <p>[A, B, D, C, L, F, E, J]</p>
    
    <p>A</p>
    
    <p>[B, C, D, J, L, F, E]</p>
    
    <p>[1, 2, 4, 3, 12, 6, 5, 10]</p>
    
    <p>1</p>
    
    <p>[2, 3, 4, 10, 12, 6, 5]</p>
  </blockquote>
  
  <p>Sorted data:</p>
  
  <blockquote>
    <p>[A, B, C, D, E, F, G, H]</p>
    
    <p>A</p>
    
    <p>[B, D, C, H, E, F, G]</p>
    
    <p>[1, 2, 3, 4, 5, 6, 7, 8]</p>
    
    <p>1</p>
    
    <p>[2, 4, 3, 8, 5, 6, 7]</p>
  </blockquote>
  
  <pre><code>import java.util.PriorityQueue;
  
  public class Queue2
  {
      public static void main(String[] args)
      {
          PriorityQueue&lt;String&gt; pQueue = new PriorityQueue&lt;String&gt;();
  
          pQueue.add("A");
          pQueue.add("C");
          pQueue.add("F");
          pQueue.add("B");
          pQueue.add("L");
          pQueue.add("D");
          pQueue.add("E");
          pQueue.add("J");
          System.out.println(pQueue); 
          System.out.println(pQueue.remove());
          System.out.println(pQueue);
  
          System.out.println();
  
          PriorityQueue&lt;Integer&gt; pQueue2 = new PriorityQueue&lt;Integer&gt;();
  
          pQueue2.add(1);
          pQueue2.add(3);
          pQueue2.add(6);
          pQueue2.add(2);
          pQueue2.add(12);
          pQueue2.add(4);
          pQueue2.add(5);
          pQueue2.add(10);
          System.out.println(pQueue2); 
          System.out.println(pQueue2.remove());
          System.out.println(pQueue2);
  
          System.out.println();
  
          PriorityQueue&lt;String&gt; pQueue3 = new PriorityQueue&lt;String&gt;();
  
          pQueue3.add("A");
          pQueue3.add("B");
          pQueue3.add("C");
          pQueue3.add("D");
          pQueue3.add("E");
          pQueue3.add("F");
          pQueue3.add("G");
          pQueue3.add("H");
          System.out.println(pQueue3); 
          System.out.println(pQueue3.remove());
          System.out.println(pQueue3);
  
          System.out.println();
  
          PriorityQueue&lt;Integer&gt; pQueue4 = new PriorityQueue&lt;Integer&gt;();
  
          pQueue4.add(1);
          pQueue4.add(2);
          pQueue4.add(3);
          pQueue4.add(4);
          pQueue4.add(5);
          pQueue4.add(6);
          pQueue4.add(7);
          pQueue4.add(8);
          System.out.println(pQueue4); 
          System.out.println(pQueue4.remove());
          System.out.println(pQueue4);
  
      }
  }
  </code></pre>