Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Data Structures Lab 13 - Heapsort

ghghg

Uploaded by

doravivek6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Data Structures Lab 13 - Heapsort

ghghg

Uploaded by

doravivek6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structures Lab 14

Insertion Sort & Heap Sort


Outline
• Insertion Sort
• Introduction to heaps
• Max heap ADT
• Min heap ADT
• Heapify
• Heapsort
• Applications
• Exercise
• Prelab Questions
Insertion sort
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing
down on the table.
– Remove one card at a time from the table, and insert
it into the correct position in the left hand
• compare it with each of the cards already in the hand,
from right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the
table
Insertion Sort
input array

5 2 4 6 1 3

at each iteration, the array is divided in two sub-arrays:

left sub-array right sub-array

sorted unsorted
Insertion Sort
INSERTION-SORT
Alg.: INSERTION-SORT(A) 1 2 3 4 5 6 7 8

a1 a2 a3 a4 a5 a6 a7 a8
for j ← 2 to n
do key ← A[ j ] key

Insert A[ j ] into the sorted sequence A[1 . . j -1]

i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
• Insertion sort – sorts the elements in place
Time Complexity
• Best case: If given elements are in sorted
order the complexity is (n)
• Worst case: (n2)
HEAPS
 Heap is an application of Binary Tree.
 Property : The root of max heap/min heap contains the largest/smallest key.
 Heaps are frequently used to implement priority queues.
 A max tree is a tree in which the key value in each node is larger than the key values in its
children.
 A max heap is a complete binary tree that is also a max tree.
 A min tree is a tree in which the key value in each node is smaller than the key values in its
children.
 A min heap is a complete binary tree that is also a min tree.
 Operations on heaps:
 creation of an empty heap
 insertion of a new element into the heap;
 deletion of the largest element from the heap
 Max Heap Examples:

 Min Heap Examples:


Max Heap ADT
Structure MaxHeap
objects: a complete binary tree of n > 0 elements organized so that the value in each node is
at least as large as those in its children
functions:
for all heap belong to MaxHeap, item belong to Element, n, max_size belong to integer

MaxHeap Create(max_size) = create an empty heap that can hold a maximum of


max_size elements

Boolean HeapFull(heap, n) = if (n==max_size) return TRUE else return FALSE

MaxHeap Insert(heap, item, n) = if (!HeapFull(heap,n)) insert item into heap and


return the resulting heap else return error

Boolean HeapEmpty(heap, n)= if (n>0) return FALSE else return TRUE

Element Delete(heap,n) = if (!HeapEmpty(heap,n)) return one instance of the


largest element in the heap and remove it from the
heap else return error
Insertion into a Max Heap

• To determine the correct place for the element that is being inserted, we use a
bubbling up process that begins at the new node of the tree and moves towards the
root.
• The element to be inserted bubbles up as far as is necessary to ensure a max heap.
• Consider the max heap :

• Insert 5 : Initially adds 5 as the left child of 2, following the complete binary Tree
property

• to follow the max heap property the nodes 2 and 5 are swapped.
Insertion into a Max Heap

• Insert 21 : Following the complete binary tree property, the node 21 is initially inserted
as left child of node 2
• Which, violates the max heap property, therefore nodes 2 and 21 are swapped

• It still violates the max heap property, therefore nodes 20 and 21 are swapped.
Deletion from a Max Heap

• When an element is to be deleted from a max heap, it is taken from the root of the
heap.

• A deletion results in removal of 21


• Since the resulting heap has only 5 elements in it. The binary tree needs to be
restructured to correspond to a complete binary tree with 5 elements.
• To do this we remove the element in position(6) i.e. 2
• Now we have the right structure: 2
– But, root is empty and 2 is not in heap
– Therefore if 2 is inserted into root
• Its forms a binary tree but not a max heap
Deletion from a Max Heap

.
Find the largest element in the left and right children of root, Swap its position with the
node 2.

In this case node 20 is the largest child, hence swap it with 2


Representing Heaps

• Heaps can be representing using arrays.


• Since a heap form complete binary trees, there will no be any wastage of space.

• The heap can be represented as:


Representing Heaps

• Structure of Heap:
Example for Building max heap
• Consider the elements: 6 5 3 1 8 7 2 4
• The above array represented as a complete binary tree would be:

• But this tree does not follow the


max-heap property:

Tree-1
Heap Sort
• Heap sort is a comparison based sorting technique based on Binary Heap data structure.
• It is similar to selection sort where we first find the maximum element and place the
maximum element at the end.
• We repeat the same process for remaining elements.
• It is an in-place algorithm (the same array is used for all the steps), but not a stable sort.
• A sorting algorithm is said to be stable if two objects with equal keys appear in the same
order in sorted output as they appear in the input array to be sorted.

ALGORITHM
1. Build a max heap from the input data. (Sort in ascending order)
2. At this point, the largest item is stored at the root of the heap. Swap it with the
last item of the heap followed by reducing the size of heap by 1.
3. Finally, heapify the root of tree (i.e build a max heap from remaining elements)
4. Repeat above steps while size of heap is greater than 1.
Heap Sort & Heapify Algorithms
Heap Sort Algorithm:
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--) // Build heap (rearrange array)
heapify(arr, n, i);

for (int i=n-1; i>=0; i--) // One by one extract an element from heap
{
swap(&arr[0], &arr[i]); // Move current root to end
heapify(arr, i, 0); // call max heapify on the reduced heap
}
}

Heapify Algorithm:
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2

if (l < n && arr[l] > arr[largest]) // If left child is larger than root
largest = l;

if (r < n && arr[r] > arr[largest]) // If right child is larger than largest so far
largest = r;

if (largest != i) // If largest is not root


{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest); // Recursively heapify the affected sub-tree
}
Applications of Heaps
 Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses
because Quicksort is better in practice.
 Nevertheless, the Heap data structure itself is enormously used. Following are some uses other
than Heapsort.
 Priority Queues: Priority queues can be efficiently implemented using Binary Heap because it
supports insert(), delete() and extractmax(), etc operations in O(log n) time
 Heap Implemented priority queues are used in Graph algorithms like:
 Shortest path algorithms: Dijkstra’s algorithm
 Minimum spanning tree algorithms: Prim’s algorithm

They can also be used for :

• Data compression: Huffman Coding algorithm


• Event-driven simulation: customers in a line
• Selection problem: Finding kth- smallest element
Exercise
1) Write a program to implement insertion sort.
2) Write a program to sort the following elements in descending order (use Max-
heap)
5 12 64 1 37 90 91 97
3) Write a program to implement Heapsort using heapify.
Prelab Questions
1) Give the advantage and disadvantage of Insertion sort.
2) List applications of insertion sort.
3) What is the difference between min heap and max heap.
4) Prove in what way heapify is better than ordinary heap.
5) Explain how priority queues are implemented using heaps.
6) List some applications of heap sort.
7) Give the average and best case complexities of heap sort.

You might also like