0% found this document useful (0 votes)
126 views

Analysis and Design Algorithm GTU Practical 3

The document describes the max-heap sort algorithm. It involves building a max heap from an input array by calling the Max-Heapify method. This method rearranges elements in the array to satisfy the heap property. The algorithm then sorts the array by repeatedly swapping the first and last elements and calling Max-Heapify to maintain the heap structure. The time complexity of heap sort is O(nlogn) as building the heap takes O(n) time and each element is inserted in O(logn) time. The space complexity is O(1) as no extra space is required.

Uploaded by

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

Analysis and Design Algorithm GTU Practical 3

The document describes the max-heap sort algorithm. It involves building a max heap from an input array by calling the Max-Heapify method. This method rearranges elements in the array to satisfy the heap property. The algorithm then sorts the array by repeatedly swapping the first and last elements and calling Max-Heapify to maintain the heap structure. The time complexity of heap sort is O(nlogn) as building the heap takes O(n) time and each element is inserted in O(logn) time. The space complexity is O(1) as no extra space is required.

Uploaded by

Karan Mistry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 3:- Implementation of max-heap sort algorithm.

Explanation:

Algorithm:

1. Use array to store the data.


2. Start storing from index 1, not 0.
3. For any given node at position i:
• Its Left Child is at [2*i] if available.
• Its right child is at [2*i+1] if available.
• Its Parent Node is at [i/2] if available.
Heapify Method:

Heapify method rearranges the elements of an array where the left and right sub -
tree of ith element obeys the heap property.

Algorithm: Max-Heapify(numbers[], i)

leftchild := numbers[2i]
rightchild := numbers [2i + 1]
if leftchild ≤ numbers[].size and numbers[leftchild] > numbers[i]
largest := leftchild
else
largest := i
if rightchild ≤ numbers[].size and numbers[rightchild] >
numbers[largest]
largest := rightchild
if largest ≠ i
swap numbers[i] with numbers[largest]
Max-Heapify(numbers, largest)

Pseudo code:

void heap_sort(intArr[])

{
int heap_size = N;

build_maxheap(Arr);
for(int i = N; i >=2; i--)
{
swap|(Arr[1],Arr[ i ]);
heap_size = heap_size -1;
max_heapify(Arr,1, heap_size);
}
}
Analysis:
Heapify a single node takes O(log N) time complexity where N is the total
number of Nodes. Therefore, building the entire Heap will take N heapify
operations and the total time complexity will be O(N*logN).
Time Complexity

• Build max heap takes O(n/2) time


• We are calling for heapify inside the for loop, which may take the height
of the heap in the worst case for all comparison. Therefore time
complexity will become O(nlogn)
• Best Time Complexity: O(nlogn)
• Average Time Complexity: O(nlogn)
• Worst Time Complexity: O(nlogn)

Space Complexity

• No auxiliary space is required in Heapsort implementation that is we are


not using any arrays, linked list, stack, queue, etc to store our elements
• Hence space complexity is: O(1)

You might also like