0
0
DSA C++programming~20 mins

Heap Sort Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Heap Sort on a small array
What is the output of the following C++ code after performing heap sort on the array?
DSA C++
void heapify(int arr[], int n, int i) {
    int largest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if (left < n && arr[left] > arr[largest])
        largest = left;

    if (right < n && arr[right] > arr[largest])
        largest = right;

    if (largest != i) {
        std::swap(arr[i], arr[largest]);
        heapify(arr, n, largest);
    }
}

void heapSort(int arr[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);

    for (int i = n - 1; i > 0; i--) {
        std::swap(arr[0], arr[i]);
        heapify(arr, i, 0);
    }
}

int main() {
    int arr[] = {4, 10, 3, 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    heapSort(arr, n);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A3 1 4 5 10
B10 5 4 3 1
C1 4 3 5 10
D1 3 4 5 10
Attempts:
2 left
💡 Hint
Heap sort sorts the array in ascending order by building a max heap first.
🧠 Conceptual
intermediate
1:30remaining
Understanding heapify function behavior
What is the main purpose of the heapify function in the heap sort algorithm?
ATo sort the array directly without building a heap
BTo swap the first and last elements of the array
CTo build a max heap from an unordered array segment
DTo reverse the array elements
Attempts:
2 left
💡 Hint
Heapify ensures the heap property for a subtree rooted at a given index.
Predict Output
advanced
2:30remaining
Output after partial heap sort steps
Given the array {7, 2, 6, 3, 9} and the heap sort algorithm, what is the array state after the first swap and heapify in the sorting phase?
DSA C++
void heapify(int arr[], int n, int i) {
    int largest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if (left < n && arr[left] > arr[largest])
        largest = left;

    if (right < n && arr[right] > arr[largest])
        largest = right;

    if (largest != i) {
        std::swap(arr[i], arr[largest]);
        heapify(arr, n, largest);
    }
}

void heapSort(int arr[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);

    std::swap(arr[0], arr[n-1]);
    heapify(arr, n-1, 0);
}

int main() {
    int arr[] = {7, 2, 6, 3, 9};
    int n = sizeof(arr) / sizeof(arr[0]);
    heapSort(arr, n);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A6 3 2 7 9
B7 3 6 2 9
C3 2 6 7 9
D2 3 6 7 9
Attempts:
2 left
💡 Hint
After building the max heap, the largest element is swapped with the last element, then heapify is called on the reduced heap.
🔧 Debug
advanced
2:00remaining
Identify the error in heapify implementation
What error will the following heapify function cause when called during heap sort?
DSA C++
void heapify(int arr[], int n, int i) {
    int largest = i;
    int left = 2 * i;
    int right = 2 * i + 1;

    if (left < n && arr[left] > arr[largest])
        largest = left;

    if (right < n && arr[right] > arr[largest])
        largest = right;

    if (largest != i) {
        std::swap(arr[i], arr[largest]);
        heapify(arr, n, largest);
    }
}
AArray index out of bounds error
BNo error, works correctly
CInfinite recursion causing stack overflow
DCompilation error due to wrong syntax
Attempts:
2 left
💡 Hint
Check how left and right child indices are calculated for a zero-based array.
🚀 Application
expert
1:30remaining
Heap sort stability and behavior
Which statement about heap sort is true?
AHeap sort is unstable and may change the relative order of equal elements
BHeap sort requires extra memory proportional to the array size
CHeap sort always sorts in descending order by default
DHeap sort is stable and maintains the relative order of equal elements
Attempts:
2 left
💡 Hint
Consider how heap sort swaps elements during sorting.