-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
52 lines (41 loc) · 1.24 KB
/
Copy pathHeapSort.java
File metadata and controls
52 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cpsc331.assignment3;
import cpsc331.assignment3.ArrayMaxHeap;
import java.util.NoSuchElementException;
import cpsc331.collections.HeapFullException;
import java.util.ArrayList;
/**
*
* Provides an implementation of the HeapSort Algorithm
*
*/
public class HeapSort<T extends Comparable<T>> {
/**
*
* @param A an ArrayList with positive length storing non-null from an ordered
* type T<br><br>
*
* Precondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> An ArrayList A with positive length, storing non-null values from
* an ordered type T, is given as input.
* </li>
* </ol>
* Postcondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The entries of A have been reordered but are otherwise unchanged. </li>
* <li> A is sorted in nondecreasing order. That is, A[h] ≤ A[h+1] for every
* integer h such that 0 ≤ h ≤ A.size()−2
* </li>
* </ol>
*
*/
public void sort(ArrayList<T> A) {
ArrayMaxHeap<T> H = new ArrayMaxHeap<T>(A);
// For each index of the array (from last to first)...
for (int i = A.size() - 1; i > 0; i--) {
// Set the index of the array to the max value
T m = H.deleteMax();
A.set(i, m);
}
}
}