CSC212 Lab 12 2014

From dftwiki3
Revision as of 19:37, 9 November 2014 by Thiebaut (talk | contribs) (Lab Problem #1)
Jump to: navigation, search

--D. Thiebaut (talk) 19:37, 9 November 2014 (EST)


Lab Problem #1


  • Java uses heaps, and calls them PriorityQueues.
  • Instead of keeping the largest element at the top of the heap, PriorityQueues keep the smallest elements at the top.
  • Try the example below to see how to use a PriorityQueue


import java.util.PriorityQueue;

public class HeapPriorityQueue {
	
	public static void main(String[] args) {
		PriorityQueue<Integer> heap = new PriorityQueue<Integer>();

		heap.add( 1 );
		heap.add( 20 );
		heap.add( 5 );
		heap.add( 100 );
		while ( ! heap.isEmpty() )
			System.out.println( heap.poll() );
	}
}


Question 1
Using some of the code/functions from this [[Quicksort.java| page], create a function called heapsort( int[] A ) that will use a priority queue to sort the array of ints A.