KEMBAR78
Heap Sort in Design and Analysis of algorithms | PPTX
DESIGN AND ANALYSIS OF
ALGORITHM
GC. Women University, FSD
Topic: Heap Sort
Submitted To: Ms. Sadia Sahar
Submitted by:Samaira Akram (15-175)
ANALYSIS OF
ALGORITHMS
Heap Sort Algorithm
HEAP SORT
• Sorting technique based upon a “Binary Heap Data
Structure”.
• Also called “In-place algorithm”.
• One of the efficient sorting methods.
• No quadratic worst-case running time.
• Involves building a heap data structure from given array.
• Similar to Selection sort.
• Finds minimum/maximum element and place in correct place.
WHAT IS A HEAP?
• An untidy collection of objects placed haphazardly on top of
each other. (General meaning)
• Special binary tree-based data structure.
Satisfies the special heap properties:
1. Shape Property: Complete binary tree (all levels of tree
are fully filled).
2. Heap Property: Nodes are either greater than or equal to
or less than or equal to each of it’s children.
BINARY TREE
• Each node having at most 2 nodes/leaves/children.
• Children can be left child and right child.
• Binary tree consists of:
• Root node
• Parent node(s)
• Child node(s)
• Depth/Level of node: Number of edges from root to node.
• Height of a node: Number of edges from the node to deepest
leaf.
• Height of tree: Height of the root.
PICTORIAL REPRESENTATION
1
2
4 5
3
6
Root Node
Parent Nodes
Children
Height of node
(2)=1
Height of tree =2
Level/Depth of
node (6) =2
TYPES OF BINARY TREE
• Full Binary Tree
• Each node has exactly zero or 2 children.
• Complete Binary Tree
• All leaves are on same level & all internal nodes have degree 2.
• In-complete Binary Tree
• Either leaf is missing ( not having exactly degree 2).
TYPES OF BINARY TREE (PIC.)
1
2
4 5
3
1
2
4 5
3
6 7
1
2
4 5
3
6
Full Binary Tree Complete Binary Tree
In-complete Binary Tree
Missing.. 
1
2 3
ARRAY REPRESENTATION OF
HEAPS
• Heap can be stored as an array A.
 Root of tree is A [1]
 Left Child of A [i] = A[2i]
 Right child of A[i] = A[2i+1]
 Parent of A[i] = A [i/2]
 Heap-size [A] ≤ length [A]
• The elements in the subarray are leaves.
16 14 10 8 7 9
1 2 3 4 5
6
4 5 6
TYPES OF HEAP
1. Max-Heap (Largest element at root) Common!
 Parent nodes are greater than their child nodes (usually used to
sort a list in descending order).
2. Min-Heap (Smallest element at root)
 Parent nodes are smaller than their child nodes (usually used to
sort a list in ascending order). Sorted Array!
MIN-HEAP VS. MAX-HEAP
1
3 4
67 17
1
2 3
4 5
6
8
911
8 9
7
20
17 14
811 9
1
2 3
4 5
6
7
16
8 9
7
Min-Heap
As first element is smallest, so to sort a list
in ascending order, we create a Min-heap
from that list & picks the first element and
repeat the process with remaining elements.
Max-Heap
As first element is largest, so used to sort a
list in descending order.
ADDING/DELETING NODES
• New nodes are always inserted at the bottom level (left to right).
• Nodes are removed from the bottom level (right to left).
1
3 4
67 17
1
2 3
4 5
6
8
911
8 9
7
What type
of heap is
it?
ALGORITHM OF HEAP SORT
HeapSort(A)
BuildMaxHeap(A)
for i = length[A] downto 2
do swap A[1] with A[i]
heap-size[A] = heap-size[A] – 1
MaxHeapify(A, 1)
end func
BuildMaxHeap(A)
heap-size[A] = length[A]
for i = |length[A]/2| downto 1
do MaxHeapify(A, i)
end func
ALGORITHM OF HEAP SORT
(CONT.)
MaxHeapify(A, i)
l = left(i)
r = right(i)
if l <= heap-size[A] and A[l] > A[i]
then largest = l
else largest = i
if r <= heap-size[A] and A[r] > A[largest]
then largest = r
if largest != i
then swap A[i] with A[largest]
MaxHeapify(A, largest)
end func
HOW HEAP SORT WORKS?
• Heap sort algorithm inserts all elements (from an unsorted
array) into a heap then swap the first element (maximum) with
the last element (minimum) and reduce the heap size (array
size) by 1, because last element is already at its final location in
the sorted array. Then we heapify the first element because
swapping has broken the Max-heap property. We continue this
process until heap size remains one.
• Hereafter swapping, it may not satisfy the heap property. So we
need to adjust the location of element to maintain heap
property.
This process is called “Heapify”.
CONVERTING ARRAY TO MAX-
HEAP
ANALYSIS OF HEAPIFY
Applying recurrence analysis:
T(n)= a*T(n/b)+f(n)
T(n)= T(2n/3)+1
Applying Master Method:
a=1
b=3/2
f(n)=1
nlog
b
a = nlog
3/2
1=n0=1 :: Case 2 applies since f(n)= nlog
b
a
T(n)=O( nlog
b
a logn)
T(n)= O( logn)
ANALYSIS OF HEAP SORT
Times
HeapSort(A)
BuildMaxHeap(A) 1(n)
for i = length[A] downto 2 n
do swap A[1] with A[i] n-1
heap-size[A] = heap-size[A] – 1 n-1
MaxHeapify(A, 1) n(logn)
end func
ANALYSIS OF HEAP SORT
(CONT.)
Since in Heap Sort we first call Build-Max-Heap function
which takes O(n) time then we call Max-Heapify function n
time, where Max-Heapify function takes O(logn) time so, total
time complexity of heap-sort comes out to be O(n* logn).
Summarizing all this:
Time Complexity of Heap Sort:
Worst Case : O (n* logn)
Average Case : O(n* logn)
Best Case : O(n* logn)
COMPARISON
Heap Sort Quick Sort Merge Sort
Selection method. Partitioning method . Merging method.
It is not stable sort. It is not stable sort. Merge is stable sort.
Doesn’t need
supplementary
memory to work.
Need supplementary
memory to work.
Need supplementary
memory to work.
Example of “In place”
sorting algorithm.
Example of recursive
divide and conquer.
Example of recursive
divide and conquer.
Not copy the array
and store it
elsewhere.
Copy the array and
store it elsewhere.
Copy the array and
store it elsewhere.
Heap Sort Quick Sort Merge Sort
Needs less space. Needs more space. Needs more space.
Example:-
Used in situations
like what's the top 10
numbers from a list
of 1 million numbers.
Example:-
Java uses a version
of quick sort called
as double pivoted
quick sort to sort
primitives.
Example:-
Merge sort works
best on linked lists.
COMPARISON
ADVANTAGES
PROS. OF HEAP SORT
Efficiency: The Heap sort algorithm is very efficient.
Memory usage: The Heap sort algorithm can be implemented as an
in-place sorting algorithm.
Simplicity: The Heap sort algorithm is simpler to understand than
other equally efficient sorting algorithms.
Consistency: The Heap sort algorithm exhibits consistent
performance.
DISADVANTAGES
CONS. OF HEAP SORT
More Storage is required: Heap sort requires more space for sorting.
Not efficient in some cases: Quick sort is much more efficient than
Heap in many cases.
Time consuming: Heap sort make a tree of sorting elements.
Slower: Generally slower than quick and merge sorts.
THE END!


Heap Sort in Design and Analysis of algorithms

  • 1.
    DESIGN AND ANALYSISOF ALGORITHM GC. Women University, FSD Topic: Heap Sort Submitted To: Ms. Sadia Sahar Submitted by:Samaira Akram (15-175)
  • 2.
  • 3.
    HEAP SORT • Sortingtechnique based upon a “Binary Heap Data Structure”. • Also called “In-place algorithm”. • One of the efficient sorting methods. • No quadratic worst-case running time. • Involves building a heap data structure from given array. • Similar to Selection sort. • Finds minimum/maximum element and place in correct place.
  • 4.
    WHAT IS AHEAP? • An untidy collection of objects placed haphazardly on top of each other. (General meaning) • Special binary tree-based data structure. Satisfies the special heap properties: 1. Shape Property: Complete binary tree (all levels of tree are fully filled). 2. Heap Property: Nodes are either greater than or equal to or less than or equal to each of it’s children.
  • 5.
    BINARY TREE • Eachnode having at most 2 nodes/leaves/children. • Children can be left child and right child. • Binary tree consists of: • Root node • Parent node(s) • Child node(s) • Depth/Level of node: Number of edges from root to node. • Height of a node: Number of edges from the node to deepest leaf. • Height of tree: Height of the root.
  • 6.
    PICTORIAL REPRESENTATION 1 2 4 5 3 6 RootNode Parent Nodes Children Height of node (2)=1 Height of tree =2 Level/Depth of node (6) =2
  • 7.
    TYPES OF BINARYTREE • Full Binary Tree • Each node has exactly zero or 2 children. • Complete Binary Tree • All leaves are on same level & all internal nodes have degree 2. • In-complete Binary Tree • Either leaf is missing ( not having exactly degree 2).
  • 8.
    TYPES OF BINARYTREE (PIC.) 1 2 4 5 3 1 2 4 5 3 6 7 1 2 4 5 3 6 Full Binary Tree Complete Binary Tree In-complete Binary Tree Missing.. 
  • 9.
    1 2 3 ARRAY REPRESENTATIONOF HEAPS • Heap can be stored as an array A.  Root of tree is A [1]  Left Child of A [i] = A[2i]  Right child of A[i] = A[2i+1]  Parent of A[i] = A [i/2]  Heap-size [A] ≤ length [A] • The elements in the subarray are leaves. 16 14 10 8 7 9 1 2 3 4 5 6 4 5 6
  • 10.
    TYPES OF HEAP 1.Max-Heap (Largest element at root) Common!  Parent nodes are greater than their child nodes (usually used to sort a list in descending order). 2. Min-Heap (Smallest element at root)  Parent nodes are smaller than their child nodes (usually used to sort a list in ascending order). Sorted Array!
  • 11.
    MIN-HEAP VS. MAX-HEAP 1 34 67 17 1 2 3 4 5 6 8 911 8 9 7 20 17 14 811 9 1 2 3 4 5 6 7 16 8 9 7 Min-Heap As first element is smallest, so to sort a list in ascending order, we create a Min-heap from that list & picks the first element and repeat the process with remaining elements. Max-Heap As first element is largest, so used to sort a list in descending order.
  • 12.
    ADDING/DELETING NODES • Newnodes are always inserted at the bottom level (left to right). • Nodes are removed from the bottom level (right to left). 1 3 4 67 17 1 2 3 4 5 6 8 911 8 9 7 What type of heap is it?
  • 13.
    ALGORITHM OF HEAPSORT HeapSort(A) BuildMaxHeap(A) for i = length[A] downto 2 do swap A[1] with A[i] heap-size[A] = heap-size[A] – 1 MaxHeapify(A, 1) end func BuildMaxHeap(A) heap-size[A] = length[A] for i = |length[A]/2| downto 1 do MaxHeapify(A, i) end func
  • 14.
    ALGORITHM OF HEAPSORT (CONT.) MaxHeapify(A, i) l = left(i) r = right(i) if l <= heap-size[A] and A[l] > A[i] then largest = l else largest = i if r <= heap-size[A] and A[r] > A[largest] then largest = r if largest != i then swap A[i] with A[largest] MaxHeapify(A, largest) end func
  • 15.
    HOW HEAP SORTWORKS? • Heap sort algorithm inserts all elements (from an unsorted array) into a heap then swap the first element (maximum) with the last element (minimum) and reduce the heap size (array size) by 1, because last element is already at its final location in the sorted array. Then we heapify the first element because swapping has broken the Max-heap property. We continue this process until heap size remains one. • Hereafter swapping, it may not satisfy the heap property. So we need to adjust the location of element to maintain heap property. This process is called “Heapify”.
  • 16.
  • 17.
    ANALYSIS OF HEAPIFY Applyingrecurrence analysis: T(n)= a*T(n/b)+f(n) T(n)= T(2n/3)+1 Applying Master Method: a=1 b=3/2 f(n)=1 nlog b a = nlog 3/2 1=n0=1 :: Case 2 applies since f(n)= nlog b a T(n)=O( nlog b a logn) T(n)= O( logn)
  • 18.
    ANALYSIS OF HEAPSORT Times HeapSort(A) BuildMaxHeap(A) 1(n) for i = length[A] downto 2 n do swap A[1] with A[i] n-1 heap-size[A] = heap-size[A] – 1 n-1 MaxHeapify(A, 1) n(logn) end func
  • 19.
    ANALYSIS OF HEAPSORT (CONT.) Since in Heap Sort we first call Build-Max-Heap function which takes O(n) time then we call Max-Heapify function n time, where Max-Heapify function takes O(logn) time so, total time complexity of heap-sort comes out to be O(n* logn). Summarizing all this: Time Complexity of Heap Sort: Worst Case : O (n* logn) Average Case : O(n* logn) Best Case : O(n* logn)
  • 20.
    COMPARISON Heap Sort QuickSort Merge Sort Selection method. Partitioning method . Merging method. It is not stable sort. It is not stable sort. Merge is stable sort. Doesn’t need supplementary memory to work. Need supplementary memory to work. Need supplementary memory to work. Example of “In place” sorting algorithm. Example of recursive divide and conquer. Example of recursive divide and conquer. Not copy the array and store it elsewhere. Copy the array and store it elsewhere. Copy the array and store it elsewhere.
  • 21.
    Heap Sort QuickSort Merge Sort Needs less space. Needs more space. Needs more space. Example:- Used in situations like what's the top 10 numbers from a list of 1 million numbers. Example:- Java uses a version of quick sort called as double pivoted quick sort to sort primitives. Example:- Merge sort works best on linked lists. COMPARISON
  • 22.
    ADVANTAGES PROS. OF HEAPSORT Efficiency: The Heap sort algorithm is very efficient. Memory usage: The Heap sort algorithm can be implemented as an in-place sorting algorithm. Simplicity: The Heap sort algorithm is simpler to understand than other equally efficient sorting algorithms. Consistency: The Heap sort algorithm exhibits consistent performance.
  • 23.
    DISADVANTAGES CONS. OF HEAPSORT More Storage is required: Heap sort requires more space for sorting. Not efficient in some cases: Quick sort is much more efficient than Heap in many cases. Time consuming: Heap sort make a tree of sorting elements. Slower: Generally slower than quick and merge sorts.
  • 24.