# Heap Sort in Data Structure

Mentor: Shailendra Chauhan
Duration : 00:04:00

## What is Heap Sort in Data Structure?

Heap sort visualizes the array's elements as a special type of complete binary tree known as a heap. It is similar to the selection sort in that we first identify the minimal element and set it at the beginning. It follows the same procedure for the remaining items.

## Working of Heap Sort Algorithm

• Build Max Heap: Create a max heap by visualizing array elements as a binary tree, with the largest element at the root.
• Repeat steps: Continue until the heap has only one element left.
• Swap: Move the root element to the end of the array (nth position), and then replace it with the heap's last item.
• Remove: Decrease the heap size by one.
• Heapify: rearrange the root so that the highest element remains at the top.
• Obtain Sorted Array: To obtain the sorted array, reverse the element order of the input array.

## Heapify a Tree

• Heapify rearranges items to preserve heap characteristics.
• After insertion, elements may not meet heap characteristics, necessitating rearrangement.
• Heapify converts a whole binary tree to a Max-Heap.
• It operates on non-leaf elements to guarantee that the heap structure is preserved.

## Complexity Analysis of Heap Sort Algorithm

### Time Complexity

#### Best Case Complexity

This occurs when no sorting is necessary, implying that the array is already sorted. The best-case time complexity for heap sort is O(n logn).

#### Average Case Complexity

This arises when the array elements are jumbled and do not appropriately climb or descend. The average time complexity of a heap sort is O(n log n).

#### Worst Case Complexity

This occurs when array members must be sorted in reverse order. That is, assume you need to sort the array elements in ascending order but the elements are in descending order. The worst-case time complexity for heap sort is O(n log n).

### Space Complexity

The space complexity is O(1) since we have a set amount of variables and do not require any more memory space other than the loop variables and auxiliary variables such as temp, n, index, and biggest.

## Applications of Heap Sort

• Sorting Efficiency: Heap sort has a worst-case time complexity of O(n log n) for array sorting.
• Priority Queue: Uses a priority queue to ensure effective insertion and extraction based on element priority.
• Top-K Elements: Using a max-heap, the top-k elements are found in O(k log n).
• Median Finding: Using two heaps, the median is calculated in O(log n) time for each element.
• External Sorting: Used for huge datasets, particularly during the initial sorting step.

• Efficiency: Heap sort has a continuous temporal complexity of O(n log n), outperforming bubble sort and insertion sort.
• Space Efficiency: Sorts items in place, using no additional memory.
• Guaranteed Performance: Unlike quicksort, the worst-case time complexity is always O(n log n).
• In-place Sorting: Works directly on the input array and is ideal for memory-constrained circumstances.
• Stability: Preserves relative element order, which is critical in some applications.
• Simplicity: The basic approach is creating a binary heap and repeatedly extracting extreme elements.
• Partial Sorting: Easily adaptable to partial sorting, which is beneficial for finding the top k components.

• Not Stable: Due to its unstable sorting, it is unsuitable for stability-dependent scenarios.
• Additional Space: Even with in-place sorting, heap formation requires additional space.
• Cache Performance: Non-sequential memory access lowers performance, especially for big arrays.
• Complexity: New programmers may struggle with intricate implementation.
• Non-adaptive: Lacks optimization for different initial orders, but performs consistently regardless.
Still have some questions? Let's discuss.