-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.java
199 lines (173 loc) · 3.8 KB
/
HeapSort.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package searchengine;
import java.util.ArrayList;
/**
* The Heap Sort algorithm
* @author Nhien (Ricky) Lam
*
*/
public class HeapSort {
// size of the Heap
private int heapsize;
/**
* Gets the size of the Heap
* @return the size of the Heap
*/
public int getHeapSize()
{
return heapsize;
}
/**
* Returns the left child of the target node
* @param i the target node
* @return the left child of the target node
*/
public int getLeftChild(int i)
{
return 2*i;
}
/**
* Returns the right child of the target node
* @param i the target node
* @return the right child of the target node
*/
public int getRightChild(int i)
{
return 2*i+1;
}
/**
* Returns the parent child of the target node
* @param i the target node
* @return the parent of the target node
*/
public int getParent(int i)
{
return i/2;
}
/**
* Maintains the max-heap property
* @param A an array A
* @param i an index in array A
*/
public void MaxHeapify(int A[], int i)
{
int l = getLeftChild(i); // left child
int r = getRightChild(i); // right child
int largest = i;
// Check left child
// if left child > largest
if(l <= heapsize && A[l] > A[i])
{
largest = l; // largest = left child
}
else
{
largest = i;
}
//Check right child
// if right child > largest
if(r <= heapsize && A[r] > A[largest])
{
largest = r; // largest = right child
}
// largest is not the root
if(largest != i)
{
// swap A[i] with A[largest]
int temp = A[i];
A[i] = A[largest];
A[largest] = temp;
// recursively call MaxHeapify
MaxHeapify(A, largest);
}
}
/**
* Produces a max-heap from an unordered input array
* @param A an array A
*/
public void BuildMaxHeap(int A[])
{
this.heapsize = A.length -1;
//Call Max-Heapify on each element in a bottom-up manner
for(int i = (A.length/2)-1; i >= 0; i--)
{
MaxHeapify(A, i);
}
}
/**
* Sorts an array in place
* @param A array A
*/
public void Heapsort(int A[])
{
BuildMaxHeap(A);
for (int i = heapsize; i >= 0; i--)
{
// swap A[0] with A[i]
int temp = A[0];
A[0] = A[i];
A[i] = temp;
this.heapsize--;
MaxHeapify(A, 0);
}
}
/**
* Returns the element of array A with the largest key
* @param A array A
* @return the element of array A with the largest key.
*/
public int HeapMaximum(int A[])
{
return A[0];
}
/**
* Increases the value of element i's key to the new
* value k, where k >= i's current key value
* @param A array A
* @param i the index of the target element in array A
* @param key a new key
*/
public void HeapIncreaseKey(int A[], int i, int key)
{
if(key < A[i])
{
throw new IllegalArgumentException("ERROR: New key is smaller than current key.");
}
A[i] = key;
while(i > 0 && A[getParent(i)] < A[i])
{
// swap A[i] with A[getParent(i)]
int temp = A[i];
A[i] = A[getParent(i)];
A[getParent(i)] = temp;
i = getParent(i);
}
}
/**
* Inserts the element key into array A
* @param A array A
* @param key the inserted key
*/
public void MaxHeapInsert(int A[], int key)
{
this.heapsize++;
A[this.heapsize] = Integer.MIN_VALUE;
HeapIncreaseKey(A, heapsize, key);
}
/**
* Removes and returns the element of array A with the largest key.
* @param A array A
* @return the element of array A with the largest key
*/
public int HeapExtractMax(int A[])
{
if(this.heapsize < 0)
{
throw new IllegalArgumentException("ERROR: Heap underflow.");
}
int max = A[0];
A[0] = A[this.heapsize];
this.heapsize--;
MaxHeapify(A, 0);
return max;
}
}