-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueueUsingArrayMaxToMin.java
41 lines (41 loc) · 1.11 KB
/
PriorityQueueUsingArrayMaxToMin.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
public class PriorityQueueUsingArrayMaxToMin extends PriorityQueueUsingArray{
@Override
public void add(int val){
if(size==0){
arr[size++]=val;
return ;
}
if(size==max){
System.out.println("Cannot Add Mode Size Overflow");
return ;
}
int i;
arr[size++]=val;
for(i=size-1;i>0;i--){
if(arr[i-1]<arr[i]){
int temp=arr[i-1];
arr[i-1]=arr[i];
arr[i]=temp;
}
else{
break;
}
}
}
public static void main(String args[]){
PriorityQueueUsingArrayMaxToMin pq=new PriorityQueueUsingArrayMaxToMin();
pq.add(6);
pq.add(25);
pq.add(10);
pq.add(15);
pq.add(13);
pq.add(50);
System.out.println(pq.size);
System.out.println(pq.poll());
System.out.println(pq.poll());
System.out.println(pq.poll());
System.out.println(pq.poll());
System.out.println(pq.poll());
System.out.println(pq.poll());
}
}