CS112b1
LAB 07 - Overview of Programming Assignment 4 and Heap Sort

Objectives

  1. A generic interface for sorting algorithms
  2. Overview of programming assignment 4
  3. An introduction to Heap data structure
  4. Heap operations
  5. HeapSort
  6. Exercises

Preamble

In this Lab, we first present a generic interface for various sorting algorithms. Then, we are to give an overview of the programming assignment 4, in which you need to implement the heap sort as well as a sorting algorithm with average complexity O(N^2) in terms of the input size N. As you may know, the heap sort has an average complexity O(N lg N). In this assignment, you are to count the number of comparison and swap operations incurred in both sorting algorithms.


A generic interface for sorting algorithms

Instead of using (down)casting to handle the issue of polymorphism, we present a generic interface for sorting algorithms using Java generics available in Java 1.5.

Code: GenericSort.java


Heap

We will introduces some concepts of Heap and show how to implement MAX-HEAP using some examples.

Code: Heap.java

Reference: An online tutorial


Heap Sort

We will show how sorting can be implemented in terms of a heap.

Code: Heap.java

Reference: An online demo


Input

We usually use an array to efficiently implement a heap. So for the programming assignment 4, we need to know the size of the input so that we can allocate enough space to perform the heap sort. Instead of using a growable array, one simple way is to use a list to first read all items from the input file then allocate an array with the size of the list. We can use a built-in Java library ArrayList to do this.

Code: Input.java


Task

Since we have seen how to implement a MAX-HEAP, we now are ready to implement the MIN-HEAP, which is almost symmetric to MAX-HEAP.

Code: MinHeap.java

  • Complete the minHeapify() method in MinHeap.java
  • Complete the buildMinHeap() method in MinHeap.java
  • Complete the sort() method in MinHeap.java (sort the array in decending order)
  • gsubmit your code

CS112b1