File tree 1 file changed +6
-9
lines changed
1 file changed +6
-9
lines changed Original file line number Diff line number Diff line change @@ -42,18 +42,15 @@ public PriorityQueue(int size) {
42
42
public void insert (int value ) {
43
43
if (isFull ()) {
44
44
throw new RuntimeException ("Queue is full" );
45
- }
46
- if (nItems == 0 ) {
47
- queueArray [0 ] = value ;
48
45
} 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
52
49
j --;
53
50
}
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
+ }
57
54
}
58
55
59
56
/**
You can’t perform that action at this time.
0 commit comments