Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0052678

Browse files
authored
feat: menambahkan algoritma queue prioritas (bellshade#151)
* feat: menambahkan algoritma queue prioritas Signed-off-by: slowy07 <[email protected]> * fix: menghapus file permutasi * fix: memperbaiki titik koma pada fungsi ``throw`` * fix: memperbaiki identifier * fix: memperbaiki cannot find symbol java.lang.String --------- Signed-off-by: slowy07 <[email protected]>
1 parent 3d3e45a commit 0052678

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package algorithm.datastructure.Queue.SourceCode;
2+
3+
/**
4+
* priority queue menambahkan elemen ke posisi
5+
* berdasarkan prioritasnya.
6+
* sehigga elemen terpenting ditempatkan di atas.
7+
* dalam contoh ini
8+
* diberikan angka yang lebih besar, prioritas
9+
* lebih tinggi. antrian dalam teori ini adalah tidak ada
10+
* ukuran tetap tetapi kita menggunakan implementasi array
11+
* benar
12+
*/
13+
class PriorityQueue {
14+
private int maxSize;
15+
private int[] queueArray;
16+
private int nItems;
17+
18+
public PriorityQueue() {
19+
/* jika kapasitas tidak ditentukan, ukuran standar 11
20+
* akan diguankan
21+
* capacity = max + 1
22+
* karena kita tidak dapat mengakses elemen ke-0 dari PQ,
23+
* dan ke
24+
* mengakomodir (maks) elemen yang diperlukan kapasitas
25+
* menjadi maks+1
26+
* induk di posisi k, anak di posisi (k*2, k*2+1),jika
27+
* kita gunakan posisi 0 dalam antrian kita,anaknya akan
28+
* berada di = *(0*2,0*2 + 1) ->(0,0).
29+
* inilah mengapa kita mulai dari posisi 1
30+
*/
31+
int size = 11;
32+
maxSize = size + 1;
33+
queueArray = new int[maxSize];
34+
nItems = 0;
35+
}
36+
37+
/**
38+
* konstruktor dari parmater
39+
* @param size
40+
*/
41+
public PriorityQueue(int size) {
42+
maxSize = size + 1;
43+
queueArray = new int[maxSize];
44+
nItems = 0;
45+
}
46+
47+
/*
48+
* fungsi pembantu untuk implementasi PQ max-heap
49+
* fungsi akan membantu menurunkan simpul
50+
* induk ke yang benar
51+
*/
52+
private void swim(int pos) {
53+
while (pos > 1 && (queueArray[pos / 2] < queueArray[pos])) {
54+
int temp = queueArray[pos];
55+
queueArray[pos] = queueArray[pos / 2];
56+
queueArray[pos / 2] = temp;
57+
pos = pos / 2;
58+
}
59+
}
60+
61+
/**
62+
* fungsi pembantu untuk implementasi PQ max-heap
63+
* fungsi akan membantu menurunkan simpul induk
64+
* ke posisi yang benar
65+
*/
66+
private void sink(int pos) {
67+
// periksa apakah posisi simpul adalah simpul induk
68+
while (2 * pos <= nItems) {
69+
int current = 2 * pos;
70+
// lompat ke posisi simpul anak
71+
// bandingkan kedua anaknya dengan anaknya dengan
72+
// yang lebih besar
73+
if (current < nItems && queueArray[current] < queueArray[current + 1]) current++;
74+
// jika node induk lebih besar, operasi sink selesai
75+
// maka kita putuskan lingkarannya
76+
if (queueArray[pos] >= queueArray[current]) break;
77+
// jika tidak menukar
78+
int temp = queueArray[pos];
79+
queueArray[pos] = queueArray[current];
80+
queueArray[current] = temp;
81+
pos = current;
82+
// tukarkan posisi induk ke posisi child dalam larik
83+
}
84+
}
85+
86+
public void insert(int value) {
87+
if (isFull()) {
88+
throw new RuntimeException("queue is penuh");
89+
} else {
90+
queueArray[++nItems] = value;
91+
swim(nItems);
92+
}
93+
}
94+
95+
public int remove() {
96+
if (isEmpty()) {
97+
throw new RuntimeException("queue kososng");
98+
} else {
99+
int max = queueArray[1];
100+
101+
// swap max adn last element
102+
int temp = queueArray[1];
103+
queueArray[1] = queueArray[nItems];
104+
queueArray[nItems] = temp;
105+
queueArray[nItems--] = 0;
106+
sink(1);
107+
108+
return max;
109+
}
110+
}
111+
112+
/**
113+
* memeriksa apayang ada di depan
114+
* antrian
115+
*/
116+
public int peek() {
117+
return queueArray[1];
118+
}
119+
120+
/**
121+
* return true jika queue kosong
122+
*/
123+
public boolean isEmpty() {
124+
return (nItems == 0);
125+
}
126+
127+
/**
128+
* return true jika queue full
129+
*/
130+
public boolean isFull() {
131+
return (nItems == maxSize - 1);
132+
}
133+
134+
public int getSize() {
135+
return nItems;
136+
}
137+
}

0 commit comments

Comments
 (0)