mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
@ -42,19 +42,16 @@ class PriorityQueue {
|
|||||||
public void insert(int value) {
|
public void insert(int value) {
|
||||||
if (isFull()) {
|
if (isFull()) {
|
||||||
throw new RuntimeException("Queue is full");
|
throw new RuntimeException("Queue is full");
|
||||||
}
|
|
||||||
if (nItems == 0) {
|
|
||||||
queueArray[0] = value;
|
|
||||||
} else {
|
} else {
|
||||||
int j = nItems;
|
int j = nItems - 1; // index of last element
|
||||||
while (j > 0 && queueArray[j - 1] > value) {
|
while (j >= 0 && queueArray[j] > value) {
|
||||||
queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
|
queueArray[j + 1] = queueArray[j]; // Shifts every element up to make room for insertion
|
||||||
j--;
|
j--;
|
||||||
}
|
}
|
||||||
queueArray[j] = value; // Once the correct position is found the value is inserted
|
queueArray[j + 1] = value; // Once the correct position is found the value is inserted
|
||||||
}
|
|
||||||
nItems++;
|
nItems++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the element from the front of the queue
|
* Remove the element from the front of the queue
|
||||||
|
Reference in New Issue
Block a user