The document discusses heap sort, which is a sorting algorithm that uses a heap data structure. It works in two phases: first, it transforms the input array into a max heap using the insert heap procedure; second, it repeatedly extracts the maximum element from the heap and places it at the end of the sorted array, reheapifying the remaining elements. The key steps are building the heap, processing the heap by removing the root element and allowing the heap to reorder, and doing this repeatedly until the array is fully sorted.
The document provides an introduction to data structures. It defines data structures as representations of logical relationships between data elements that consider both the elements and their relationships. It classifies data structures as either primitive or non-primitive. Primitive structures are directly operated on by machine instructions while non-primitive structures are built from primitive ones. Common non-primitive structures include stacks, queues, linked lists, trees and graphs. The document then discusses arrays as a data structure and operations on arrays like traversal, insertion, deletion, searching and sorting.
Artificial intelligence has many applications in healthcare, including disease diagnosis, personalized treatment, drug discovery, robotic surgery, and clinical research. AI can more accurately diagnose diseases like cancer and heart disease using large amounts of medical data. It is also used to design personalized treatment plans and modify patient behavior based on individual health data. Additionally, AI assists with drug discovery, manufacturing, and selection of treatment paths for patients. Robotic surgery using machines like da Vinci allows for more precise procedures. AI has potential to transform healthcare by making processes like data analysis and repetitive jobs more efficient.
The document summarizes the heap sort algorithm. It begins by defining a binary heap and its properties, such as being a complete binary tree where all levels except the last are fully filled. It then explains that heap sort works by first converting the input array into a max heap, then repeatedly removing the maximum element and sinking it down to sort the array. The key steps of the heap sort algorithm are building the max heap, swapping the root with the last element, reducing the heap size, and re-heapifying the tree after each swap.
This document discusses project management techniques CPM and PERT. It begins by defining a project and project management. It then discusses network planning methods including CPM and PERT. The four steps to managing a project with these methods are described: describing the project, diagramming the network, estimating time of completion, and monitoring progress. Key concepts like activities, precedence relationships, and events are also defined. The document goes on to provide details on CPM and PERT, including estimating time, determining critical paths, and differences between the two methods.
Quicksort is a divide and conquer sorting algorithm that works by partitioning an array around a pivot value. It then recursively sorts the sub-arrays on each side. The key steps are: 1) Choose a pivot element to split the array into left and right halves, with all elements on the left being less than the pivot and all on the right being greater; 2) Recursively quicksort the left and right halves; 3) Combine the now-sorted left and right halves into a fully sorted array. The example demonstrates quicksorting an array of 6 elements by repeatedly partitioning around a pivot until the entire array is sorted.
Introduction to data structures and AlgorithmDhaval Kaneria
This document provides an introduction to algorithms and data structures. It defines algorithms as step-by-step processes to solve problems and discusses their properties, including being unambiguous, composed of a finite number of steps, and terminating. The document outlines the development process for algorithms and discusses their time and space complexity, noting worst-case, average-case, and best-case scenarios. Examples of iterative and recursive algorithms for calculating factorials are provided to illustrate time and space complexity analyses.
This document provides information about priority queues and binary heaps. It defines a binary heap as a nearly complete binary tree where the root node has the maximum/minimum value. It describes heap operations like insertion, deletion of max/min, and increasing/decreasing keys. The time complexity of these operations is O(log n). Heapsort, which uses a heap data structure, is also covered and has overall time complexity of O(n log n). Binary heaps are often used to implement priority queues and for algorithms like Dijkstra's and Prim's.
Hashing is a technique used to uniquely identify objects by assigning each object a key, such as a student ID or book ID number. A hash function converts large keys into smaller keys that are used as indices in a hash table, allowing for fast lookup of objects in O(1) time. Collisions, where two different keys hash to the same index, are resolved using techniques like separate chaining or linear probing. Common applications of hashing include databases, caches, and object representation in programming languages.
The document discusses binary trees and their representations and operations. It defines binary trees as trees where each node has at most two child nodes. It also defines complete binary trees as trees where every node has two children except leaf nodes. The document discusses array and linked representations of binary trees and various traversal operations like preorder, inorder and postorder traversals. It also provides code snippets for inserting and deleting nodes from a binary tree.
The document discusses heap data structures and their use in priority queues and heapsort. It defines a heap as a complete binary tree stored in an array. Each node stores a value, with the heap property being that a node's value is greater than or equal to its children's values (for a max heap). Algorithms like Max-Heapify, Build-Max-Heap, Heap-Extract-Max, and Heap-Increase-Key are presented to maintain the heap property during operations. Priority queues use heaps to efficiently retrieve the maximum element, while heapsort sorts an array by building a max heap and repeatedly extracting elements.
B-Trees are tree data structures used to store data on disk storage. They allow for efficient retrieval of data compared to binary trees when using disk storage due to reduced height. B-Trees group data into nodes that can have multiple children, reducing the height needed compared to binary trees. Keys are inserted by adding to leaf nodes or splitting nodes and promoting middle keys. Deletion involves removing from leaf nodes, borrowing/promoting keys, or joining nodes.
The document discusses graph traversal algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of how BFS and DFS work, including pseudocode for algorithms. It also discusses applications of BFS such as finding shortest paths and detecting bipartitions. Applications of DFS include finding connected components and topological sorting.
The document discusses recursion, including:
1) Recursion involves breaking a problem down into smaller subproblems until a base case is reached, then building up the solution to the overall problem from the solutions to the subproblems.
2) A recursive function is one that calls itself, with each call typically moving closer to a base case where the problem can be solved without recursion.
3) Recursion can be linear, involving one recursive call, or binary, involving two recursive calls to solve similar subproblems.
Stack and its Applications : Data Structures ADTSoumen Santra
Stacks are a data structure that follow the last-in, first-out (LIFO) principle. Elements are inserted and removed from the same end called the top of the stack. Common stack operations include push to add an element, pop to remove an element, peek to view the top element, and isEmpty to check if the stack is empty. Stacks have various applications like representing function call stacks, evaluating mathematical expressions, and solving puzzles like the Towers of Hanoi. They can be implemented using arrays or linked lists.
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
In computer science, tree traversal (also known as tree search) is a form of graph traversal and refers to the process of visiting (checking and/or updating) each node in a tree data structure, exactly once. Such traversals are classified by the order in which the nodes are visited. The following algorithms are described for a binary tree, but they may be generalized to other trees as well.
The document describes the bubble sort algorithm. Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. Each pass of bubble sort bubbles up the largest remaining element to its correct place near the end of the list. It takes multiple passes, with fewer comparisons each time, to fully sort the list from lowest to highest.
This presentation is useful to study about data structure and topic is Binary Tree Traversal. This is also useful to make a presentation about Binary Tree Traversal.
Queue is a first-in first-out (FIFO) data structure where elements can only be added to the rear of the queue and removed from the front of the queue. It has two pointers - a front pointer pointing to the front element and a rear pointer pointing to the rear element. Queues can be implemented using arrays or linked lists. Common queue operations include initialization, checking if empty/full, enqueue to add an element, and dequeue to remove an element. The document then describes how these operations work for queues implemented using arrays, linked lists, and circular arrays. It concludes by providing exercises to implement specific queue tasks.
Each node in a doubly linked list contains two pointers - one pointing to the next node and one pointing to the previous node. This allows traversal in both directions through the list. A doubly linked list supports common operations like insertion and deletion at both ends of the list as well as anywhere in the list by updating the relevant pointer fields of the nodes. While requiring more space per node, doubly linked lists allow easy traversal backwards through the list and reversing of the list.
Linked lists are linear data structures where each node contains a data field and a pointer to the next node. There are two types: singly linked lists where each node has a single next pointer, and doubly linked lists where each node has next and previous pointers. Common operations on linked lists include insertion and deletion which have O(1) time complexity for singly linked lists but require changing multiple pointers for doubly linked lists. Linked lists are useful when the number of elements is dynamic as they allow efficient insertions and deletions without shifting elements unlike arrays.
This document summarizes the heapify algorithm. The heapify algorithm rearranges a binary tree to maintain the heap property, where the root node is greater than or equal to its children (for a max heap) or less than or equal (for a min heap). It does this by comparing the root node to its children, swapping if needed, and then recursively heapifying the subtree. The complexity of the heapify algorithm is O(log n). Examples are provided of applying the heapify algorithm to transform an example tree into both a min heap and a max heap.
This document discusses leftist heaps, which are a type of priority queue implemented as a variant of a binary heap. Leftist heaps maintain the property that the right descendant of each node has a lower rank, or distance to the nearest leaf node, than the left descendant. This property helps keep the tree balanced during operations like insertion and merging of heaps that have a time complexity of O(log n). Deletion of the minimum element involves disconnecting and merging the left and right subtrees.
recognizer for a language, Deterministic finite automata, Non-deterministic finite automata, conversion of NFA to DFA, Regular Expression to NFA, Thomsons Construction
Heap sort is a sorting algorithm that uses a heap data structure. It has two main steps: 1) transforming the array into a max or min heap, and 2) performing the actual sort by extracting the largest/smallest element and transforming the remaining heap. Heap sort runs in O(n log n) time and uses O(1) constant memory, making it an efficient in-place sorting algorithm.
This document provides information about priority queues and binary heaps. It defines a binary heap as a nearly complete binary tree where the root node has the maximum/minimum value. It describes heap operations like insertion, deletion of max/min, and increasing/decreasing keys. The time complexity of these operations is O(log n). Heapsort, which uses a heap data structure, is also covered and has overall time complexity of O(n log n). Binary heaps are often used to implement priority queues and for algorithms like Dijkstra's and Prim's.
Hashing is a technique used to uniquely identify objects by assigning each object a key, such as a student ID or book ID number. A hash function converts large keys into smaller keys that are used as indices in a hash table, allowing for fast lookup of objects in O(1) time. Collisions, where two different keys hash to the same index, are resolved using techniques like separate chaining or linear probing. Common applications of hashing include databases, caches, and object representation in programming languages.
The document discusses binary trees and their representations and operations. It defines binary trees as trees where each node has at most two child nodes. It also defines complete binary trees as trees where every node has two children except leaf nodes. The document discusses array and linked representations of binary trees and various traversal operations like preorder, inorder and postorder traversals. It also provides code snippets for inserting and deleting nodes from a binary tree.
The document discusses heap data structures and their use in priority queues and heapsort. It defines a heap as a complete binary tree stored in an array. Each node stores a value, with the heap property being that a node's value is greater than or equal to its children's values (for a max heap). Algorithms like Max-Heapify, Build-Max-Heap, Heap-Extract-Max, and Heap-Increase-Key are presented to maintain the heap property during operations. Priority queues use heaps to efficiently retrieve the maximum element, while heapsort sorts an array by building a max heap and repeatedly extracting elements.
B-Trees are tree data structures used to store data on disk storage. They allow for efficient retrieval of data compared to binary trees when using disk storage due to reduced height. B-Trees group data into nodes that can have multiple children, reducing the height needed compared to binary trees. Keys are inserted by adding to leaf nodes or splitting nodes and promoting middle keys. Deletion involves removing from leaf nodes, borrowing/promoting keys, or joining nodes.
The document discusses graph traversal algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of how BFS and DFS work, including pseudocode for algorithms. It also discusses applications of BFS such as finding shortest paths and detecting bipartitions. Applications of DFS include finding connected components and topological sorting.
The document discusses recursion, including:
1) Recursion involves breaking a problem down into smaller subproblems until a base case is reached, then building up the solution to the overall problem from the solutions to the subproblems.
2) A recursive function is one that calls itself, with each call typically moving closer to a base case where the problem can be solved without recursion.
3) Recursion can be linear, involving one recursive call, or binary, involving two recursive calls to solve similar subproblems.
Stack and its Applications : Data Structures ADTSoumen Santra
Stacks are a data structure that follow the last-in, first-out (LIFO) principle. Elements are inserted and removed from the same end called the top of the stack. Common stack operations include push to add an element, pop to remove an element, peek to view the top element, and isEmpty to check if the stack is empty. Stacks have various applications like representing function call stacks, evaluating mathematical expressions, and solving puzzles like the Towers of Hanoi. They can be implemented using arrays or linked lists.
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
In computer science, tree traversal (also known as tree search) is a form of graph traversal and refers to the process of visiting (checking and/or updating) each node in a tree data structure, exactly once. Such traversals are classified by the order in which the nodes are visited. The following algorithms are described for a binary tree, but they may be generalized to other trees as well.
The document describes the bubble sort algorithm. Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. Each pass of bubble sort bubbles up the largest remaining element to its correct place near the end of the list. It takes multiple passes, with fewer comparisons each time, to fully sort the list from lowest to highest.
This presentation is useful to study about data structure and topic is Binary Tree Traversal. This is also useful to make a presentation about Binary Tree Traversal.
Queue is a first-in first-out (FIFO) data structure where elements can only be added to the rear of the queue and removed from the front of the queue. It has two pointers - a front pointer pointing to the front element and a rear pointer pointing to the rear element. Queues can be implemented using arrays or linked lists. Common queue operations include initialization, checking if empty/full, enqueue to add an element, and dequeue to remove an element. The document then describes how these operations work for queues implemented using arrays, linked lists, and circular arrays. It concludes by providing exercises to implement specific queue tasks.
Each node in a doubly linked list contains two pointers - one pointing to the next node and one pointing to the previous node. This allows traversal in both directions through the list. A doubly linked list supports common operations like insertion and deletion at both ends of the list as well as anywhere in the list by updating the relevant pointer fields of the nodes. While requiring more space per node, doubly linked lists allow easy traversal backwards through the list and reversing of the list.
Linked lists are linear data structures where each node contains a data field and a pointer to the next node. There are two types: singly linked lists where each node has a single next pointer, and doubly linked lists where each node has next and previous pointers. Common operations on linked lists include insertion and deletion which have O(1) time complexity for singly linked lists but require changing multiple pointers for doubly linked lists. Linked lists are useful when the number of elements is dynamic as they allow efficient insertions and deletions without shifting elements unlike arrays.
This document summarizes the heapify algorithm. The heapify algorithm rearranges a binary tree to maintain the heap property, where the root node is greater than or equal to its children (for a max heap) or less than or equal (for a min heap). It does this by comparing the root node to its children, swapping if needed, and then recursively heapifying the subtree. The complexity of the heapify algorithm is O(log n). Examples are provided of applying the heapify algorithm to transform an example tree into both a min heap and a max heap.
This document discusses leftist heaps, which are a type of priority queue implemented as a variant of a binary heap. Leftist heaps maintain the property that the right descendant of each node has a lower rank, or distance to the nearest leaf node, than the left descendant. This property helps keep the tree balanced during operations like insertion and merging of heaps that have a time complexity of O(log n). Deletion of the minimum element involves disconnecting and merging the left and right subtrees.
recognizer for a language, Deterministic finite automata, Non-deterministic finite automata, conversion of NFA to DFA, Regular Expression to NFA, Thomsons Construction
Heap sort is a sorting algorithm that uses a heap data structure. It has two main steps: 1) transforming the array into a max or min heap, and 2) performing the actual sort by extracting the largest/smallest element and transforming the remaining heap. Heap sort runs in O(n log n) time and uses O(1) constant memory, making it an efficient in-place sorting algorithm.
This document describes heap data structures and algorithms like heap sort. It defines a max heap and min heap. It explains the build heap, heapify, insertion and deletion algorithms. Build heap transforms an array into a max heap by applying heapify to each node from bottom to top. Heapify maintains the heap property when a node is added or removed. Heap sort works by building a max heap from the input array and then extracting elements from the root to sort the array in descending order.
HEAP SORT ILLUSTRATED WITH HEAPIFY, BUILD HEAP FOR DYNAMIC ARRAYS.
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.
Heap sort is a sorting algorithm that uses a heap data structure. It first transforms the input array into a max heap by calling the heapify procedure. It then repeatedly swaps the root element with the last element, reduces the size of the heap by 1, and calls heapify to restore the heap property. This process continues until the entire array is sorted. Heap sort has a time complexity of O(n log n) and space complexity of O(1), making it efficient for sorting large datasets.
The document discusses heap sort, a sorting algorithm that works by organizing data into a binary heap data structure. It has a time complexity of O(n log n) and space complexity of O(1), making it efficient for sorting large datasets. The algorithm involves building a max or min heap from an array and then repeatedly removing the maximum or minimum element and restoring the heap property.
The document discusses heap sort, a sorting algorithm that works by organizing data into a binary heap data structure. It has a time complexity of O(n log n) and space complexity of O(1), making it efficient for sorting large datasets. The algorithm involves building a max or min heap from the input data and then repeatedly removing the maximum or minimum element and restoring the heap property.
Heap sort is a sorting algorithm that uses a heap data structure. It works in two phases: (1) building a max heap from the input data, and (2) extracting elements from the heap and inserting them into the output array. This results in the elements being sorted in O(n log n) time with O(1) auxiliary space. Heap sort is often preferred over other sorting algorithms for its efficiency and not requiring additional storage space.
Heapsort is an O(n log n) sorting algorithm that uses a heap data structure. It works by first converting the input array into a max heap, where the largest element is stored at the root. It then repeatedly removes the largest element from the heap and inserts it into the sorted end of the array. This process continues until the heap is empty, resulting in a fully sorted array. The document provides detailed explanations and examples of how to construct a max heap from an array and how to remove and re-heapify elements during the sorting process.
The document discusses heapsort, an efficient sorting algorithm that uses a heap data structure. Heapsort first builds a max heap from the input array in O(n) time using a heapify procedure. It then extracts elements from the heap into the sorted output array, maintaining the heap property, resulting in an overall time complexity of O(n log n).
heap sort in the design anad analysis of algorithmsssuser7319f8
Heap sort is a sorting algorithm that uses a heap data structure. It works in two phases: first it builds a max heap from the input data, then it repeatedly extracts the largest element from the heap and inserts it into the sorted end of the data. This results in the elements being sorted in O(n log n) time using only O(1) additional memory space, making it efficient for both time and space complexity. It performs better than quicksort in the worst case but requires random access to data unlike merge sort.
Heap sort is a comparison-based sorting algorithm that uses a heap data structure. It works in two phases: first it builds a max heap from the input data and then extracts elements from the heap one by one, each time putting the largest remaining element in its sorted position. This results in the elements being sorted in non-decreasing order with a time complexity of O(n log n). Heap sort is an efficient in-place sorting algorithm that uses constant extra space.
Heapsort is an O(n log n) sorting algorithm that uses a heap data structure represented as a balanced binary tree. It works by first heapifying an input array in O(n log n) time to create a max heap. It then repeatedly removes the maximum element from the heap, putting it in its sorted position, and sifting the new root element down to reestablish the heap property, until the heap is empty. Overall, heapsort runs in O(n log n) time like other comparison-based sorting algorithms.
This document describes the heap sort algorithm. It begins by explaining heaps and their properties like the heap property. It then describes key heap operations like Max-Heapify, Build-Max-Heap, finding the maximum element, extract max, increase key, and insert. These operations allow heaps to function as priority queues. Heap sort works by building a max heap from the input array using Build-Max-Heap, then repeatedly extracting the maximum element and putting it in its sorted position. This runs in O(n log n) time, combining advantages of insertion and merge sort by sorting in-place like insertion sort but with a faster running time of merge sort.
: A heap is a nearly complete binary tree with the following two properties:
Structural property: all levels are full, except possibly the last one, which is filled from left to right
Order (heap) property: for any node x
Parent(x) ≥ x
Heaps are nearly complete binary trees stored as arrays that satisfy the heap property: for max-heaps a parent node is greater than or equal to its children, and for min-heaps a parent is less than or equal to its children. Heap operations like insert, extract max/min, and increase/decrease key take O(log n) time due to the heap's height. The heapsort algorithm uses a max-heap to sort an array in O(n log n) time by repeatedly extracting and placing the max element in its final position. Priority queues implemented with heaps support insertion, maximum/minimum extraction, and key increases/decreases in O(log n) time.
Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...Sérgio Sacani
One notable example of exoplanet diversity is the population of circumbinary planets, which orbit around both stars of a binary star system. There are so far only 16 known circumbinary exoplanets, all of which lie in the same orbital plane as the host binary. Suggestions exist that circumbinary planets could also exist on orbits highly inclined to the binary, close to 90◦, polar orbits. No such planets have been found yet but polar circumbinary gas and debris discs have been observed and if these were to form planets then those would be left on a polar orbit. We report strong evidence for a polar circumbinary exoplanet, which orbits a close pair of brown dwarfs which are on an eccentric orbit. We use radial-velocities to measure a retrograde apsidal precession for the binary, and show that this can only be attributed to the presence of a polar planet.
Applications of Radioisotopes in Cancer Research.pptxMahitaLaveti
:
This presentation explores the diverse and impactful applications of radioisotopes in cancer research, spanning from early detection to therapeutic interventions. It covers the principles of radiotracer development, radiolabeling techniques, and the use of isotopes such as technetium-99m, fluorine-18, iodine-131, and lutetium-177 in molecular imaging and radionuclide therapy. Key imaging modalities like SPECT and PET are discussed in the context of tumor detection, staging, treatment monitoring, and evaluation of tumor biology. The talk also highlights cutting-edge advancements in theranostics, the use of radiolabeled antibodies, and biodistribution studies in preclinical cancer models. Ethical and safety considerations in handling radioisotopes and their translational significance in personalized oncology are also addressed. This presentation aims to showcase how radioisotopes serve as indispensable tools in advancing cancer diagnosis, research, and targeted treatment.
Eric Schott- Environment, Animal and Human Health (3).pptxttalbert1
Baltimore’s Inner Harbor is getting cleaner. But is it safe to swim? Dr. Eric Schott and his team at IMET are working to answer that question. Their research looks at how sewage and bacteria get into the water — and how to track it.
1) Decorticate animal is the one without cerebral cortex
1) The preparation of decerebrate animal occurs because of the removal of all connections of cerebral hemispheres at the level of midbrain
Study in Pink (forensic case study of Death)memesologiesxd
A forensic case study to solve a mysterious death crime based on novel Sherlock Homes.
including following roles,
- Evidence Collector
- Cameraman
- Medical Examiner
- Detective
- Police officer
Enjoy the Show... ;)
This PowerPoint offers a basic idea about Plant Secondary Metabolites and their role in human health care systems. It also offers an idea of how the secondary metabolites are synthesised in plants and are used as pharmacologically active constituents in herbal medicines
This presentation explores the application of Discrete Choice Experiments (DCEs) to evaluate public preferences for environmental enhancements to Airthrey Loch, a freshwater lake located on the University of Stirling campus. The study aims to identify the most valued ecological and recreational improvements—such as water quality, biodiversity, and access facilities by analyzing how individuals make trade-offs among various attributes. The results provide insights for policy-makers and campus planners to design sustainable and community-preferred interventions. This work bridges environmental economics and conservation strategy using empirical, choice-based data analysis.
Euclid: The Story So far, a Departmental Colloquium at Maynooth UniversityPeter Coles
The European Space Agency's Euclid satellite was launched on 1st July 2023 and, after instrument calibration and performance verification, the main cosmological survey is now well under way. In this talk I will explain the main science goals of Euclid, give a brief summary of progress so far, showcase some of the science results already obtained, and set out the time line for future developments, including the main data releases and cosmological analysis.
2. Definitions of heap:
A heap is a data structure that stores a collection of
objects (with keys), and has the following
properties:
Complete Binary tree
Heap Order
3. The heap sort algorithm has two
major steps :
i. The first major step involves transforming the complete
tree into a heap.
ii. The second major step is to perform the actual sort by
extracting the largest or lowerst element from the root
and transforming the remaining tree into a heap.
7. 1-Max heap :
max-heap Definition:
is a complete binary tree in which the value in
each internal node is greater than or equal to
the values in the children of that node.
Max-heap property:
The key of a node is ≥ than the keys of its children.
8. 8
Max heap Operation
A heap can be stored as an
array A.
Root of tree is A[1]
Left child of A[i] = A[2i]
Right child of A[i] = A[2i + 1]
Parent of A[i] = A[ i/2 ]
19. Heap-Sort
sorting strategy:
1. Build Max Heap from unordered array;
2. Find maximum element A[1];
3. Swap elements A[n] and A[1] :
now max element is at the end of the array! .
4. Discard node n from heap
(by decrementing heap-size variable).
5. New root may violate max heap property, but its
children are max heaps. Run max_heapify to fix this.
6. Go to Step 2 unless heap is empty.
30. Heap Sort pseducode
Heapsort(A as array)
BuildHeap(A)
for i = n to 1
swap(A[1], A[i])
n = n - 1
Heapify(A, 1)
BuildHeap(A as array)
n = elements_in(A)
for i = floor(n/2) to 1
Heapify(A,i)
31. Heapify(A as array, i as int)
left = 2i
right = 2i+1
if (left <= n) and (A[left] > A[i])
max = left
else
max = i
if (right<=n) and (A[right] > A[max])
max = right
if (max != i)
swap(A[i], A[max])
Heapify(A, max)
32. 2-Min heap :
min-heap Definition:
is a complete binary tree in which the value in
each internal node is lower than or equal to the
values in the children of that node.
Min-heap property:
The key of a node is <= than the keys of its
children.
33. Min heap Operation
A heap can be stored as an array A.
Root of tree is A[0]
Left child of A[i] = A[2i+1]
Right child of A[i] = A[2i + 2]
Parent of A[i] = A[( i/2)-1]
33
49. Insertion
Algorithm
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
General strategy is percolate up (or bubble up): if
the parent of the element is smaller than the
element, then interchange the parent and child.
OR
Restore the min-heap property if violated
General strategy is percolate up (or bubble up): if
the parent of the element is larger than the element,
then interchange the parent and child.
50. 19
12 16
41 7
19
12 16
41 7 17
19
12 17
41 7 16
Insert 17
swap
Percolate up to maintain the heap
property
51. Conclusion The primary advantage of the heap sort is its
efficiency. The execution time efficiency of the
heap sort is O(n log n).
The memory efficiency of the heap sort, unlike
the other n log n sorts, is constant, O(1), because
the heap sort algorithm is not recursive.