algoritmauzmanı
Sign in
TREN
5.5Priority Queue
Lesson 5.5.6

Which structure? Sorted, unsorted and heap

The same can be built with different structures, and each puts the work in a different place. Some keep insertion cheap and struggle at removal; others do the opposite. This lesson sets the four ways side by side and shows when to choose which.

What you will learn
  • The insert and remove times of an unsorted array, a sorted array and a sorted linked list
  • Why a binary heap does both operations in , as a short preview
  • Which structure to choose for a given workload
6 min

1Four ways side by side

In the table n is the number of elements in the queue. All of them are min priority queues: the smallest value has the highest priority.

StructureinsertremoveMinpeek
Unsorted arrayO(1)O(n)O(n)
Sorted array (large to small)O(n)O(1)O(1)
Sorted linked list (small to large)O(n)O(1)O(1)
Binary heapO(log n)O(log n)O(1)

2Unsorted array: easy to insert, hard to remove

The new element is written at the end of the array, just like inserting at the end: . But the highest-priority element may be in any cell. To remove it, the whole array is scanned for the smallest value: . The last element is moved into the cell that was found and size goes down by one; the order does not matter, so no shifting is needed. peek does the same scan.

This makes sense when elements are inserted very often and removed rarely. Note that moving the last element can break the arrival order of equal-priority elements.

3Sorted array and sorted linked list: easy to remove

Both sorted structures put the work into insertion. In the array elements shift to open a slot for the new element; in the linked list there is a walk to the right place. Both take O(n). In return the highest-priority element always sits at one end, so removal and peek are O(1).

The difference between the two is the familiar one between an array and a linked list. The array's capacity is fixed in advance and its elements sit side by side. The linked list has no limit, but every node carries an extra pointer and every insertion needs a memory allocation.

4Binary heap: both operations are fast

Heaps come in detail in the trees section. Here we only look at why they are fast. A binary heap is a tree where every node has at most two children, and it is stored in an array. Its only rule: every node is smaller than or equal to its children. So the smallest value is always at the root and peek takes O(1).

Insertion puts the new element at the very end, then the element moves up by swapping with any larger parent. Removal takes the root, moves the last element to the root and moves it down by swapping with its smaller child. The tree is balanced, so its height is about log n. Both operations walk a single path between the root and a leaf: O(log n).

Unlike the sorted structures, a heap does not keep the arrival order of equal-priority elements. If the order matters, every element gets an arrival number, and that number is checked when priorities are equal.

5When does the difference show?

The time of a single operation can mislead. Picture n elements inserted into the queue and then all of them removed. With the sorted structures the insertions add up to work proportional to n². With a heap all the work is proportional to n log n.

nSorted structure (about n²)Heap (about n log n)
1,0001 million10 thousand
100,00010 billion1.7 million
1,000,0001 trillion20 million

With a few hundred elements the difference does not matter and the simplicity of the sorted structures wins. With a million elements the heap does less than one ten-thousandth of the sorted structure's work.

6Which one to choose?

  • Many insertions, few removals: an unsorted array. For example, all records are collected and only a few smallest values are needed at the end.
  • Few elements, frequent peeks: a sorted array or a sorted linked list. The code is short and the highest-priority element is always at hand.
  • Equals must stay in order: the sorted structures do this by themselves.
  • Many elements, frequent insertions and removals: a heap. In real programs this is the default choice.

Ready-made libraries use heaps too. Java's java.util.PriorityQueue and Python's heapq module are binary heaps, and both give the smallest value first by default. The C standard library has no ready-made priority queue.

7Try it

A program will insert 10,000 elements and then remove the smallest value only once. Which structure does the least work?

Show the answer

The answer is A. In an unsorted array every insertion is O(1) and the single removal does one O(n) scan. In the sorted structures every insertion is O(n); 10,000 insertions add up to far more.

How long do insertion and removal take with a binary heap?

Show the answer

The answer is B. Insertion walks the new element up; removal walks the element moved to the root down. Both take as many steps as the height of the tree, about log n.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.