0% found this document useful (0 votes)
39 views31 pages

Heap Sort: Presented by

Heap sort is a sorting algorithm that uses a special type of binary tree called a heap to sort data. It works by organizing data into a max heap or min heap and then extracting elements from the heap in sorted order. Heap sort has a best, average, and worst case time complexity of O(n log n) and is an efficient sorting algorithm due to its logarithmic time complexity. However, it is not a stable sort and requires additional memory for the heap data structure.

Uploaded by

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

Heap Sort: Presented by

Heap sort is a sorting algorithm that uses a special type of binary tree called a heap to sort data. It works by organizing data into a max heap or min heap and then extracting elements from the heap in sorted order. Heap sort has a best, average, and worst case time complexity of O(n log n) and is an efficient sorting algorithm due to its logarithmic time complexity. However, it is not a stable sort and requires additional memory for the heap data structure.

Uploaded by

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

HEAP SORT

Presented By:
P Karteek (210101120061)
Khagendra Samal (210101120063)
Dillip Behera (210101120071)
Harisankar Patra (210101120072)
Karan Bagarti (210101120105)
Debasish Gouda (21010112016)
M Mamata (210101120079)
K Sruti Rani (210101120080
Rashmita Kumari Pradhan (210101120110)
Ankita Pradhan (210101120109)
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
 A sorting algorithm that works by first organizing the
data to be sorted into a special type of binary tree called
a heap.

19

12 16

1 4 7

19 12 16 1 4 7

Array A
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
 Storedata in ascending order
 Has property of
A[Parent(i)] ≥ A[i]
 Min Heap
 Storedata 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
1

4 16

7 12 19

1 4 16 7 12 19

Array A
INSERTION
 Algorithm
1)Arrange the nodes in the binary tree form .
2)Node should be arranged as per the specific rules .
3)If the inserted node is bigger than its parent node then replace the
node .
4)If the inserted node is lesser than its parent node then do not change
the position.
5)Do not allow entering nodes on right side until the child nodes of the
left are fulfilled.
6)Do not allow entering nodes on left side until the child nodes of the
right are fulfilled.
7)The procedure from step 3 to step 6 is repeated for each of the node .
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
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.
 
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
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
TIME ANALYSIS
• Best Case Complexity - It occurs when there is no sorting required,
i.e. the array is already sorted. The best-case time complexity of
heap sort is O(n logn).
• Average Case Complexity - It occurs when the array elements are
in jumbled order that is not properly ascending and not properly
descending. The average case time complexity of heap sort is O(n
log n).
• Worst Case Complexity - It occurs when the array elements are
required to be sorted in reverse order. That means suppose you have
to sort the array elements in ascending order, but its elements are in
descending order. The worst-case time complexity of heap sort
is O(n log n).
ADVANTAGES :
• Efficiency – The time required to perform Heap sort increases
logarithmically while other algorithms may grow exponentially
slower as the number of items to sort increases. This sorting
algorithm is very efficient.

• Memory Usage – Memory usage is minimal because apart


from what is necessary to hold the initial list of items to be
sorted, it needs no additional memory space to work.

• Simplicity – It is simpler to understand than other equally


efficient sorting algorithms because it does not use advanced
computer science concepts such as recursion.
DISADVANTAGES
• Heap sort is not a stable sort and requires constant space for
sorting.

• Memory management is quite complex due to heap data


structure.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy