You are on page 1of 5

Data Structures

Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Heap Sort / HEAP SORT

HEAP SORT
Definition

Example

Algorithm - maxHeap

Definition: Heap Sort

Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly. If α has child node β then,  

As the value of parent is greater than that of child, this property generates Max Heap. Based on this criteria, a heap can be of two types:

Min-Heap, where the value of the root node is less than or equal to either of its children.

Max-Heap, where the value of the root node is greater than or equal to either of its children.

Both trees are constructed using the same input and order of arrival.

Steps to be followed for heap sort

Step 1 − Create a new node at the end of heap.

Step 2 − Assign new value to the node.

Step 3 − Compare the value of this child node with its parent.

Step 4 − If value of parent is less than child, then swap them.

Step 5 − Repeat step 3 & 4 until Heap property holds.

Example

◄ Program - Quick and Merge Sort Jump to... Unit 2 ►


Data Structures
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Heap Sort / HEAP SORT

HEAP SORT
Definition

Example

Algorithm - maxHeap

Algorithm: maxHeap

procedure heapsort(a, count) is

    input: an unordered array a of length count

     (Build the heap in array a so that largest value is at the root)

    heapify(a, count)

     (The following loop maintains the invariants that a[0:end] is a heap and every element

     beyond end is greater than everything before it (so a[end:count] is in sorted order))

    end ← count - 1

    while end > 0 do

        (a[0] is the root and largest value. The swap moves it in front of the sorted elements.)

        swap(a[end], a[0])

        (the heap size is reduced by one)

        end ← end - 1

        (the swap ruined the heap property, so restore it)

        siftDown(a, 0, end)

Challenge Questions

◄ Program - Quick and Merge Sort Jump to... Unit 2 ►


Data Structures
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Heap Sort / HEAP SORT

HEAP SORT
Definition

Example

Algorithm - maxHeap

Example

Input → 35   33   42   10   14   19   27   44   26   31

Step 1:

Step 2:

Step 3:

Step 4:

Step 5:
Algorithm - maxHeap

◄ Program - Quick and Merge Sort Jump to... Unit 2 ►

You might also like