PRESENTED BY :
BHUVNESH
YADAV
18CSE17
PRESENTED TO:
PRASHANT
MAHARISHI SIR
Heap sort is a comparison based sorting technique
based on Binary Heap data structure. It is similar
to selection sort where we first find the maximum
element and place the maximum element at the
end. We repeat the same process for remaining
element.
Binary Tree is the simplest form of tree data structure
where each node can have max of two children, known as
left and right. The node of a Binary Tree is formed with two
pointers to point child nodes named as left and right.
 A complete binary tree is similar to a full binary tree, but with
two major differences
 Complete binary tree should be completely filled to the height
level h-1 but may not be completely filled at highest level (h).
 New nodes must me added starting from left side and thus all
the leaf elements must lean towards the left.
 The last leaf element might not have a right sibling i.e. a
complete binary tree doesn't have to be a full binary tree.
Heap is a special binary tree based data structure. A binary tree is
said to follow a heap data structure if it is a complete binary tree.
There are two types of heap structure. Max heap and min heap.
Max heap/Descending heap
Max heap is a heap structure where parent element is always
Larger than child elements. This ordering rule is maintained in the
entire tree. Thus all the parent elements are larger than children
elements in any level of the tree and the root contains the largest
value.
Min heap/Ascending heap
Min heap is a heap structure where parent
element is lower than child elements. Thus root
of the min heap contains the lowest value and
the further we go to the depth, values are
larger.
There is a relation between binary tree and array. A Binary Heap is a
complete Binary Tree and it can be represented as an array. Each
element index in array can be mapped to a node in binary tree. In the
reverse case also it is true. A tree node can be mapped to an index
in the array. Array indexing starts with 0 and root in binary tree is the
starting element. The root can be placed at index zero. Since we
populate child elements from left side so first left should be at index
1 and right should be at index 2. In general if the parent node is
stored at index i, the left child can be calculated by (2 * i + 1) and
right child by (2 * i + 2).
Binary Heap and array elements mapping
In order to maintain the max-heap property, we call the procedure MAX-
HEAPIFY. Its inputs are an array A and an index i into the array. When it
is called, MAXHEAPIFY assumes that the binary trees rooted at
LEFT(i) / and RIGHT(i )/ are max heaps, but that A[i] might be smaller
than its children, thus violating the max-heap property. MAX-HEAPIFY
lets the value at A[i] “float down” in the max-heap so that the subtree
rooted at index i obeys the max-heap property.
MAX-HEAPIFY(A, i )
1. l = LEFT(i) /
2. r = RIGHT(i) /
3. if l<= A:heap-size and A[l] > A[i]
4. largest = l
5. else largest = i
6. if r<= A:heap-size and A[r]>A[largest]
7. largest = r
8. if largest =! i
9. exchange A[i] with A[largest]
10. MAX-HEAPIFY.(A, largest)
Example of heap sort
HEAPSORT(A)
1. BUILD-MAX-HEAP(A)
2. for i = A. length down to 2
3 exchange A[1] with A[i]
4 A. heap-size = A. heap-size -1
5 MAX-HEAPIFY(A,1)
Example: For sorting an array element using above algorithm
Creation : O(n*logn)
Sorting : O(n*logn)
Space complexity : O(1)
We use heap sort in priority queue
 heapsort

heapsort

  • 1.
  • 2.
    Heap sort isa comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element.
  • 3.
    Binary Tree isthe simplest form of tree data structure where each node can have max of two children, known as left and right. The node of a Binary Tree is formed with two pointers to point child nodes named as left and right.
  • 4.
     A completebinary tree is similar to a full binary tree, but with two major differences  Complete binary tree should be completely filled to the height level h-1 but may not be completely filled at highest level (h).  New nodes must me added starting from left side and thus all the leaf elements must lean towards the left.  The last leaf element might not have a right sibling i.e. a complete binary tree doesn't have to be a full binary tree.
  • 5.
    Heap is aspecial binary tree based data structure. A binary tree is said to follow a heap data structure if it is a complete binary tree. There are two types of heap structure. Max heap and min heap. Max heap/Descending heap Max heap is a heap structure where parent element is always Larger than child elements. This ordering rule is maintained in the entire tree. Thus all the parent elements are larger than children elements in any level of the tree and the root contains the largest value.
  • 6.
    Min heap/Ascending heap Minheap is a heap structure where parent element is lower than child elements. Thus root of the min heap contains the lowest value and the further we go to the depth, values are larger.
  • 7.
    There is arelation between binary tree and array. A Binary Heap is a complete Binary Tree and it can be represented as an array. Each element index in array can be mapped to a node in binary tree. In the reverse case also it is true. A tree node can be mapped to an index in the array. Array indexing starts with 0 and root in binary tree is the starting element. The root can be placed at index zero. Since we populate child elements from left side so first left should be at index 1 and right should be at index 2. In general if the parent node is stored at index i, the left child can be calculated by (2 * i + 1) and right child by (2 * i + 2). Binary Heap and array elements mapping
  • 10.
    In order tomaintain the max-heap property, we call the procedure MAX- HEAPIFY. Its inputs are an array A and an index i into the array. When it is called, MAXHEAPIFY assumes that the binary trees rooted at LEFT(i) / and RIGHT(i )/ are max heaps, but that A[i] might be smaller than its children, thus violating the max-heap property. MAX-HEAPIFY lets the value at A[i] “float down” in the max-heap so that the subtree rooted at index i obeys the max-heap property. MAX-HEAPIFY(A, i ) 1. l = LEFT(i) / 2. r = RIGHT(i) / 3. if l<= A:heap-size and A[l] > A[i] 4. largest = l 5. else largest = i 6. if r<= A:heap-size and A[r]>A[largest] 7. largest = r 8. if largest =! i 9. exchange A[i] with A[largest] 10. MAX-HEAPIFY.(A, largest)
  • 11.
  • 12.
    HEAPSORT(A) 1. BUILD-MAX-HEAP(A) 2. fori = A. length down to 2 3 exchange A[1] with A[i] 4 A. heap-size = A. heap-size -1 5 MAX-HEAPIFY(A,1) Example: For sorting an array element using above algorithm
  • 13.
    Creation : O(n*logn) Sorting: O(n*logn) Space complexity : O(1) We use heap sort in priority queue