0% found this document useful (0 votes)
696 views48 pages

Heap Sort PDF

The document discusses heap sort, a sorting algorithm that uses a heap data structure. It provides details on max heaps and min heaps, including their properties and representations as arrays. The key steps of heap sort are building a max or min heap from an input array, then repeatedly removing the maximum/minimum element and restoring the heap to sort the array.

Uploaded by

rehana M.Shafi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
696 views48 pages

Heap Sort PDF

The document discusses heap sort, a sorting algorithm that uses a heap data structure. It provides details on max heaps and min heaps, including their properties and representations as arrays. The key steps of heap sort are building a max or min heap from an input array, then repeatedly removing the maximum/minimum element and restoring the heap to sort the array.

Uploaded by

rehana M.Shafi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

HEAP SORT

Group members :
Mehak latif (2021-CS-502)
Zainab shahzadi (2021-CS-503)
Momina saeed (2021-CS-504)
Ariba riaz (2021-CS-507)
Amina bibi (2021-CS-536)
OUTLINES:
 To explore the implementation and performance of
heap sort algorithm
 Min heap

 Max heap

 Deletion of min heap

 Deletion of max heap

 Applications

 Conclusion
HEAP
A heap is a data structure that stores a
collection of objects (with keys), and has the
following properties:
 Complete Binary tree
 Heap Order

It is implemented as an array where each


node in the tree corresponds to an element
of the array.
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.

 In this case the maximum value is stored in the root


DEFINITION
 Max Heap
 Store data in ascending order
 Has property of
A[Parent(i)] ≥ A[i]
 Min Heap
 Store data in descending order
 Has property of
A[Parent(i)] ≤ A[i]
MAX HEAP EXAMPLE
19

12 16

1 4 7

19 12 16 1 4 7

Array A
MIN HEAP EXAMPLE

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

Restore the min-heap property if violated


 General strategy is percolate up (or bubble up): if the
parent of the element is larger than the element, then
interchange the parent and child.
Algorithm of max-heap for insertion
#include<iostream>
Using namespace std;
Class heap {
Public:
Int arr[100]
Int size;
Heap()
{
Arr[0]=-1;
Size=0;
}
Void insert (int val)
{size=size+1;
Int index =size;
Arr[index]=val;
While(index >1)
{
Int parent =index/2;
If (arr[parent]<arr[index])
{swap(arr[parent],arr[index]);
Index=parent;
}
Else{
Return;
}
}
Void print ()
{
For(int i=0;i<=size;i++)
{
Cout<<arr[i]<<“ “;
}
cout<<endl;
}};
Int main()
{
Heap h;
h.insert(50);
h.insert(55);
h.insert(53);
h.insert(52);
h.insert(54);
h.print();
Return 0;
}
19 19

12 16 12 16

4 7 1 4 7 17
1

Insert 17
19

12 17
swap

1 4 7 16

Percolate up to maintain the heap


property
DELETION

Deletion in heap always happen at the root node to


remove the maximum or minimum value

 Delete max
 Move the last number to the root
 Restore the max heap property by percolate down.

 Delete min
 Move the last number to the root
 Restore the min heap property by percolate down.
MAX HEAP DELETION ALGORITHM

 Remove the root node


 Move the last element of last level to root

 Compare the value of this child node with its parent

 If the value of parent is less than the child node ,


then swap them
 Repeat the step 3 & 4 until the heap property hold
Algorithm of max heap for deletion

Void deletefeomheap()
{
If (size==0)
{
Cout<<“nothing to delete”;
Return;
}
Arr[1]=arr[size];
Size--;
Int i=1;
While(i<size)
{
Int leftindex=2*I;
Int rightindex=2*i+1;
If(leftindex<size&&arr[i]<arr[leftindex])
{
Swap(arr[i],arr[leftindex]);
i=leftindex;
}
Else if (rightindex<size&&arr[i]<arr[rightindex])
}
Else {
Return ;
}
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.
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.
HEAP SORT
 The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data

 To sort the elements in the decreasing order, use a min heap


 To sort the elements in the increasing order, use a max heap

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
APPLICATIONS
 Heap sort a O(n log n) complexity algorithm.
 To efficiently find kth smallest or largest element in
an array.
 To implement priority queues.

Priority Queues
 A priority queue is similar to regular queue in which
each element additionally has a priority associated
with it.
 Priority queue can be of two types
 Max priority queue: an element with high priority
is served before an element with low priority.
 Min priority queue: an element with minimum
priority is served before an element with high
priority.
 An efficient way to implement priority queues is
to use heap data structures
Max priority queue is normally
implemented using Max heap.
Min priority queue is normally
implemented using Min heap.
 Heap implemented priority queues are also used
in graph algorithms such as Dijkstra’s and prim’s
algorithm.
Max Priority Queue Algorithm:
 Build a max heap

The highest element will move to the root.

 Copy the last element to root.

 Decrement the current size by 1.

 Make a call to maxheapify with first index to


reconfirm the heap property for bottom
Maxheapify (A , n , i)
{
int largest = i ;
int i = 2 * i ;
int r = ( 2 * i ) + 1;
If ( i <= n && A[ i ] >A[ largest ] )
{
largest = i ;
}
If ( r <= n && A[ r ] >A[ largest ] )
{
largest = r;
}
If ( largest != i )
{
swap(A[ largest ] , A[ i ]);
Heapify ( A , n , largest );
}
Heapsort( A, n )
{
For ( i = n/2 ; i >= 1 ; i - - )
{
Heapify ( A , n , i )
}
For ( i = n ; i >= 1 ; i - - )
{
swap(A[ 1 ] , A[ i ]);
Heapify ( A , n-1 , 1 );
}
Example: Convert the following array to a heap

16 4 7 1 12 19

Picture the array as a complete binary tree:

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
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.
THANK YOU

You might also like