0% found this document useful (0 votes)
22 views6 pages

DAA - Heap Sort (Anurag Verma) v1.0

The document outlines an experiment to implement Heap Sort and analyze its time complexity. It details the algorithm steps, provides C code for the implementation, and presents the output of execution times for varying input sizes. The conclusion summarizes the time complexity of Heap Sort as O(n log n) for best, average, and worst cases.

Uploaded by

Anurag Soni
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)
22 views6 pages

DAA - Heap Sort (Anurag Verma) v1.0

The document outlines an experiment to implement Heap Sort and analyze its time complexity. It details the algorithm steps, provides C code for the implementation, and presents the output of execution times for varying input sizes. The conclusion summarizes the time complexity of Heap Sort as O(n log n) for best, average, and worst cases.

Uploaded by

Anurag Soni
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/ 6

Anurag Verma EN22CS301175

Experiment 4
Aim
To implement Heap Sort & determine its time complexity.

Theory

Heap Sort Algorithm


1.​ Build a Max Heap:​
Convert the unsorted array into a Max Heap (a complete binary tree
where the parent node is always greater than or equal to its children).
○​ This can be done using the heapify function, which ensures the
heap property is maintained.
2.​ Extract the Maximum Element:
○​ The largest element (root of the Max Heap) is swapped with the
last element of the heap.
○​ The size of the heap is reduced (excluding the last element,
which is now sorted).
3.​ Heapify the Root:
○​ Since swapping may break the heap property, heapify is called
on the root to restore the Max Heap.
4.​ Repeat Steps 2-3:
○​ Continue extracting the maximum element and heapifying the
reduced heap until only one element remains.

Code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define INPUTS 20

36
Anurag Verma EN22CS301175

// Function to generate random numbers


void generateRandomNumbers(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] = rand() % 10000; // Random numbers between 0 and 9999
}
}

void heapify(int arr[], int n, int i) {


// Initialize largest as root
int largest = i;

// left index = 2*i + 1


int l = 2 * i + 1;

// right index = 2*i + 2


int r = 2 * i + 2;

// If left child is larger than root


if (l < n && arr[l] > arr[largest]) {
largest = l;
}

// If right child is larger than largest so far


if (r < n && arr[r] > arr[largest]) {
largest = r;
}

// If largest is not root


if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;

// Recursively heapify the affected sub-tree

37
Anurag Verma EN22CS301175

heapify(arr, n, largest);
}
}

// Main function to do heap sort


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

// One by one extract an element from heap


for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

// Call max heapify on the reduced heap


heapify(arr, i, 0);
}
}

int checkProgramWithSize(int size) {


int arr[size];
clock_t start, end;
double cpu_time_used;

// Seed for random number generation


srand(time(NULL));

// Generate random numbers


generateRandomNumbers(arr, size);

// Record start time

38
Anurag Verma EN22CS301175

start = clock();

heapSort(arr, size);

// Record end time


end = clock();

// Calculate elapsed time


cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;

printf("%d, %f\n", size, cpu_time_used);

return 0;
}

int main() {
printf("No of Inputs Heap Sort, time (sec)\n");
for (int i = 1; i < INPUTS + 1; i++) {
checkProgramWithSize(i * 10000);
}
}

39
Anurag Verma EN22CS301175

Output

Output data:
No of Inputs Heap Sort time (sec)
10000 0.001219
20000 0.002714
30000 0.004254
40000 0.006263
50000 0.007122
60000 0.008839
70000 0.010672
80000 0.013499
90000 0.013536
100000 0.015581
110000 0.017152
120000 0.020666
130000 0.021925
140000 0.023534
150000 0.025767
160000 0.027184
170000 0.028418
180000 0.030151
190000 0.032939
200000 0.034115

40
Anurag Verma EN22CS301175

Conclusion
Time Complexity
Quick Sort
●​ Best Case: (Ω(n log n))
●​ Average Case: (θ(n log n))
●​ Worst Case: (O(n log n))

41

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy