Professional Documents
Culture Documents
Sorting Algorithms
Sorting Algorithms
Input:
A sequence of n numbers <a1, a2, .., an>
Output:
A permutation (reordering) <a1, a2, ..,
an> of the input sequence such that a1
a2 ..an
Mergesort
Quicksort
Insertion Sort
ij1
while i > 0 and A[i] > key do
A[i+1] A[i]
ii1
A[i+1] key
16
2 3
14 10
4 6
5 7
8 7 9 3
8 9 10
2 4 1
buildHeap extractMax
Heap
BUILD-MAX-HEAP(H) produces a max-heap from an
unordered input array; runs in linear time
Build-Max-Heap (A)
For K length [A] downto 2 do
exchange A[1] and A[K]
heapsize[A] heapsize [A] 1
Max-Heapify (A, 1)
Heaps
H = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}
H 1
4
2 3
1 3
4 5 6 7
2 16 9 10
8 9 10
14 8 7
H 1
4
2 3
MAX-HEAPIFY(H, 5) 1 3
4 5 6 7
2 16 9 10
8 9 10
14 8 7
H 1
4
2 3
1 3
4 5 6 7
2 16 9 10
8 9 10
14 8 7
MAX-HEAPIFY(H, 4) H 1
4
2 3
1 3
4 5 6 7
2 16 9 10
8 9 10
14 8 7
H 1
4
2 3
1 3
4 5 6 7
2 16 9 10
8 9 10
14 8 7
MAX-HEAPIFY(H, 3) H 1
4
2 3
1 3
4 5 6 7
14 16 9 10
8 9 10
2 8 7
H 1
4
2 3
1 3
4 5 6 7
14 16 9 10
8 9 10
2 8 7
H 1
MAX-HEAPIFY(H, 2)
4
2 3
1 10
4 5 6 7
14 16 9 3
8 9 10
2 8 7
H 1
4
2 3
1 10
4 5 6 7
14 16 9 3
8 9 10
2 8 7
MAX-HEAPIFY(H, 1) H 1
4
2 3
16 10
4 5 6 7
14 7 9 3
8 9 10
2 8 1
H 1
4
2 3
16 10
4 5 6 7
14 7 9 3
8 9 10
2 8 1
H 1
FINAL TREE 16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10
2 4 1
Exercises:
1. Illustrate the operation of BUILD-MAX-
HEAP on array
H = {5, 3, 17, 10, 84, 19, 6, 22, 9}
2. Illustrate the operation of BUILD-MAX-
HEAP on array
H = {27, 17, 3, 16, 13, 10, 1, 5, 7, 12, 4,
8, 9, 0}
3. Illustrate the operation of Heapsort on
the array H = {5, 13, 2, 25, 7, 17, 20, 8,
4}
Priority Queues (Max)
A priority Queue is a data structure for
maintaining a set S elements, each with an
associated value called Key.
Operations:
(a) Max-Heap-Insert (S, x) inserts the
element x into the set S.
(b) Heap-Maximum (S) returns the element
of S with the largest key
(c) Heap-Extract-Max (S) removes and
returns the element of S with the largest
key
(d) Heap-Increase-Key(S, x, k) increases the
value of element xs key to the new value
k, which is assumed to be at least as large
as xs current key value.
Heap-Extract-Max (A) O(lg n)
PQ 1
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10
2 4 1
PQ 1
1
Extracting PQ[1], MAX-HEAPIFY(PQ, 1) 2 3
14 10
4 5 6 7
8 7 9 3
8 9
2 4
max = 16
PQ 1
1
2 3 max = 16
14 10
4 5 6 7
8 7 9 3
8 9
2 4
PQ 1
MAX-HEAPIFY(PQ, 2)
14
2 3
1 10
4 5 6 7
8 7 9 3
8 9
2 4
max = 16
PQ 1
14 max = 16
2 3
1 10
4 5 6 7
8 7 9 3
8 9
2 4
PQ 1
MAX-HEAPIFY(PQ, 4) 14
2 3
8 10
4 5 6 7
1 7 9 3
8 9
2 4
max = 16
PQ 1
14
2 3
max = 16
8 10
4 5 6 7
1 7 9 3
8 9
2 4
PQ 1
14
MAX-HEAPIFY(PQ, 9) 2 3
8 10
4 5 6 7
4 7 9 3
8 9
2 1
max = 16
PQ 1
14
2 3
8 10
4 5 6 7
4 7 9 3
8 9
2 1
max = 16
Reposition <node>
Or simply stated as
Add a new node to the next available
position of the heap
Assign an arbitrary value to it
Heap-Increase-Key (<heap>,
<new_node>, <key>)
Max-Heap-Insert (PQ, x) inserts an element x
into the set PQ
Ex. Insert(PQ, 15)
PQ 1
16
2 3
14 10
4 5 6 7
8 7 9 13
8
2
PQ
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9
2 15
PQ 1
16
2 3
14 10
4 5 6 7
15 7 9 3
8 9
2 8
PQ 1
16
2 3
15 10
4 5 6 7
14 7 9 3
8 9
2 8
PQ 1
16
2 3
15 10
4 5 6 7
14 7 9 3
8 9
2 8
Exercise:
Illustrate the operation of MAX-HEAP-
INSERT(H, 10) on the heap H = {15, 13,
9, 5, 12, 8, 7, 4, 0, 6, 2, 1}.
Note:
Max-heaps are used in Heapsort.
Heapsort is an efficient sorting algorithm
with a worst case time complexity of O(n
log n) as compared to bubble sort, balloon
sort, insertion sort, and selection sorts
O(n2).
Max-Heap-Delete (A, key)
http://linux.wku.edu/~lamonml/al
gor/sort/heap.html
http://www.mkssoftware.com/do
cs/man3/heapsort.3.asp
http://www.csse.monash.edu.au/
~lloyd/tildeAlgDS/Sort/Heap/
http://www.cs.northwestern.edu/
academics/courses/311/notes/1
8.pdf