-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue
More file actions
41 lines (31 loc) · 772 Bytes
/
PriorityQueue
File metadata and controls
41 lines (31 loc) · 772 Bytes
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
package edu;
public class PriorityQueue {
public static void main(String[] args) {
int[] elements = new int[3];
int[] priority = new int[3];
int count = 0;
elements[0] = 10;
priority[0] = 2;
count++;
elements[1] = 20;
priority[1] = 1;
count++;
elements[2] = 30;
priority[2] = 3;
count++;
while(count > 0) {
int highest = 0; // initialization
for(int i = 0; i < count; i++) { // 0 < 3 , 1 < 3, 2 < 3
if(priority[i] < priority[highest]) {
highest = i;
}
}
System.out.println("Served element: " + elements[highest] + " having priority (" + priority[highest] + ")");
for(int i = highest; i < count - 1; i++) {
elements[i] = elements[i+1];
priority[i] = priority[i+1];
}
count--;
}
}
}