Analysis and Design Algorithm GTU Practical 3
Analysis and Design Algorithm GTU Practical 3
Explanation:
Algorithm:
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
Space Complexity