0% found this document useful (0 votes)
20 views9 pages

Heapsort Exe

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Heapsort Exe

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

XCS 405- DESIGN AND ANALYSIS OF

ALGORITHMS

MINI PROJECT

on the title

IMPLEMENT HEAPSORT AND FIND


OUT THE TIME COMPLEXITIES

Submitted by:

AHAMED SUFIYAN.A -121012063229


VIGNESH.R -121012063244
SAKTHI PRAHADESH.P -121012063240

Under Supervision : Dr.T.Kavitha, Asst. Prof.

Department of Computer Science


&Engineering
ACKNOWLEDGMENT

We extend our heartfelt and sincere thanks to the Management of Periyar


Maniammai Institute of Science and Technology for providing us with all sorts
of support for the completion of this Mini Project. We record obligations to
Dr.T.Kavitha,Assistant Professor, Periyar Maniammai Institute of Science and
Technology for their guidance and sustained encouragement for the successful
completion of this Mini Project. We are highly grateful and wholeheartedly
express our gratitude to Dr.M.Sharmila Begum, Professor and Head,Department
of Computer Science and Engineering, for her expert guidance, suggestions, and
active encouragement for the fulfillment of the Mini Project. We record our deep
sense of indebtedness to our Mini Project Guide Dr.T.Kavitha, for his unstinted
support and guidance throughout this Mini Project. We extend our deep sense
of gratitude to our Mini project Coordinator, Assistant Professor, Department
of Computer Science and Technology for her significant guidance and her
persistent help in all our endeavors. We also take pleasure in expressing our
humble note of gratitude to other Faculty members, Non-Teaching Staff
members, Parents, Friends and our seniors for providing their moral support in
successful completion ofthis Mini Project.
ABSTRACT

Heap sort is performed on the heap data structure. We know that heap is a
complete binary tree. Heap tree can be of two types. Min-heap or max heap.
For min heap the root element is minimum and for max heap the root is
maximum. After forming a heap, we can delete an element from the root and
send the last element to the root. After these swapping procedure, we need
to re-heap the whole array. By deleting elements from root we can sort the
whole array

ALGORITHM DESCRIPTION :

The heapsort algorithm uses the data structure called the heap. A heap is
defined as a complete binary tree in which each node has a value greater than
both its children (if any). Each node in the heap corresponds to an element of
the array, with the root node corresponding to the element with index 0 in
the array. Considering a node corresponding to index i, then its left child has
index (2*i + 1) and its right child has index (2*i + 2). If any or both of these
elements do not exist in the array, then the corresponding child node does
not exist either. Note that in a heap the largest element is located at the root
node. The code for the algorithm is:
ALGORITHM:

Heapify(A, i){
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-> A[largest]
Heapify(A, largest)
}

Buildheap(A){
heapsize[A] <- length[A]
for i <- |length[A]/2| downto 1
do Heapify(A, i)
}

Heapsort(A){
Buildheap(A)
for i <- length[A] downto 2
do swap A[1] <-> A[i]
heapsize[A] <- heapsize[A] - 1
Heapify(A, 1)
}
EXPLANATION OF ALGORITHM:

Heap Sort operates by first converting the initial array into a heap. The HeapSort
algorithm uses a process called Heapify to accomplish its job. The Heapify
algorithm, as shown above, receives a binary tree as input and converts it to a
heap. The root is then compared with its two immediate children, and the larger
child is swapped with it. This may result in one of the left or right subtrees losing
their heap property. Consequently, the Heapify algorithm is recursively applied to
the appropriate subtree rooted at the the node whose value was just swapped
with the root and the process continues until either a leaf node is reached, or it is
determined that the heap property is satisfied in the subtree.

The entire HeapSort method consists of two major steps:

STEP 1: Construct the initial heap using adjust (N/2) times on all non-leaf
nodes.
STEP 2: Sort by swapping and adjusting the heap (N-1) times.

In step 2, since the largest element of the heap is always at the root, after the
initial heap is constructed, the root value is swapped with the lowest, rightmost
element. This node corresponds to the last element of the array and so after the
swapping the righmost element of the array has the largest value, which is what
we want it to have. Consequently, the rightmost element is correctly place, and so
the corresponding node becomes a light gray in the tree display depicting the
heap. Also, the elements (nodes) chosen for swapping at this point of the
algorithm are shown as yellow nodes.

Thereafter, since the value of the lowest, rightmost node has moved up to the
root, the heap property of the rest of the heap (minus the gray nodes) has been
disturbed. Consequently, now the Heapify process is applied to the rest of the
heap (treating the light gray nodes as though they were no longer a part of the
heap). Once the rest of the heap is again converted to a proper heap, the
swapping process as described in the previous paragraph, followed by the Heapify
process is applied again. This continues until the root node of the heap turns light
gray - i.e., the root has its correct value. Then, the array is sorted.
IMPLEMENTATION:

#include<iostream>
using namespace std;

void display(int *array, int size) {


for(int i = 1; i<=size; i++)
cout << array[i] << " ";
cout << endl;
}

void heapify(int *array, int n) {


int i, par, l, r, node;
// create max heap

for(i = 1; i<= n; i++) {


node = i; par = (int)node/2;
while(par >= 1) {
//if new node bigger than parent, then swap
if(array[par] < array[node])
swap(array[par], array[node]);
node = par;
par = (int)node/2;//update parent to check
}
}
}

void heapSort(int *array, int n) {


int i;

for(i = n; i>= 1; i--) {


heapify(array, i);//heapify each time
swap(array[1], array[i]);//swap last element with first
}
}

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n+1]; //effective index starts from i = 1.
cout << "Enter elements:" << endl;

for(int i = 1; i<=n; i++) {


cin >> arr[i];
}

cout << "Array before Sorting: ";


display(arr, n);
heapSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}
OUTPUT:

TIME COMPLEXITY:

At each step of the Heapify algorithm, a node is compared to its children and
one of them is chosen as the next root. One level of the tree is dropped at
each step of this process. Since the heap is a complete binary tree, there are
atmost log(N) levels. Thus, the worst case time complexity of Heapify is
O(log(N)). In the Heapsort algorithm, Heapify is used (N/2) times to
construct the initial heap and (N-1) times to sort. Thus the Heapsort
algorithm has a worst case time complexity of O(N*log(N)).
CONCLUSION:

To every type of sorting or searching algorithm, advantages and


disadvantages are always there. With Heap Sorting algorithms, there are
very few disadvantages. There is no additional requirement of memory
space.

The other factor is time. It is found that the time complexity is calculated as
nlog(n), but the actual Heap Sort is lesser than O(nlog(n)). The reason is
that reduction or extraction from the Heap Sort reduces the size and takes
much less time as the process goes on.

Hence, Heap Sorting is considered one of the ”best” sorting algorithms


because of various reasons in the world of Data Structure.

REFERENCE:

https://www.google.com/amp/s/www.geeksforgeeks.org/heap-sort/amp/
https://en.m.wikipedia.org/wiki/Heapsort
https://brilliant.org/wiki/heap-sort/

You might also like