SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Heap Sort
Dr. Ra’Fat A. AL-msie’deen
Chapter 3
Mutah University
Faculty of IT, Department of Software Engineering
Outlines: Heap Sort
• Input: One-Dimension Array.
• Advantages of Insertion Sort and Merge Sort
• Heap Sort:
 The Heap Property.
 Heapify Function “MAX-HEAPIFY (A, i)”
 Build Heap Function “BUILD-MAX-HEAP(A)”
 Heap Sort Function.
• Max-Priority Queues (Basic Operations):
 Maximum
 Extract-Max
 Increase Key
 Insert Key
2
1D Array
• 1-dimensional array x = [a, y, f, k]
• x[1] = a; x[2] = y; x[3] = f; x[4] = k
3
a y f kMemory
start
Sorting Revisited
• So far we’ve talked about two algorithms to sort an array of
numbers:
o What is the advantage of merge sort?
 Answer: good worst-case running time O(n lg n).
 Conceptually easy, Divide-and-Conquer.
o What is the advantage of insertion sort?
 Answer: sorts in place: only a constant number of array
elements are stored outside the input array at any time.
 Easy to code, When array “nearly sorted”, runs fast in
practice.
 Avg. case=worst case=n2
• Next on the agenda: Heapsort.
 Combines advantages of both previous algorithms.
4
Heaps
• A heap can be seen as a complete binary tree.
• In practice, heaps are usually implemented as arrays.
• An array A that represent a heap is an object with two
attributes:
 A[1 .. length[A]].
 length[A]: # of elements in the array.
 heap-size[A]: # of elements in the heap stored within
array A, where heap-size[A] ≤ length[A].
 No element past A[heap-size[A]] is an element of the
heap.
5
1 5 6 77 8 99 14 66 72 101A =
Heaps (Cont.)
• For example, heap-size of the following heap = 10.
• Also, length[A] = 10.
6
Floors & Ceilings
• For any real number x, we denote the greatest
integer less than or equal to x by
o read “the floor of x”
• For any real number x, we denote the least integer
greater than or equal to x by
o read “the ceiling of x”
• For any integer n,
7
 x
 x
    nnn  2/2/
Referencing Heap Elements
• The root node is A[1].
• Node i is A[i].
• Parent(i) == > return i/2
• Left(i) == > return 2i
• Right(i) == > return 2i + 1
• Level 0 (2, 4, 1).
• Level 1 (8, 7, 9, 3).
• Level 2 (15, 10).
• Level 3 (16).
PARENT (i)
1 return i/2
LEFT (i)
1 return 2i
RIGHT (i)
1 return 2i + 1
The Heap Property
• Heaps also satisfy the heap property:
o A[Parent(i)] ≥ A[i] for all nodes i > 1
o In other words, the value of a node is at most the value of
its parent.
 The largest value in a heap is at its root (A[1]).
 and subtrees rooted at a specific node contain values
no larger than that node’s value.
9
Heap Operations: MAX-HEAPIFY()
• Heapify(): maintain the heap property.
• Given: a node i in the heap with children L and R.
• two subtrees rooted at L and R, assumed to be heaps.
• Problem: The subtree rooted at i may violate the heap
property (How?)
 A[i] may be smaller than its children value
• Action: let the value of the parent node “float down” so
subtree at i satisfies the heap property.
 If A[i] < A[L] or A[i] < A[R], swap A[i] with the largest of A[L]
and A[R].
 Recurse on that subtree.
10
Heap Operations: MAX-HEAPIFY()
MAX-HEAPIFY (A, i)
1 l = LEFT(i)
2 r = RIGHT(i)
3 if l ≤ A.heap-size and A[l] > A[i]
4 largest = l
5 else largest = i
6 if r ≤ A.heap-size and A[r] > A[largest]
7 largest = r
8 if largest ≠ i
9 exchange A[i] with A[largest]
10 MAX-HEAPIFY (A, largest)
11
Heap Operations: MAX-HEAPIFY()
 Heapify (A, 2) Example
 A.heap-size = 10.
 The max-heap property is restored for
node 2 in (b) by exchanging A[2] with A[4].
 The recursive call MAX-HEAPIFY (A, 4).
 Swapping A[4] with A[9].
 Heapify (A, 9)
16 4 10 14 7 9 3 2 8 1
16 14 10 8 7 9 3 2 4 1
Heap Height
• Definitions:
 The height of a node in the tree = the number of edges on
the longest downward path to a leaf.
13
# of nodes in each level
• Fact: an n-element heap has at most 2h-k nodes of
level k, where h is the height of the tree.
 for k = h (root level) == > 2h-h = 20 = 1.
 .
 .
 .
 .
 for k = 0 (leaves level) == > 2h-0 = 2h
14
Heap Operations: BUILD-MAX-HEAP()
• We can build a heap in a bottom-up manner by running
Heapify() on successive subarrays.
// given an unsorted array A, make A a heap
BUILD-MAX-HEAP (A)
1 A.heap-size = A.length
2 for i = [A.length/2] downto 1
3 MAX-HEAPIFY (A, i)
15
BUILD-MAX-HEAP() - Example
• Example: Building a max-heap from the following unsorted
array results in the first heap example. (Work through
example: A = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7})
 i starts off as 5. (n=10, n/2=5)
 MAX-HEAPIFY is applied to subtrees rooted at nodes (in
order): 16, 2, 3, 1, 4.
 A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
BUILD-MAX-HEAP() - Example
17
BUILD-MAX-HEAP() - Example
18
Heapsort
• Given BuildHeap(), an in-place sorting algorithm is easily
constructed:
 Maximum element is at A[1].
 Discard by swapping with element at A[n].
o Decrement heap_size[A].
o A[n] now contains correct value.
 Restore heap property at A[1] by calling Heapify().
 Repeat, always swapping A[1] for A[heap_size(A)].
19
Heapsort
HEAPSORT(A)
1 BUILD-MAX-HEAP(A)
2 for i = A.length downto 2
3 exchange A[1] with A[i]
4 A.heap-size = A.heap-size - 1
5 MAX-HEAPIFY(A, 1)
20
HEAPSORT( ) - Example
• The heapsort algorithm starts by using BUILD-MAX-HEAP to
build a max-heap on the input array A[1 … n], where n =
A.length.
• A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
21
The operation of HEAPSORT. (a) The max-heap data structure just after
BUILD-MAX-HEAP has built it in line 1.
HEAPSORT( ) - Example
22
i= 10 i = 9
 Given an input array, the heapsort algorithm acts as follows:
o Builds a max-heap from the array.
o Starting with the root (the maximum element), the algorithm places the
maximum element into the correct place in the array by swapping it with the
element in the last position in the array.
o “Discard” this last node (knowing that it is in its correct place) by decreasing
the heap size, and calling MAX-HEAPIFY on the new (possibly incorrectly-
placed) root.
o Repeat this "discarding" process until only one node (the smallest element)
remains, and therefore is in the correct place in the array.
HEAPSORT( ) - Example
23
A = {7, 4, 3, 1, 2, 8, 9, 10, 14, 16}
i = 6 i = 7
i = 8
i = 9 i = 10
HEAPSORT( ) - Example
24
HEAPSORT( ) – Example (2)
25
Outlines: Heap Sort
• Input: One-Dimension Array.
• Advantages of Insertion Sort and Merge Sort
• Heap Sort:
 The Heap Property.
 Heapify Function “MAX-HEAPIFY (A, i)”
 Build Heap Function “BUILD-MAX-HEAP(A)”
 Heap Sort Function.
• Max-Priority Queues (Basic Operations):
 Maximum
 Extract-Max
 Increase Key
 Insert Key
26
Max-Priority Queues
• A data structure for maintaining a set S of elements, each with
an associated value called a key.
• Applications:
o Scheduling jobs on a shared computer.
o Prioritizing events to be processed based on their
predicted time of occurrence.
o Printer queue.
• Heap can be used to implement a max-priority queue.
27
Max-Priority Queue: Basic Operations
• Maximum(S): → return A[1]
o returns the element of S with the largest key (value).
• Extract-Max(S):
o removes and returns the element of S with the largest
key.
• Increase-Key(S, x, k):
o increases the value of element x’s key to the new value k,
x.value ≤ k.
• Insert(S, x):
o inserts the element x into the set S, (i.e., S → S ∪ {x}).
28
Finding the maximum element
• Getting the maximum element is easy: it’s the
root.
HEAP-MAXIMUM (A)
1 return A[1]
• Time: Θ(1).
29
HEAP-EXTRACT-MAX (A)
• The procedure HEAP-EXTRACT-MAX implements the EXTRACT-
MAX operation.
HEAP-EXTRACT-MAX (A)
1 if A.heap-size < 1 // zero elements
2 error “heap underflow”
3 max = A[1] // max element in first position
4 A[1] = A[A.heap-size]
// value of last position assigned to first position
5 A.heap-size = A.heap-size - 1
6 MAX-HEAPIFY(A, 1)
7 return max
30
Note: lines 4-6 are similar to the for loop body of Heapsort procedure.
HEAP-INCREASE-KEY(A, i, key)
// increase a value (key) in the array
HEAP-INCREASE-KEY(A, i, key)
1 if key < A[i]
2 error “new key is smaller than current key”
3 A[i] = key
4 while i > 1 and A[Parent (i)] < A[i]
5 exchange A[i] with A[Parent (i)]
6 i = Parent (i) // move index up to parent
31
Increase-Key(A, 4, 15) Example
• A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}.
32
i=10i=9
Increase-Key(A, 4, 15) Example
• A = {16, 14, 10, 8, 7, 9, 3, 2, 15, 1}.
• The index i increased to 15.
33
Increase-Key(A, 4, 15) Example
• A = {16, 14, 10, 15, 7, 9, 3, 2, 8, 1}.
• After one iteration of the while loop of lines 4-6, the node and
its parent have exchanged keys (values), and the index i
moves up to the parent.
34
i=4
Increase-Key(A, 4, 15) Example
• A = {16, 15, 10, 14, 7, 9, 3, 2, 8, 1}.
• After one more iteration of the while loop.
• The max-heap property now holds and the procedure
terminates.
35
i=2
Increase-Key(A, 4, 15) Example
36
MAX-HEAP-INSERT (A)
// insert a value at the end of the binary tree then move it in the
right position
MAX-HEAP-INSERT(A, key)
1 A.heap-size = A.heap-size + 1
2 A[A.heap-size]= -∞
3 HEAP-INCREASE-KEY(A, A.heap-size, key)
37
Example: Operation of Heap Insert
• Figure x: The operation of HEAP-INSERT. (a) The heap of Figure (a) before
we insert a node with key 15. (b) A new leaf is added to the tree. (c)
Values on the path from the new leaf to the root are copied down until a
place for the key 15 is found. (d) The key 15 is inserted.
38
Heap Sort
Dr. Ra’Fat A. AL-msie’deen
Chapter 3
Mutah University
Faculty of IT, Department of Software Engineering

Weitere ähnliche Inhalte

Was ist angesagt?

Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
despicable me
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 

Was ist angesagt? (20)

Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sorting
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 
Counting sort
Counting sortCounting sort
Counting sort
 
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuanScientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority Queues
 
Python array
Python arrayPython array
Python array
 
Lec5
Lec5Lec5
Lec5
 
Sorting
SortingSorting
Sorting
 
Linked list
Linked listLinked list
Linked list
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Presentation
PresentationPresentation
Presentation
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Array operations
Array operationsArray operations
Array operations
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Intoduction to numpy
Intoduction to numpyIntoduction to numpy
Intoduction to numpy
 

Ähnlich wie Algorithms - "heap sort"

lecture 5
lecture 5lecture 5
lecture 5
sajinsc
 
lecture 6
lecture 6lecture 6
lecture 6
sajinsc
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
hardmarcelia
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
Israr63
 

Ähnlich wie Algorithms - "heap sort" (20)

lecture 5
lecture 5lecture 5
lecture 5
 
lecture 6
lecture 6lecture 6
lecture 6
 
Heap
HeapHeap
Heap
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
Heaps
HeapsHeaps
Heaps
 
21. Heap_new.ppt
21. Heap_new.ppt21. Heap_new.ppt
21. Heap_new.ppt
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
 
Heap tree
Heap treeHeap tree
Heap tree
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 

Mehr von Ra'Fat Al-Msie'deen

BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdfBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
Ra'Fat Al-Msie'deen
 
Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
Ra'Fat Al-Msie'deen
 
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
Ra'Fat Al-Msie'deen
 
Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...
Ra'Fat Al-Msie'deen
 

Mehr von Ra'Fat Al-Msie'deen (20)

SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdfSoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
 
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdfBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
 
Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
 
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
 
Source Code Summarization
Source Code SummarizationSource Code Summarization
Source Code Summarization
 
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
 
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
 
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
 
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
Automatic Labeling of the Object-oriented Source Code: The Lotus ApproachAutomatic Labeling of the Object-oriented Source Code: The Lotus Approach
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
 
Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...
 
Detecting commonality and variability in use-case diagram variants
Detecting commonality and variability in use-case diagram variantsDetecting commonality and variability in use-case diagram variants
Detecting commonality and variability in use-case diagram variants
 
Naming the Identified Feature Implementation Blocks from Software Source Code
Naming the Identified Feature Implementation Blocks from Software Source CodeNaming the Identified Feature Implementation Blocks from Software Source Code
Naming the Identified Feature Implementation Blocks from Software Source Code
 
Application architectures - Software Architecture and Design
Application architectures - Software Architecture and DesignApplication architectures - Software Architecture and Design
Application architectures - Software Architecture and Design
 
Planning and writing your documents - Software documentation
Planning and writing your documents - Software documentationPlanning and writing your documents - Software documentation
Planning and writing your documents - Software documentation
 
Requirements management planning & Requirements change management
Requirements management planning & Requirements change managementRequirements management planning & Requirements change management
Requirements management planning & Requirements change management
 
Requirements change - requirements engineering
Requirements change - requirements engineeringRequirements change - requirements engineering
Requirements change - requirements engineering
 
Requirements validation - requirements engineering
Requirements validation - requirements engineeringRequirements validation - requirements engineering
Requirements validation - requirements engineering
 
Software Documentation - writing to support - references
Software Documentation - writing to support - referencesSoftware Documentation - writing to support - references
Software Documentation - writing to support - references
 
Software Architecture and Design
Software Architecture and DesignSoftware Architecture and Design
Software Architecture and Design
 
Software Architecture and Design
Software Architecture and DesignSoftware Architecture and Design
Software Architecture and Design
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

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
 
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Ă...
 
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
 
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
 
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
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

Algorithms - "heap sort"

  • 1. Heap Sort Dr. Ra’Fat A. AL-msie’deen Chapter 3 Mutah University Faculty of IT, Department of Software Engineering
  • 2. Outlines: Heap Sort • Input: One-Dimension Array. • Advantages of Insertion Sort and Merge Sort • Heap Sort:  The Heap Property.  Heapify Function “MAX-HEAPIFY (A, i)”  Build Heap Function “BUILD-MAX-HEAP(A)”  Heap Sort Function. • Max-Priority Queues (Basic Operations):  Maximum  Extract-Max  Increase Key  Insert Key 2
  • 3. 1D Array • 1-dimensional array x = [a, y, f, k] • x[1] = a; x[2] = y; x[3] = f; x[4] = k 3 a y f kMemory start
  • 4. Sorting Revisited • So far we’ve talked about two algorithms to sort an array of numbers: o What is the advantage of merge sort?  Answer: good worst-case running time O(n lg n).  Conceptually easy, Divide-and-Conquer. o What is the advantage of insertion sort?  Answer: sorts in place: only a constant number of array elements are stored outside the input array at any time.  Easy to code, When array “nearly sorted”, runs fast in practice.  Avg. case=worst case=n2 • Next on the agenda: Heapsort.  Combines advantages of both previous algorithms. 4
  • 5. Heaps • A heap can be seen as a complete binary tree. • In practice, heaps are usually implemented as arrays. • An array A that represent a heap is an object with two attributes:  A[1 .. length[A]].  length[A]: # of elements in the array.  heap-size[A]: # of elements in the heap stored within array A, where heap-size[A] ≤ length[A].  No element past A[heap-size[A]] is an element of the heap. 5 1 5 6 77 8 99 14 66 72 101A =
  • 6. Heaps (Cont.) • For example, heap-size of the following heap = 10. • Also, length[A] = 10. 6
  • 7. Floors & Ceilings • For any real number x, we denote the greatest integer less than or equal to x by o read “the floor of x” • For any real number x, we denote the least integer greater than or equal to x by o read “the ceiling of x” • For any integer n, 7  x  x     nnn  2/2/
  • 8. Referencing Heap Elements • The root node is A[1]. • Node i is A[i]. • Parent(i) == > return i/2 • Left(i) == > return 2i • Right(i) == > return 2i + 1 • Level 0 (2, 4, 1). • Level 1 (8, 7, 9, 3). • Level 2 (15, 10). • Level 3 (16). PARENT (i) 1 return i/2 LEFT (i) 1 return 2i RIGHT (i) 1 return 2i + 1
  • 9. The Heap Property • Heaps also satisfy the heap property: o A[Parent(i)] ≥ A[i] for all nodes i > 1 o In other words, the value of a node is at most the value of its parent.  The largest value in a heap is at its root (A[1]).  and subtrees rooted at a specific node contain values no larger than that node’s value. 9
  • 10. Heap Operations: MAX-HEAPIFY() • Heapify(): maintain the heap property. • Given: a node i in the heap with children L and R. • two subtrees rooted at L and R, assumed to be heaps. • Problem: The subtree rooted at i may violate the heap property (How?)  A[i] may be smaller than its children value • Action: let the value of the parent node “float down” so subtree at i satisfies the heap property.  If A[i] < A[L] or A[i] < A[R], swap A[i] with the largest of A[L] and A[R].  Recurse on that subtree. 10
  • 11. Heap Operations: MAX-HEAPIFY() MAX-HEAPIFY (A, i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l ≤ A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r ≤ A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest ≠ i 9 exchange A[i] with A[largest] 10 MAX-HEAPIFY (A, largest) 11
  • 12. Heap Operations: MAX-HEAPIFY()  Heapify (A, 2) Example  A.heap-size = 10.  The max-heap property is restored for node 2 in (b) by exchanging A[2] with A[4].  The recursive call MAX-HEAPIFY (A, 4).  Swapping A[4] with A[9].  Heapify (A, 9) 16 4 10 14 7 9 3 2 8 1 16 14 10 8 7 9 3 2 4 1
  • 13. Heap Height • Definitions:  The height of a node in the tree = the number of edges on the longest downward path to a leaf. 13
  • 14. # of nodes in each level • Fact: an n-element heap has at most 2h-k nodes of level k, where h is the height of the tree.  for k = h (root level) == > 2h-h = 20 = 1.  .  .  .  .  for k = 0 (leaves level) == > 2h-0 = 2h 14
  • 15. Heap Operations: BUILD-MAX-HEAP() • We can build a heap in a bottom-up manner by running Heapify() on successive subarrays. // given an unsorted array A, make A a heap BUILD-MAX-HEAP (A) 1 A.heap-size = A.length 2 for i = [A.length/2] downto 1 3 MAX-HEAPIFY (A, i) 15
  • 16. BUILD-MAX-HEAP() - Example • Example: Building a max-heap from the following unsorted array results in the first heap example. (Work through example: A = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7})  i starts off as 5. (n=10, n/2=5)  MAX-HEAPIFY is applied to subtrees rooted at nodes (in order): 16, 2, 3, 1, 4.  A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
  • 19. Heapsort • Given BuildHeap(), an in-place sorting algorithm is easily constructed:  Maximum element is at A[1].  Discard by swapping with element at A[n]. o Decrement heap_size[A]. o A[n] now contains correct value.  Restore heap property at A[1] by calling Heapify().  Repeat, always swapping A[1] for A[heap_size(A)]. 19
  • 20. Heapsort HEAPSORT(A) 1 BUILD-MAX-HEAP(A) 2 for i = A.length downto 2 3 exchange A[1] with A[i] 4 A.heap-size = A.heap-size - 1 5 MAX-HEAPIFY(A, 1) 20
  • 21. HEAPSORT( ) - Example • The heapsort algorithm starts by using BUILD-MAX-HEAP to build a max-heap on the input array A[1 … n], where n = A.length. • A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1} 21 The operation of HEAPSORT. (a) The max-heap data structure just after BUILD-MAX-HEAP has built it in line 1.
  • 22. HEAPSORT( ) - Example 22 i= 10 i = 9  Given an input array, the heapsort algorithm acts as follows: o Builds a max-heap from the array. o Starting with the root (the maximum element), the algorithm places the maximum element into the correct place in the array by swapping it with the element in the last position in the array. o “Discard” this last node (knowing that it is in its correct place) by decreasing the heap size, and calling MAX-HEAPIFY on the new (possibly incorrectly- placed) root. o Repeat this "discarding" process until only one node (the smallest element) remains, and therefore is in the correct place in the array.
  • 23. HEAPSORT( ) - Example 23 A = {7, 4, 3, 1, 2, 8, 9, 10, 14, 16} i = 6 i = 7 i = 8 i = 9 i = 10
  • 24. HEAPSORT( ) - Example 24
  • 25. HEAPSORT( ) – Example (2) 25
  • 26. Outlines: Heap Sort • Input: One-Dimension Array. • Advantages of Insertion Sort and Merge Sort • Heap Sort:  The Heap Property.  Heapify Function “MAX-HEAPIFY (A, i)”  Build Heap Function “BUILD-MAX-HEAP(A)”  Heap Sort Function. • Max-Priority Queues (Basic Operations):  Maximum  Extract-Max  Increase Key  Insert Key 26
  • 27. Max-Priority Queues • A data structure for maintaining a set S of elements, each with an associated value called a key. • Applications: o Scheduling jobs on a shared computer. o Prioritizing events to be processed based on their predicted time of occurrence. o Printer queue. • Heap can be used to implement a max-priority queue. 27
  • 28. Max-Priority Queue: Basic Operations • Maximum(S): → return A[1] o returns the element of S with the largest key (value). • Extract-Max(S): o removes and returns the element of S with the largest key. • Increase-Key(S, x, k): o increases the value of element x’s key to the new value k, x.value ≤ k. • Insert(S, x): o inserts the element x into the set S, (i.e., S → S ∪ {x}). 28
  • 29. Finding the maximum element • Getting the maximum element is easy: it’s the root. HEAP-MAXIMUM (A) 1 return A[1] • Time: Θ(1). 29
  • 30. HEAP-EXTRACT-MAX (A) • The procedure HEAP-EXTRACT-MAX implements the EXTRACT- MAX operation. HEAP-EXTRACT-MAX (A) 1 if A.heap-size < 1 // zero elements 2 error “heap underflow” 3 max = A[1] // max element in first position 4 A[1] = A[A.heap-size] // value of last position assigned to first position 5 A.heap-size = A.heap-size - 1 6 MAX-HEAPIFY(A, 1) 7 return max 30 Note: lines 4-6 are similar to the for loop body of Heapsort procedure.
  • 31. HEAP-INCREASE-KEY(A, i, key) // increase a value (key) in the array HEAP-INCREASE-KEY(A, i, key) 1 if key < A[i] 2 error “new key is smaller than current key” 3 A[i] = key 4 while i > 1 and A[Parent (i)] < A[i] 5 exchange A[i] with A[Parent (i)] 6 i = Parent (i) // move index up to parent 31
  • 32. Increase-Key(A, 4, 15) Example • A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}. 32 i=10i=9
  • 33. Increase-Key(A, 4, 15) Example • A = {16, 14, 10, 8, 7, 9, 3, 2, 15, 1}. • The index i increased to 15. 33
  • 34. Increase-Key(A, 4, 15) Example • A = {16, 14, 10, 15, 7, 9, 3, 2, 8, 1}. • After one iteration of the while loop of lines 4-6, the node and its parent have exchanged keys (values), and the index i moves up to the parent. 34 i=4
  • 35. Increase-Key(A, 4, 15) Example • A = {16, 15, 10, 14, 7, 9, 3, 2, 8, 1}. • After one more iteration of the while loop. • The max-heap property now holds and the procedure terminates. 35 i=2
  • 37. MAX-HEAP-INSERT (A) // insert a value at the end of the binary tree then move it in the right position MAX-HEAP-INSERT(A, key) 1 A.heap-size = A.heap-size + 1 2 A[A.heap-size]= -∞ 3 HEAP-INCREASE-KEY(A, A.heap-size, key) 37
  • 38. Example: Operation of Heap Insert • Figure x: The operation of HEAP-INSERT. (a) The heap of Figure (a) before we insert a node with key 15. (b) A new leaf is added to the tree. (c) Values on the path from the new leaf to the root are copied down until a place for the key 15 is found. (d) The key 15 is inserted. 38
  • 39. Heap Sort Dr. Ra’Fat A. AL-msie’deen Chapter 3 Mutah University Faculty of IT, Department of Software Engineering