Heap Sort Daa
Heap Sort Daa
A B C D E F G
1 2 3 4 5 6 7
Full Binary Tree/ Complete Binary Tree
A full binary Tree is a Binary Tree in which all
internal nodes have degree and all levels are at
the same level.
A heap is defined to be a
complete binary tree with the
property that the value of each
node is at least as large as the
value of its children.
HEAP
The binary heap data structures is an array that
can be viewed as a complete binary tree. Each
node of the binary tree corresponds to an element
of the array. The array is completely filled on all
levels except possibly lowest.
19
12 16
1 4 7
19 12 16 1 4 7
Array A
HEAP
The root of the tree A[1] and given index i of a
node, the indices of its parent, left child and right
child can be computed
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
HEAP ORDER PROPERTY
For every node v, other than the root, the key
stored in v is greater or equal (smaller or equal
for max heap) than the key stored in the parent
of v.
12 16
1 4 7
19 12 16 1 4 7
Array A
MIN HEAP EXAMPLE
1
4 16
7 12 19
1 4 16 7 12 19
Array A
INSERTION
Algorithm
1. Add the new element to the next available position at
the lowest level
2. Restore the max-heap property if violated
General strategy is percolate up (or bubble up): if the
parent of the element is smaller than the element, then
interchange the parent and child.
OR
12 16 12 16
4 7 1 4 7 17
1
Insert 17
19
12 17
swap
1 4 7 16
Delete min
Copy the last number to the root ( overwrite the
minimum element stored there ).
Restore the min heap property by percolate down.
HEAP SORT
A sorting algorithm that works by first organizing
the data to be sorted into a special type of binary tree
called a heap
PROCEDURES ON HEAP
Heapify
Build Heap
Heap Sort
HEAPIFY
Heapify picks the largest child key and compare it to the
parent key. If parent key is larger than heapify quits,
otherwise it swaps the parent key with the largest child
key. So that the parent is now becomes larger than its
children.
Heapify(A, i)
{
l left(i)
r right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest l
else largest i
if r <= heapsize[A] and A[r] > A[largest]
then largest r
if largest != i
then swap A[i] A[largest]
Heapify(A, largest)
}
BUILD HEAP
We can use the procedure 'Heapify' in a bottom-up fashion
to convert an array A[1 . . n] into a heap. Since the
elements in the subarray A[n/2 +1 . . n] are all leaves, the
procedure BUILD_HEAP goes through the remaining
nodes of the tree and runs 'Heapify' on each one. The
bottom-up order of processing node guarantees that the
subtree rooted at children are heap before 'Heapify' is run
at their parent.
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2 //down to 1
do Heapify(A, i)
}
HEAP SORT ALGORITHM
The heap sort algorithm starts by using procedure BUILD-
HEAP to build a heap on the input array A[1 . . n]. Since
the maximum element of the array stored at the root A[1],
it can be put into its correct final position by exchanging it
with A[n] (the last element in A). If we now discard node n
from the heap than the remaining elements can be made
into heap. Note that the new element at the root may
violate the heap property. All that is needed to restore the
heap property.
Heapsort(A)
{
Buildheap(A)
for i length[A] //down to 2
do swap A[1] A[i]
heapsize[A] heapsize[A] - 1
Heapify(A, 1)
}
Example: Convert the following array to a heap
16 4 7 1 12 19
16
4 7
1 12 19
16 16
4 7 4 19
swap
12 19 1 12 7
1
16 19
swap
12 19 12 16
swap
4 7 1 4 7
1
HEAP SORT
The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data
19
12 16
1 4 7
EXAMPLE OF HEAP SORT
Take out biggest
19
12 16
Move the last element
to the root
1 4 7
Sorted:
Array A
12 16 1 4 7 19
7
swap
HEAPIFY()
12 16
1 4
Sorted:
Array A
7 12 16 1 4 19
16
12 7
1 4
Sorted:
Array A
16 12 7 1 4 19
Take out biggest
16
Move the last element
to the root
12 7
1 4
Sorted:
Array A
12 7 1 4 16 19
4
12 7
Sorted:
Array A
4 12 7 1 16 19
swap 4
HEAPIFY()
12 7
Sorted:
Array A
4 12 7 1 16 19
12
4 7
Sorted:
Array A
12 4 7 1 16 19
Take out biggest
12
Move the last
element to the
root 4 7
Sorted:
Array A
4 7 1 12 16 19
1
swap
4 7
Sorted:
Array A
1 4 7 12 16 19
7
4 1
Sorted:
Array A
7 4 1 12 16 19
Take out biggest
7
Move the last
element to the
4 1 root
Sorted:
Array A
1 4 7 12 16 19
swap 1
HEAPIFY()
4
Sorted:
Array A
4 1 7 12 16 19
Take out biggest
Move the last 4
element to the
root
1
Sorted:
Array A
1 4 7 12 16 19
Take out biggest
1
Sorted:
Array A
1 4 7 12 16 19
Sorted:
1 4 7 12 16 19
TIME ANALYSIS
Build Heap Algorithm will run in O(n) time
There are n-1 calls to Heapify each call requires
O(log n) time
Heap sort program combine Build Heap program
and Heapify, therefore it has the running time of
O(n log n) time
Total time complexity: O(n log n)
COMPARISON WITH QUICK
SORT AND MERGE SORT
Quick sort is typically somewhat
faster, due to better cache behavior
and other factors, but the worst-case
running time for quick sort is O (n2),
which is unacceptable for large data
sets and can be deliberately
triggered given enough knowledge of
the implementation, creating a
security risk.
COMPARISON WITH QUICK SORT AND
MERGE SORT (CONT)
Heap sort also competes with merge sort, which
has the same time bounds, but requires Ω(n)
auxiliary space, whereas heap sort requires only
a constant amount.
Heap sort also typically runs more quickly in
practice. However, merge sort is simpler to
understand than heap sort, is a stable sort,
parallelizes better, and can be easily adapted to
operate on linked lists and very large lists stored
on slow-to-access media such as disk storage or
network attached storage.
CONCLUSION
The primary advantage of the heap sort is its
efficiency. The execution time efficiency of the
heap sort is O(n log n). The memory efficiency
of the heap sort, unlike the other n log n
sorts, is constant, O(1), because the heap sort
algorithm is not recursive.
The heap sort algorithm has two major steps.
The first major step involves transforming
the complete tree into a heap. The second
major step is to perform the actual sort by
extracting the largest element from the root
and transforming the remaining tree into a
heap.