Medium Algorithms Sorting Algorithms
How does heap sort work and what are its properties?
Answer
Heap sort builds a max-heap from the array (O(n)), then repeatedly extracts the maximum, places it at the end, and re-heapifies (O(n log n)). Total complexity is O(n log n) for all cases - no worst-case degradation like quicksort. Space is O(1) - truly in-place. However, it's not stable and has poor cache locality compared to quicksort. Used when guaranteed O(n log n) is needed without extra memory.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Backend Developer Systems Developer