SlideShare ist ein Scribd-Unternehmen logo
1 von 56
Downloaden Sie, um offline zu lesen
HEAP SORT
Sunawar Khan
International Islamic University
Islamabad
January 04, 2016
INTRODUCTION
• What is Full Binary Tree
◦ Binary tree in which each node is either a leaf or has degree exactly 2.
2 of 26
INTRODUCTION
• What is Full Binary Tree
◦ Binary tree in which each node is either a leaf or has degree exactly 2.
• What is Complete binary tree
◦ Binary tree in which all leaves are on the same level and all internal
nodes have degree 2
2 of 26
INTRODUCTION
• What is Full Binary Tree
◦ Binary tree in which each node is either a leaf or has degree exactly 2.
• What is Complete binary tree
◦ Binary tree in which all leaves are on the same level and all internal
nodes have degree 2
• What is Heap Sort
◦ Combines the better attributes of merge sort and insertion sort.
• Like merge sort, but unlike insertion sort, running time is O(nlgn).
• Like insertion sort, but unlike merge sort, sorts in place.
2 of 26
INTRODUCTION
• What is Full Binary Tree
◦ Binary tree in which each node is either a leaf or has degree exactly 2.
• What is Complete binary tree
◦ Binary tree in which all leaves are on the same level and all internal
nodes have degree 2
• What is Heap Sort
◦ Combines the better attributes of merge sort and insertion sort.
• Like merge sort, but unlike insertion sort, running time is O(nlgn).
• Like insertion sort, but unlike merge sort, sorts in place.
◦ Introduces an algorithm design technique
• Create data structure (heap) to manage information during the
execution of an algorithm.
2 of 26
INTRODUCTION
• What is Full Binary Tree
◦ Binary tree in which each node is either a leaf or has degree exactly 2.
• What is Complete binary tree
◦ Binary tree in which all leaves are on the same level and all internal
nodes have degree 2
• What is Heap Sort
◦ Combines the better attributes of merge sort and insertion sort.
• Like merge sort, but unlike insertion sort, running time is O(nlgn).
• Like insertion sort, but unlike merge sort, sorts in place.
◦ Introduces an algorithm design technique
• Create data structure (heap) to manage information during the
execution of an algorithm.
◦ The heap has other applications beside sorting.
• Priority Queues
2 of 26
HEAP-Example
3 of 26
What is Heap-Properties
• Properties of Heap is
◦ Completeness Property
◦ Order Proprty
4 of 26
What is Heap-Properties
• Properties of Heap is
◦ Completeness Property
◦ Order Proprty
• Types of Heap is
◦ Max Heap
A[PARENT(i)] A[i]
◦ Min Heap
A[PARENT(i)] ≤ A[i]
4 of 26
What is Heap-Properties
• Properties of Heap is
◦ Completeness Property
◦ Order Proprty
• Types of Heap is
◦ Max Heap
A[PARENT(i)] A[i]
◦ Min Heap
A[PARENT(i)] ≤ A[i]
4 of 26
HEAP Properties Example
5 of 26
What is Heap-Characteristic
• Height of a node = the number of edges on the longest simple
path from the node down to a leaf lgn .
6 of 26
What is Heap-Characteristic
• Height of a node = the number of edges on the longest simple
path from the node down to a leaf lgn .
• Level of a node = the length of a path from the root to the node.
n/2 .
6 of 26
What is Heap-Characteristic
• Height of a node = the number of edges on the longest simple
path from the node down to a leaf lgn .
• Level of a node = the length of a path from the root to the node.
n/2 .
• Height of tree = height of root node. height h ≤ n/2h + 1
6 of 26
Heap Representations
• Heap can Be Represented As
◦ Root of tree is A[1]
7 of 26
Heap Representations
• Heap can Be Represented As
◦ Root of tree is A[1]
◦ Left
A[i] = A[2i]
7 of 26
Heap Representations
• Heap can Be Represented As
◦ Root of tree is A[1]
◦ Left
A[i] = A[2i]
◦ Right
A[i] = A[2i + 1]
7 of 26
Heap Representations
• Heap can Be Represented As
◦ Root of tree is A[1]
◦ Left
A[i] = A[2i]
◦ Right
A[i] = A[2i + 1]
◦ Parent
A[i] = A[ i/2 ]
7 of 26
Heap Representations
• Heap can Be Represented As
◦ Root of tree is A[1]
◦ Left
A[i] = A[2i]
◦ Right
A[i] = A[2i + 1]
◦ Parent
A[i] = A[ i/2 ]
◦ Heapsize[A] ≤ Length[A]
7 of 26
Problems
• What are the minimum and maximum numbers of elements in a
heap of height h?
• Show that an n-element heap has height lgn .
• Where in a max-heap might the smallest element reside, assuming
that all elements are distinct?
• Is an array that is in sorted order a min-heap?
• Is the array with values 23, 17, 14, 6, 13, 10, 1, 5, 7, 12 a
max-heap?
8 of 26
Maintaining The Heap Property
MAXIMUM-HEAPIFY PROCEDURE
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] ↔ A[largest]
10. MAX-HEAPIFY(A, largest)
9 of 26
Maintain Heap Example
10 of 26
Running Time of MAX-HEAPIFY
• Running time of MAX-HEAPIFY is O(lgn)
11 of 26
Running Time of MAX-HEAPIFY
• Running time of MAX-HEAPIFY is O(lgn)
• Can be written in terms of the height of the heap, as being O(h)
Since the height of the heap is lgn
11 of 26
Running Time of MAX-HEAPIFY
• Running time of MAX-HEAPIFY is O(lgn)
• Can be written in terms of the height of the heap, as being O(h)
Since the height of the heap is lgn
• MaxHeapify takes O(h) where h is the height of the node where
MaxHeapify is applied.
T(n) = O(lgn)
11 of 26
Problems
• What is the effect of calling MAX-HEAPIFY(A, i) when the
element A[i] is larger than its children?
• What is the effect of calling MAX-HEAPIFY(A, i) for
i>A.heap-size/2?
• Show that the worst-case running time of MAX-HEAPIFY on a
heap of size n is Ω(lgn).
12 of 26
Building a heap
Building Max Heap PROCEDURE
BuildMaxHeap(A)
1. heap-size[A] ← length[A]
2. for i ← length[A]/2 downto 1
3. do MaxHeapify(A, i)
13 of 26
Build Heap-Example
14 of 26
Running Time of Build-Heap
• Running time: O(nlgn)
This is not an asymptotically tight upper bound
15 of 26
HEAP SORT
• Goal:
◦ Sort an array using heap representations
16 of 26
HEAP SORT
• Goal:
◦ Sort an array using heap representations
• Idea:
◦ Build a max-heap from the array
◦ Swap the root (the maximum element) with the last element in the
array
◦ Discard this last node by decreasing the heap size
◦ Call MAX-HEAPIFY on the new root
◦ Repeat this process until only one node remains
16 of 26
HEAP SORT PROCEDURE
• Sort by maintaining the as yet unsorted elements as a max-heap.
17 of 26
HEAP SORT PROCEDURE
• Sort by maintaining the as yet unsorted elements as a max-heap.
• Start by building a max-heap on all elements in A.
17 of 26
HEAP SORT PROCEDURE
• Sort by maintaining the as yet unsorted elements as a max-heap.
• Start by building a max-heap on all elements in A.
Maximum element is in the root, A[1].
17 of 26
HEAP SORT PROCEDURE
• Sort by maintaining the as yet unsorted elements as a max-heap.
• Start by building a max-heap on all elements in A.
Maximum element is in the root, A[1].
• Move the maximum element to its correct final position.
Exchange A[1] with A[n].
17 of 26
HEAP SORT PROCEDURE
• Sort by maintaining the as yet unsorted elements as a max-heap.
• Start by building a max-heap on all elements in A.
Maximum element is in the root, A[1].
• Move the maximum element to its correct final position.
Exchange A[1] with A[n].
• Discard A[n] it is now sorted.
Decrement heap-size[A].
17 of 26
HEAP SORT PROCEDURE
• Sort by maintaining the as yet unsorted elements as a max-heap.
• Start by building a max-heap on all elements in A.
Maximum element is in the root, A[1].
• Move the maximum element to its correct final position.
Exchange A[1] with A[n].
• Discard A[n] it is now sorted.
Decrement heap-size[A].
• Restore the max-heap property on A[1..n1].
Call MaxHeapify(A, 1).
17 of 26
HEAP SORT PROCEDURE
• Sort by maintaining the as yet unsorted elements as a max-heap.
• Start by building a max-heap on all elements in A.
Maximum element is in the root, A[1].
• Move the maximum element to its correct final position.
Exchange A[1] with A[n].
• Discard A[n] it is now sorted.
Decrement heap-size[A].
• Restore the max-heap property on A[1..n1].
Call MaxHeapify(A, 1).
• Repeat until heap-size[A] is reduced to 2.
17 of 26
HEAP SORT
// Input: A: an (unsorted) array
// Output: A modifed to be sorted from smallest to largest
// Running Time: O(nlogn) where n = length[A]
HeapSort(A)
1. Build-Max-Heap(A)
2. for i ← length[A] downto 2
3. do exchange A[1] ↔ A[i]
4. heap-size[A] ← heap-size[A] 1
5. MaxHeapify(A, 1)
18 of 26
Heap Sort Example
19 of 26
Running Time for Heap Sort
• Build-Max-Heap takes O(n) and each of the n-1 calls to
Max-Heapify takes time O(lgn).
T(n) = O(nlgn)
20 of 26
Running Time for Heap Sort
• Build-Max-Heap takes O(n) and each of the n-1 calls to
Max-Heapify takes time O(lgn).
T(n) = O(nlgn)
• The heap sorting algorithm uses two procedures : BUILD-HEAP
and HEAPIFY. Thus, total running time Tsort(n) to sort an array
of size n is
20 of 26
Problems
• What is the running time of HEAPSORT on an array A of length n
that is already sorted in increasing order? What about decreasing
order?
• Show that the worst-case running time of HEAPSORT is Ω(nlgn).
21 of 26
Complexity
• To insert a single node in an empty heap is : O(1).
• To insert a single element in a n node heap is : O(logn).
• To insert n elements in a n node heap is : O(nlogn).
• To delete the largest element in a max heap tree : O(1).
• To delete the smallest element in a max heap tree : O(logn).
• To delete n elements in a max heap tree : O(nlogn).
• To create a heap, time complexity will be : O(nlogn).
22 of 26
Complexity
Now to sort the heap tree requires
• Arrange or insert the elements in a form of heap i.e O(nlogn)
• Delete the elements one by one after swapping operation O(nlogn)
• This gives Heap sort complexity
O(nlogn) + O(nlogn)
2O(nlogn) = O(nlogn).
23 of 26
Heap Procedures for Sorting
• MaxHeapify O(lgn)
24 of 26
Heap Procedures for Sorting
• MaxHeapify O(lgn)
• BuildMaxHeap O(n)
24 of 26
Heap Procedures for Sorting
• MaxHeapify O(lgn)
• BuildMaxHeap O(n)
• HeapSort O(nlgn)
24 of 26
SUMMARY
• We can perform the following operations on heaps:
◦ MAX-HEAPIFY O(lgn)
25 of 26
SUMMARY
• We can perform the following operations on heaps:
◦ MAX-HEAPIFY O(lgn)
◦ BUILD-MAX-HEAP O(n)
25 of 26
SUMMARY
• We can perform the following operations on heaps:
◦ MAX-HEAPIFY O(lgn)
◦ BUILD-MAX-HEAP O(n)
◦ HEAP-SORT O(nlgn)
25 of 26
SUMMARY
• We can perform the following operations on heaps:
◦ MAX-HEAPIFY O(lgn)
◦ BUILD-MAX-HEAP O(n)
◦ HEAP-SORT O(nlgn)
◦ MAX-HEAP-INSERT O(lgn)
25 of 26
SUMMARY
• We can perform the following operations on heaps:
◦ MAX-HEAPIFY O(lgn)
◦ BUILD-MAX-HEAP O(n)
◦ HEAP-SORT O(nlgn)
◦ MAX-HEAP-INSERT O(lgn)
◦ HEAP-EXTRACT-MAX O(lgn)
25 of 26
SUMMARY
• We can perform the following operations on heaps:
◦ MAX-HEAPIFY O(lgn)
◦ BUILD-MAX-HEAP O(n)
◦ HEAP-SORT O(nlgn)
◦ MAX-HEAP-INSERT O(lgn)
◦ HEAP-EXTRACT-MAX O(lgn)
◦ HEAP-INCREASE-KEY O(lgn)
25 of 26
SUMMARY
• We can perform the following operations on heaps:
◦ MAX-HEAPIFY O(lgn)
◦ BUILD-MAX-HEAP O(n)
◦ HEAP-SORT O(nlgn)
◦ MAX-HEAP-INSERT O(lgn)
◦ HEAP-EXTRACT-MAX O(lgn)
◦ HEAP-INCREASE-KEY O(lgn)
◦ HEAP-MAXIMUM O(1)
25 of 26
Building a heap
• Presented To:- Dr. Arshad Aewan
• Presented By:- SK Ahsan
• Reg No:- 813/FBAS/MSCS/F14
• Topic:- Heap Sort(Procedure)
• My Dreams Are My Own Drems(Me)
26 of 26

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Heaps
HeapsHeaps
Heaps
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Heaps
HeapsHeaps
Heaps
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
Heap
HeapHeap
Heap
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Heaps
HeapsHeaps
Heaps
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Queue
QueueQueue
Queue
 
Expression trees
Expression treesExpression trees
Expression trees
 
Quick sort
Quick sortQuick sort
Quick sort
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Shell sorting
Shell sortingShell sorting
Shell sorting
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 

Ähnlich wie Heap sort

Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialAymericTaylor
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxIsrar63
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisRadhika Talaviya
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNratnapatil14
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptSudhaPatel11
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsssuser7319f8
 

Ähnlich wie Heap sort (20)

Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
 
Heapsort
HeapsortHeapsort
Heapsort
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
 
Sorting
SortingSorting
Sorting
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
sparse set.pdf
sparse set.pdfsparse set.pdf
sparse set.pdf
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Splay tree
Splay treeSplay tree
Splay tree
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
 
heaps2
heaps2heaps2
heaps2
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
 

Mehr von International Islamic University (20)

Hash tables
Hash tablesHash tables
Hash tables
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Graph 1
Graph 1Graph 1
Graph 1
 
Graph 2
Graph 2Graph 2
Graph 2
 
Graph 3
Graph 3Graph 3
Graph 3
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Quick sort
Quick sortQuick sort
Quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Linear timesorting
Linear timesortingLinear timesorting
Linear timesorting
 
Facial Expression Recognitino
Facial Expression RecognitinoFacial Expression Recognitino
Facial Expression Recognitino
 
Lecture#4
Lecture#4Lecture#4
Lecture#4
 
Lecture#3
Lecture#3 Lecture#3
Lecture#3
 
Lecture#2
Lecture#2 Lecture#2
Lecture#2
 
Case study
Case studyCase study
Case study
 
Arrays
ArraysArrays
Arrays
 
Pcb
PcbPcb
Pcb
 
Data transmission
Data transmissionData transmission
Data transmission
 
Basic organization of computer
Basic organization of computerBasic organization of computer
Basic organization of computer
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 

Kürzlich hochgeladen

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Kürzlich hochgeladen (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

Heap sort

  • 1. HEAP SORT Sunawar Khan International Islamic University Islamabad January 04, 2016
  • 2. INTRODUCTION • What is Full Binary Tree ◦ Binary tree in which each node is either a leaf or has degree exactly 2. 2 of 26
  • 3. INTRODUCTION • What is Full Binary Tree ◦ Binary tree in which each node is either a leaf or has degree exactly 2. • What is Complete binary tree ◦ Binary tree in which all leaves are on the same level and all internal nodes have degree 2 2 of 26
  • 4. INTRODUCTION • What is Full Binary Tree ◦ Binary tree in which each node is either a leaf or has degree exactly 2. • What is Complete binary tree ◦ Binary tree in which all leaves are on the same level and all internal nodes have degree 2 • What is Heap Sort ◦ Combines the better attributes of merge sort and insertion sort. • Like merge sort, but unlike insertion sort, running time is O(nlgn). • Like insertion sort, but unlike merge sort, sorts in place. 2 of 26
  • 5. INTRODUCTION • What is Full Binary Tree ◦ Binary tree in which each node is either a leaf or has degree exactly 2. • What is Complete binary tree ◦ Binary tree in which all leaves are on the same level and all internal nodes have degree 2 • What is Heap Sort ◦ Combines the better attributes of merge sort and insertion sort. • Like merge sort, but unlike insertion sort, running time is O(nlgn). • Like insertion sort, but unlike merge sort, sorts in place. ◦ Introduces an algorithm design technique • Create data structure (heap) to manage information during the execution of an algorithm. 2 of 26
  • 6. INTRODUCTION • What is Full Binary Tree ◦ Binary tree in which each node is either a leaf or has degree exactly 2. • What is Complete binary tree ◦ Binary tree in which all leaves are on the same level and all internal nodes have degree 2 • What is Heap Sort ◦ Combines the better attributes of merge sort and insertion sort. • Like merge sort, but unlike insertion sort, running time is O(nlgn). • Like insertion sort, but unlike merge sort, sorts in place. ◦ Introduces an algorithm design technique • Create data structure (heap) to manage information during the execution of an algorithm. ◦ The heap has other applications beside sorting. • Priority Queues 2 of 26
  • 8. What is Heap-Properties • Properties of Heap is ◦ Completeness Property ◦ Order Proprty 4 of 26
  • 9. What is Heap-Properties • Properties of Heap is ◦ Completeness Property ◦ Order Proprty • Types of Heap is ◦ Max Heap A[PARENT(i)] A[i] ◦ Min Heap A[PARENT(i)] ≤ A[i] 4 of 26
  • 10. What is Heap-Properties • Properties of Heap is ◦ Completeness Property ◦ Order Proprty • Types of Heap is ◦ Max Heap A[PARENT(i)] A[i] ◦ Min Heap A[PARENT(i)] ≤ A[i] 4 of 26
  • 12. What is Heap-Characteristic • Height of a node = the number of edges on the longest simple path from the node down to a leaf lgn . 6 of 26
  • 13. What is Heap-Characteristic • Height of a node = the number of edges on the longest simple path from the node down to a leaf lgn . • Level of a node = the length of a path from the root to the node. n/2 . 6 of 26
  • 14. What is Heap-Characteristic • Height of a node = the number of edges on the longest simple path from the node down to a leaf lgn . • Level of a node = the length of a path from the root to the node. n/2 . • Height of tree = height of root node. height h ≤ n/2h + 1 6 of 26
  • 15. Heap Representations • Heap can Be Represented As ◦ Root of tree is A[1] 7 of 26
  • 16. Heap Representations • Heap can Be Represented As ◦ Root of tree is A[1] ◦ Left A[i] = A[2i] 7 of 26
  • 17. Heap Representations • Heap can Be Represented As ◦ Root of tree is A[1] ◦ Left A[i] = A[2i] ◦ Right A[i] = A[2i + 1] 7 of 26
  • 18. Heap Representations • Heap can Be Represented As ◦ Root of tree is A[1] ◦ Left A[i] = A[2i] ◦ Right A[i] = A[2i + 1] ◦ Parent A[i] = A[ i/2 ] 7 of 26
  • 19. Heap Representations • Heap can Be Represented As ◦ Root of tree is A[1] ◦ Left A[i] = A[2i] ◦ Right A[i] = A[2i + 1] ◦ Parent A[i] = A[ i/2 ] ◦ Heapsize[A] ≤ Length[A] 7 of 26
  • 20. Problems • What are the minimum and maximum numbers of elements in a heap of height h? • Show that an n-element heap has height lgn . • Where in a max-heap might the smallest element reside, assuming that all elements are distinct? • Is an array that is in sorted order a min-heap? • Is the array with values 23, 17, 14, 6, 13, 10, 1, 5, 7, 12 a max-heap? 8 of 26
  • 21. Maintaining The Heap Property MAXIMUM-HEAPIFY PROCEDURE 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] ↔ A[largest] 10. MAX-HEAPIFY(A, largest) 9 of 26
  • 23. Running Time of MAX-HEAPIFY • Running time of MAX-HEAPIFY is O(lgn) 11 of 26
  • 24. Running Time of MAX-HEAPIFY • Running time of MAX-HEAPIFY is O(lgn) • Can be written in terms of the height of the heap, as being O(h) Since the height of the heap is lgn 11 of 26
  • 25. Running Time of MAX-HEAPIFY • Running time of MAX-HEAPIFY is O(lgn) • Can be written in terms of the height of the heap, as being O(h) Since the height of the heap is lgn • MaxHeapify takes O(h) where h is the height of the node where MaxHeapify is applied. T(n) = O(lgn) 11 of 26
  • 26. Problems • What is the effect of calling MAX-HEAPIFY(A, i) when the element A[i] is larger than its children? • What is the effect of calling MAX-HEAPIFY(A, i) for i>A.heap-size/2? • Show that the worst-case running time of MAX-HEAPIFY on a heap of size n is Ω(lgn). 12 of 26
  • 27. Building a heap Building Max Heap PROCEDURE BuildMaxHeap(A) 1. heap-size[A] ← length[A] 2. for i ← length[A]/2 downto 1 3. do MaxHeapify(A, i) 13 of 26
  • 29. Running Time of Build-Heap • Running time: O(nlgn) This is not an asymptotically tight upper bound 15 of 26
  • 30. HEAP SORT • Goal: ◦ Sort an array using heap representations 16 of 26
  • 31. HEAP SORT • Goal: ◦ Sort an array using heap representations • Idea: ◦ Build a max-heap from the array ◦ Swap the root (the maximum element) with the last element in the array ◦ Discard this last node by decreasing the heap size ◦ Call MAX-HEAPIFY on the new root ◦ Repeat this process until only one node remains 16 of 26
  • 32. HEAP SORT PROCEDURE • Sort by maintaining the as yet unsorted elements as a max-heap. 17 of 26
  • 33. HEAP SORT PROCEDURE • Sort by maintaining the as yet unsorted elements as a max-heap. • Start by building a max-heap on all elements in A. 17 of 26
  • 34. HEAP SORT PROCEDURE • Sort by maintaining the as yet unsorted elements as a max-heap. • Start by building a max-heap on all elements in A. Maximum element is in the root, A[1]. 17 of 26
  • 35. HEAP SORT PROCEDURE • Sort by maintaining the as yet unsorted elements as a max-heap. • Start by building a max-heap on all elements in A. Maximum element is in the root, A[1]. • Move the maximum element to its correct final position. Exchange A[1] with A[n]. 17 of 26
  • 36. HEAP SORT PROCEDURE • Sort by maintaining the as yet unsorted elements as a max-heap. • Start by building a max-heap on all elements in A. Maximum element is in the root, A[1]. • Move the maximum element to its correct final position. Exchange A[1] with A[n]. • Discard A[n] it is now sorted. Decrement heap-size[A]. 17 of 26
  • 37. HEAP SORT PROCEDURE • Sort by maintaining the as yet unsorted elements as a max-heap. • Start by building a max-heap on all elements in A. Maximum element is in the root, A[1]. • Move the maximum element to its correct final position. Exchange A[1] with A[n]. • Discard A[n] it is now sorted. Decrement heap-size[A]. • Restore the max-heap property on A[1..n1]. Call MaxHeapify(A, 1). 17 of 26
  • 38. HEAP SORT PROCEDURE • Sort by maintaining the as yet unsorted elements as a max-heap. • Start by building a max-heap on all elements in A. Maximum element is in the root, A[1]. • Move the maximum element to its correct final position. Exchange A[1] with A[n]. • Discard A[n] it is now sorted. Decrement heap-size[A]. • Restore the max-heap property on A[1..n1]. Call MaxHeapify(A, 1). • Repeat until heap-size[A] is reduced to 2. 17 of 26
  • 39. HEAP SORT // Input: A: an (unsorted) array // Output: A modifed to be sorted from smallest to largest // Running Time: O(nlogn) where n = length[A] HeapSort(A) 1. Build-Max-Heap(A) 2. for i ← length[A] downto 2 3. do exchange A[1] ↔ A[i] 4. heap-size[A] ← heap-size[A] 1 5. MaxHeapify(A, 1) 18 of 26
  • 41. Running Time for Heap Sort • Build-Max-Heap takes O(n) and each of the n-1 calls to Max-Heapify takes time O(lgn). T(n) = O(nlgn) 20 of 26
  • 42. Running Time for Heap Sort • Build-Max-Heap takes O(n) and each of the n-1 calls to Max-Heapify takes time O(lgn). T(n) = O(nlgn) • The heap sorting algorithm uses two procedures : BUILD-HEAP and HEAPIFY. Thus, total running time Tsort(n) to sort an array of size n is 20 of 26
  • 43. Problems • What is the running time of HEAPSORT on an array A of length n that is already sorted in increasing order? What about decreasing order? • Show that the worst-case running time of HEAPSORT is Ω(nlgn). 21 of 26
  • 44. Complexity • To insert a single node in an empty heap is : O(1). • To insert a single element in a n node heap is : O(logn). • To insert n elements in a n node heap is : O(nlogn). • To delete the largest element in a max heap tree : O(1). • To delete the smallest element in a max heap tree : O(logn). • To delete n elements in a max heap tree : O(nlogn). • To create a heap, time complexity will be : O(nlogn). 22 of 26
  • 45. Complexity Now to sort the heap tree requires • Arrange or insert the elements in a form of heap i.e O(nlogn) • Delete the elements one by one after swapping operation O(nlogn) • This gives Heap sort complexity O(nlogn) + O(nlogn) 2O(nlogn) = O(nlogn). 23 of 26
  • 46. Heap Procedures for Sorting • MaxHeapify O(lgn) 24 of 26
  • 47. Heap Procedures for Sorting • MaxHeapify O(lgn) • BuildMaxHeap O(n) 24 of 26
  • 48. Heap Procedures for Sorting • MaxHeapify O(lgn) • BuildMaxHeap O(n) • HeapSort O(nlgn) 24 of 26
  • 49. SUMMARY • We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) 25 of 26
  • 50. SUMMARY • We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) ◦ BUILD-MAX-HEAP O(n) 25 of 26
  • 51. SUMMARY • We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) ◦ BUILD-MAX-HEAP O(n) ◦ HEAP-SORT O(nlgn) 25 of 26
  • 52. SUMMARY • We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) ◦ BUILD-MAX-HEAP O(n) ◦ HEAP-SORT O(nlgn) ◦ MAX-HEAP-INSERT O(lgn) 25 of 26
  • 53. SUMMARY • We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) ◦ BUILD-MAX-HEAP O(n) ◦ HEAP-SORT O(nlgn) ◦ MAX-HEAP-INSERT O(lgn) ◦ HEAP-EXTRACT-MAX O(lgn) 25 of 26
  • 54. SUMMARY • We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) ◦ BUILD-MAX-HEAP O(n) ◦ HEAP-SORT O(nlgn) ◦ MAX-HEAP-INSERT O(lgn) ◦ HEAP-EXTRACT-MAX O(lgn) ◦ HEAP-INCREASE-KEY O(lgn) 25 of 26
  • 55. SUMMARY • We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) ◦ BUILD-MAX-HEAP O(n) ◦ HEAP-SORT O(nlgn) ◦ MAX-HEAP-INSERT O(lgn) ◦ HEAP-EXTRACT-MAX O(lgn) ◦ HEAP-INCREASE-KEY O(lgn) ◦ HEAP-MAXIMUM O(1) 25 of 26
  • 56. Building a heap • Presented To:- Dr. Arshad Aewan • Presented By:- SK Ahsan • Reg No:- 813/FBAS/MSCS/F14 • Topic:- Heap Sort(Procedure) • My Dreams Are My Own Drems(Me) 26 of 26