-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueHeap.java
More file actions
70 lines (55 loc) · 1.12 KB
/
PriorityQueueHeap.java
File metadata and controls
70 lines (55 loc) · 1.12 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.nio.channels.Pipe.SinkChannel;
public class PriorityQueueHeap {
private int N = 0;
private int[] PQ;
public PriorityQueueHeap (int capacity) {
PQ = new int[capacity];
}
public boolean isEmpty() {
return N==0;
}
public void insert(int item) {
PQ[++N] = item;
swim(N);
}
public int delMax() {
int max = PQ[1];
PQ[1]=PQ[N--];
sink(1);
return max;
}
public void sink(int k) {
while(2*k<=N)
{
int j = 2*k;
if(j<N&&PQ[j]<PQ[j+1]) j++;
if(PQ[k]>=PQ[j]) break;
swap(PQ, k, j);
k = j;
}
}
public void swim(int k) {
while(k>1)
{
if(PQ[k]>PQ[k/2])
swap(PQ,k,k/2);
k=k/2;
}
}
private void swap(int[] arr,int i,int j)
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PriorityQueueHeap priorityQueueHeap = new PriorityQueueHeap(10);
for (int i = 0; i < 5; i++) {
priorityQueueHeap.insert(i);
}
for (int i = 0; i < 5; i++) {
System.out.println(priorityQueueHeap.delMax());
}
}
}