Hello. I can not understand the principle of insertion into the priority queue, and specifically the shift up. For example, I have an array consisting of 6 elements.
int[] j = {5, 10, 15, 20, 25, 30}; And I want to add item = 16;
public void insert(long item) // Вставка элемента { int j; if(nItems==0) // Если очередь пуста, queArray[nItems++] = item; // вставляем в ячейку 0 else // Если очередь содержит элементы { for(j=nItems-1; j>=0; j--) // Перебор в обратном направлении { if( item > queArray[j] ) // Если новый элемент больше, queArray[j+1] = queArray[j]; // сдвинуть вверх else // Если меньше, break; // сдвиг прекращается } queArray[j+1] = item; // Вставка элемента nItems++; } } I go through the array until the item value exceeds the jth value. Ie when j = 2, queArray[j] will be equal to 15. Next, you need to move the elements up. For this, in theory, this part should be responsible:
queArray[j+1] = queArray[j]; But in this way, I do not increase the size of the array and do not move it, I simply assign queArray[j+1] to queArray[j] , 20 = 15 and thus simply remove the value 20 from the array and replace it with 15. Where I wrong?
j = 2;? And is it possible in the queue, just rub the element, instead of a shift. This is an example of the algorithms and data structures of Lafor. MB code is wrong there, could you check? - Kojer DefornItems - 1and goes down - etki