SlideShare uma empresa Scribd logo
1 de 56
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxAlbin562191
 
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 algorithmssamairaakram
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 

Mais procurados (20)

Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
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
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
stack presentation
stack presentationstack presentation
stack presentation
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Merge sort
Merge sortMerge sort
Merge sort
 
Stack
StackStack
Stack
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Quick sort
Quick sortQuick sort
Quick sort
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Linked List
Linked ListLinked List
Linked List
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 
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
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 

Último

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
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
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Último (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

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