ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-213Heap Sort
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-213

    Data Structures & Algorithms

    Progress0 / 37 done
    Topics37
    1Abstract Data Types2Complexity Analysis3Big Oh Notation4Stacks (Linked Lists and Array Implementations)5Recursion and analyzing recursive algorithms6Divide and Conquer Algorithms7Sorting Algorithms8Selection Sort9Insertion Sort10Merge Sort11Quick Sort12Bubble Sort13Heap Sort14Shell Sort15Radix Sort16Bucket Sort17Queue18Dequeuer19Priority Queues (linked and array implementations of queues)20Linked List and Its Various Types21Sorted Linked List22Searching an Unsorted Array23Binary Search for Sorted Arrays24Hashing and Indexing25Open Addressing and Chaining26Trees and Tree Traversals27Binary Search Trees28Heaps29M-Way Trees30Balanced Trees31Graphs32Breadth-First Traversal33Depth-First Traversal34Topological Order35Shortest Path36Adjacency Matrix and Adjacency List Implementations37Memory Management and Garbage Collection
    5 min read
    Topic 13 of 37
    Topic Study Note Illustration
    13

    Heap Sort

    Description : Heap sort is a comparison-based sorting algorithm that uses a data structure called a binary heap. It first builds a max heap (or min heap)...

    Heap Sort

    Description: Heap sort is a comparison-based sorting algorithm that uses a data structure called a binary heap. It first builds a max heap (or min heap) from the input data, and then it repeatedly extracts the maximum (or minimum) element from the heap and reconstructs the heap until the array is sorted.

    How It Works

    Binary heap
    Visual ReferenceBinary Heap
    Wikipedia
    1. Build a Max Heap: Convert the array into a max heap, where the largest element is at the root.
    2. Extract Maximum: Remove the root element (the maximum) and place it at the end of the array. Reduce the size of the heap by one.
    3. Re-Heapify: Restore the heap property by calling a heapify function on the root.
    4. Repeat: Continue extracting the maximum and re-heapifying until the heap is empty.

    Time Complexity

    • Worst Case: O(nlog⁡n)O(n \log n)O(nlogn)
    • Average Case: O(nlog⁡n)O(n \log n)O(nlogn)
    • Best Case: O(nlog⁡n)O(n \log n)O(nlogn) — heap sort performs consistently across all cases.

    Space Complexity

    • Space Complexity: O(1)O(1)O(1) — heap sort is an in-place sorting algorithm, requiring only a constant amount of additional space.

    Stability

    • Stability: Heap sort is not a stable sort. Equal elements may not maintain their relative order after sorting.

    Example Code

    Here's a C++ implementation of heap sort:

    #include <iostream>
    using namespace std;
    
    // Function to heapify a subtree rooted at index i
    void heapify(int arr[], int n, int i) {
        int largest = i; // Initialize largest as root
        int left = 2 * i + 1; // left = 2*i + 1
        int right = 2 * i + 2; // right = 2*i + 2
    
        // If left child is larger than root
        if (left < n && arr[left] > arr[largest]) {
            largest = left;
        }
    
        // If right child is larger than largest so far
        if (right < n && arr[right] > arr[largest]) {
            largest = right;
        }
    
        // If largest is not root, swap and continue heapifying
        if (largest != i) {
            swap(arr[i], arr[largest]);
            heapify(arr, n, largest);
        }
    }
    
    // Main function to sort an array using heap sort
    void heapSort(int arr[], int n) {
        // Build a max heap
        for (int i = n / 2 - 1; i >= 0; i--) {
            heapify(arr, n, i);
        }
    
        // One by one extract elements from heap
        for (int i = n - 1; i >= 0; i--) {
            swap(arr[0], arr[i]); // Move current root to end
            heapify(arr, i, 0); // Heapify the reduced heap
        }
    }
    
    int main() {
        int arr[] = {12, 11, 13, 5, 6, 7};
        int n = sizeof(arr) / sizeof(arr[0]);
        heapSort(arr, n);
    
        cout << "Sorted array: ";
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    Explanation of the Code

    1. Function Definitions:

      • heapify: Ensures the subtree rooted at index i maintains the heap property. It checks the left and right children and swaps elements as necessary.
      • heapSort: Manages the overall sorting process. It first builds the heap and then repeatedly extracts the maximum element.
    2. Building the Heap:

      • The first loop in heapSort constructs the max heap by calling heapify on each non-leaf node starting from the last parent node down to the root.
    3. Extracting Elements:

      • The second loop extracts the maximum element (the root of the heap) by swapping it with the last element in the array, reducing the heap size, and calling heapify to restore the heap property.
    4. Output: After sorting, the program prints the sorted array.

    Conclusion

    Heap sort is an efficient sorting algorithm with a guaranteed O(nlog⁡n)O(n \log n)O(nlogn) time complexity. It uses a binary heap data structure to sort elements in place, making it suitable for large datasets. However, its lack of stability and the more complex implementation compared to simpler algorithms like quick sort and merge sort might make it less popular in certain contexts. Nevertheless, heap sort is often used in applications where memory usage is a concern, as it operates in constant space.

    ← Previous topic 12
    Bubble Sort
    Next topic 14 →
    Shell Sort

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    5mremaining
    779words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Data Structures & Algorithms