Skip to content

Commit 947127c

Browse files
make code less
1 parent f0bdd2b commit 947127c

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

DataStructures/Queues/PriorityQueues.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,15 @@ public PriorityQueue(int size) {
4242
public void insert(int value) {
4343
if (isFull()) {
4444
throw new RuntimeException("Queue is full");
45-
}
46-
if (nItems == 0) {
47-
queueArray[0] = value;
4845
} else {
49-
int j = nItems;
50-
while (j > 0 && queueArray[j - 1] > value) {
51-
queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
46+
int j = nItems - 1; // index of last element
47+
while (j >= 0 && queueArray[j] > value) {
48+
queueArray[j + 1] = queueArray[j]; // Shifts every element up to make room for insertion
5249
j--;
5350
}
54-
queueArray[j] = value; // Once the correct position is found the value is inserted
55-
}
56-
nItems++;
51+
queueArray[j + 1] = value; // Once the correct position is found the value is inserted
52+
nItems++;
53+
}
5754
}
5855

5956
/**

0 commit comments

Comments
 (0)