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. 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 We will introduces some concepts of Heap and show how to implement MAX-HEAP using some examples. Code: Heap.java Reference: An online tutorial We will show how sorting can be implemented in terms of a heap. Code: Heap.java Reference: An online demo 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
CS112b1 |
||