Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions DataStructures/Queues/PriorityQueues.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,15 @@ public PriorityQueue(int size) {
public void insert(int value) {
if (isFull()) {
throw new RuntimeException("Queue is full");
}
if (nItems == 0) {
queueArray[0] = value;
} else {
int j = nItems;
while (j > 0 && queueArray[j - 1] > value) {
queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
int j = nItems - 1; // index of last element
while (j >= 0 && queueArray[j] > value) {
queueArray[j + 1] = queueArray[j]; // Shifts every element up to make room for insertion
j--;
}
queueArray[j] = value; // Once the correct position is found the value is inserted
}
nItems++;
queueArray[j + 1] = value; // Once the correct position is found the value is inserted
nItems++;
}
}

/**
Expand Down