![]() |
CS 216 - Data Structures
Homework #7 |
NOTE: This work is to be done in groups of 2-3.
Define a class HeapSorter<E extends Comparable<E>> with a default constructor and a method public void sort(E[] a) that sorts a in place without making use of an additional array. You are encouraged to adapt the text pseudocode. (Warning: Pseudocode arrays use 1-based rather than 0-based indexing, so take extra care with off-by-one errors.) Here is some sample test code illustrating one possible use:
Scanner in = new Scanner(System.in);
System.out.print("How many integers? ");
int size = in.nextInt();
System.out.print("How many possible random values? ");
int values = in.nextInt();
Integer[] array = new Integer[size];
Random random = new Random();
for (int i = 0; i < size; i++)
array[i] = random.nextInt(values);
new HeapSorter().sort(array);
for (int i = 0; i < size - 1; i++)
if (array[i] > array[i + 1]) {
System.out.println("Array not sorted");
System.exit(1);
}
System.out.println("Array sorted");