PROGRAM FOR HEAP SORT IN C IN DEPTH – SHOUT CODERS

4 Likes Comment

WHAT IS A HEAP?

It is a binary tree with the following properties:
Property 1: it is a complete binary tree
Property 2: the value stored at a node is greater or equal to the values stored at the children (heap property)

LARGEST HEAP ELEMENT

From Property 2, the largest value of the heap is always stored at the root

heap Elements 20 14 17 8 6 9 4 1

HEAP IMPLEMENTATION USING ARRAY REPRESENTATION

A heap is a complete binary tree, so it is easy to be implemented using an array representation.

20 14 17 8 6 9 4 1

REMOVING THE LARGEST ELEMENT FROM THE HEAP

  • Copy the bottom rightmost element to the root.
  • Delete the bottom rightmost node
  • Fix the heap property by calling ReheapDown

INSERTING A NEW ELEMENT INTO THE HEAP

  • Insert the new element in the next bottom leftmost place
  • Fix the heap property by calling Reheapup


PRIORITY QUEUES:

It is a queue with each element being associated with a “priority”

From the elements in the queue, the one with the highest priority is dequeued first

DEFINITION OF MAX HEAP

  • Store data in ascending order
  • Has property of A[Parent(i)] >= A[i]

MAX HEAP ARRAY IS:

20 17 14 9 8 6 4 1

DEFINITION OF MIN HEAP:

  • Store data in descending order
  • Has property of A[Parent(i)] <= A[i]

MIN HEAP ARRAY IS:

1 4 6 8 9 14 17 20

INSERTION:

Algorithm
1. Add the new element to the next available position at the
lowest level

2. Restore the max-heap property if violated

  • General strategy is percolate up (or bubble up):
    if the parent of the element is smaller than the
    element, then interchange the parent and child.

OR
Restore the min-heap property if violated

  • General strategy is percolate up (or bubble up):
    if the parent of the element is larger than the
    element, then interchange the parent and child.

DELETION:

DELETE MAX:

  • Copy the last number to the root (overwrite the maximum element stored there).
  • Restore the max heap property by percolate down

DELETE MIN:

  • Copy the last number to the root (overwrite the minimum element stored there
  • Restore the min heap property by percolate down.

HEAP SORT

A sorting algorithm that works by first organizing the data to be sorted into a special type of binary tree called a heap.

PROCEDURES ON HEAP

  • HEAPIFY
  • BUILD HEAP
  • HEAP SORT

HEAPIFY:

Heapify picks the largest child key and compare it to the parent
key. If parent key is larger than heapify quits, otherwise it swaps
the parent key with the largest child key. So that the parent is now
becomes larger than its children.

BUILD HEAP

We can use the procedure ‘Heapify’ in a bottom-up fashion to
convert an array A[1 . . n] into a heap. Since the elements in the
subarray A[n/2 +1 . . n] are all leaves, the procedure BUILD_HEAP
goes through the remaining nodes of the tree and runs ‘Heapify’
on each one. The bottom-up order of processing node guarantees
that the subtree rooted at children are heap before ‘Heapify’ is run
at their parent.

HEAPSORT ALGORITHM:

The heap sort algorithm starts by using procedure BUILD-HEAP to
build a heap on the input array A[1 . . n]. Since the maximum
element of the array stored at the root A[1], it can be put into its
correct final position by exchanging it with A[n] (the last element in
A). If we now discard node n from the heap than the remaining
elements can be made into heap. Note that the new element at
the root may violate the heap property. All that is needed to
restore the heap property.

The heap sort algorithm consists of two phrases:

  • To sort the elements in the decreasing order, use a min heap
  • To sort the elements in the increasing order, use a max heap

Procedure:

C PROGRAM:

INPUT-OUTPUT:

TIME ANALYSIS:

  • Build Heap Algorithm will run in O(n) time
  • There are n-1 calls to Heapify each call requires O(log n) time
  • Heap sort program combine Build Heap program and Heapify, Therefore it has the running time of O(n log n) time
  • Total time complexity: O(n log n)

COMPARISON WITH QUICK SORT, MERGE SORT AND HEAP SORT

  • Quick sort is typically somewhat faster, due to better cache
    behavior and other factors, but the worst-case running time for
    quick sort is O (n 2 ), which is unacceptable for large data sets and
    can be deliberately triggered given enough knowledge of the
    implementation, creating a security risk.
  • The quick sort algorithm also requires Ω (log n) extra storage
    space, making it not a strictly in-place algorithm. This typically does not pose a problem except on the smallest embedded systems, or on systems where memory allocation is highly restricted. Constant space (in-place) variants of quick sort are possible to construct, but are rarely used in practice due to their extra complexity.
  • Thus, because of the O(n log n) upper bound on heap sort’s running time and constant upper bound on its auxiliary storage, embedded systems with real-time constraints or systems concerned with security often use heap sort.
  • Heap sort also competes with merge sort, which has the same time bounds, but requires Ω(n) auxiliary space, whereas heap sort requires only a constant amount. Heap sort also typically runs more quickly in practice. However, merge sort is simpler to understand than heap sort, is a stable sort, parallelizes better, and can be easily adapted to operate on linked lists and very large lists stored on slow-to-access media such as disk storage or network attached storage. Heap sort shares none of these benefits; in particular, it relies strongly on random access.

You might like

Saif

About the Author: Saif

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami