Priority Queue A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key.
Heap and Priority Queue Heap can be used to implement a priority queue.
Priority Queue There are two kinds of priority queue max-priority queue min-priority queue
Priority Queue Applications of priority queue Job scheduling on a shared computer Event-driven simulation
Priority Queue A max-priority queue supports the following operations INSERT(S,x), MAXIMUM(S) EXTRACT-MAX(S), INCREASE-KEY(S,x,k)
Priority Queue HEAP-MAXIMUM(A) return A[1]
Priority Queue HEAP-EXTRACT-MAX(A) if heap-size[A] < 1 then error “heap underflow” max    A[1] A[1]    A[heap-size[A]] heap-size[A]    heap-size[A]-1 MAX-HEAPIFY(A,1) return max
Priority Queue HEAP-INCREASE-KEY(A, i, key) if key < A[i] then error “new key is smaller than current key” A[i]    key while i > 1 and A[PARENT(i)] < A[i] do exchange A[i]    A[PARENT(i)] i    PARENT(i)
Priority Queue i (a) 16 2 9 8 3 10 7 14 4 1
Priority Queue i (b) 16 2 9 8 3 10 7 14 15 1
Priority Queue i (c) 16 2 9 15 3 10 7 14 8 1
Priority Queue i (d) 16 2 9 14 3 10 7 15 8 1
Priority Queue MAX-HEAP-INSERT(A, key) heap-size[A]    heap-size[A]+1 A[heap-size[A]]    - ∞ HEAP-INCREASE-KEY (A, heap-size[A], key)
Quick Sort Divide: Partition the array into two sub-arrays A[p . . q-1]  and A[q+1 . . r] such that each element of  A[p . . q-1] is less than or equal to A[q], which in turn less than or equal to each element of A[q+1 . . r]
Quick Sort Conquer: Sort the two sub-arrays A[p . . q-1]  and A[q+1 . . r] by recursive calls to quick sort.
Quick Sort Combine: Since the sub-arrays are sorted in place, no work is needed to combine them.
Quick Sort QUICKSORT(A, p, r) if p< r  then q    PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r)
Quick Sort PARTITION(A, p, r) x    A[r] i    p-1
Quick Sort for j    p to r-1 do if A[j] <= x then i   i+1 exchange A[i]       A[j] exchange A[i+1]       A[r] return i+1
Quick Sort (a) i 4 6 5 3 1 7 8 2 p, j r
Quick Sort (b) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (c) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (d) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (e) 4 6 5 3 8 7 1 2 j i p r
Quick Sort (f) 4 6 5 7 8 3 1 2 i p r j
Quick Sort (g) 4 6 5 7 8 3 1 2 i p r j
Quick Sort (h) 4 6 5 7 8 3 1 2 i p r
Quick Sort (i) 8 6 5 7 4 3 1 2 i p r

Algorithm: priority queue