0% found this document useful (0 votes)
11 views

Daa2 1

Daa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Daa2 1

Daa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

HEAP SORT

#include <stdio.h>

// Function to swap two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// To heapify a subtree rooted with node i


void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child

// If left child is larger than root


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

// If right child is larger than largest so far


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

// If largest is not root


if (largest != i) {
swap(&arr[i], &arr[largest]);

// Recursively heapify the affected sub-tree


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
swap(&arr[0], &arr[i]);

// Call heapify on the reduced heap


heapify(arr, i, 0);
}
}

// A function to print array of size n


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program
int main() {
int n;

// Get the number of elements


printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];

// Get the elements


printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Perform heap sort


heapSort(arr, n);

// Print the sorted array


printf("Sorted array is:\n");
printArray(arr, n);

return 0;
}

Enter number of elements: 5


Enter the elements:
21
58
11
44
62
Sorted array is:
11 21 44 58 62

0r
#include <stdio.h>

// Heapify function
void heapify(int arr[], int n, int i) {
int temp, maximum, left_index, right_index;

// currently assuming i position to


// be holding the largest value
maximum = i;

// right child index


right_index = 2 * i + 2;

// left child index


left_index = 2 * i + 1;

// if left index value is greater than the current index


// value
if (left_index < n && arr[left_index] > arr[maximum])
maximum = left_index;

// if right index value is greater than the current index


// value
if (right_index < n && arr[right_index] > arr[maximum])
maximum = right_index;

// checking if we needed to swap the elements or not


if (maximum != i) {
temp = arr[i];
arr[i] = arr[maximum];
arr[maximum] = temp;
heapify(arr, n, maximum);
}
}

// HeapSorting function
void heapsort(int arr[], int n) {
int i, temp;

// performing heapify on the non-leaf nodes so n/2 - 1


// to 0 are the non-leaf nodes
for (i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
// the current array is changed to max heap

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


temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}

// A function to print array of size n


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// Driver code
int main() {
int n;

// Get the number of elements


printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];

// Get the elements


printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Displaying original array


printf("Original Array: ");
printArray(arr, n);

// Sort the array using heap sort


heapsort(arr, n);

// Displaying sorted array


printf("Array after performing heap sort: ");
printArray(arr, n);

return 0;
}

Display the table showing the time complexity of


bubble, selection, insertion,
merge, quick, and heap sort methods for the values of
N from 10(+5) to 50.

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

// Swap utility
void swap(long int* a, long int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}

// Bubble sort
void bubbleSort(long int a[], long int n) {
for (long int i = 0; i < n - 1; i++) {
for (long int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
swap(&a[j], &a[j + 1]);
}
}
}
}

// Insertion sort
void insertionSort(long int arr[], long int n) {
long int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

// Move elements of arr[0..i-1], that are


// greater than key, to one position ahead
// of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// Selection sort
void selectionSort(long int arr[], long int n) {
long int i, j, midx;

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


// Find the minimum element in unsorted array
midx = i;

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


if (arr[j] < arr[midx])
midx = j;

// Swap the found minimum element


// with the first element
swap(&arr[midx], &arr[i]);
}
}

// Function to print array


void printArray(long int arr[], long int size) {
for (long int i = 0; i < size; i++) {
printf("%ld ", arr[i]);
}
printf("\n");
}

// Function to copy array


void copyArray(long int src[], long int dest[], long int size) {
for (long int i = 0; i < size; i++) {
dest[i] = src[i];
}
}

// Driver code
int main() {
long int n;
srand(time(0)); // Seed for random number generation

// Arrays to store time duration


// of sorting algorithms
double bubbleTime, insertionTime, selectionTime;

printf(" N | Bubble Sort | Insertion Sort | Selection Sort\n");


printf("----|-------------|----------------|----------------\n");

// Loop through array sizes from 10 to 50 in increments of 5


for (n = 10; n <= 50; n += 5) {
long int a[n], b[n], c[n];

// Generating n random numbers


for (long int i = 0; i < n; i++) {
long int no = rand() % n + 1;
a[i] = no;
}

// Copy the array into b and c


copyArray(a, b, n);
copyArray(a, c, n);

// Using clock_t to store time


clock_t start, end;

// Bubble sort
start = clock();
bubbleSort(a, n);
end = clock();
bubbleTime = ((double)(end - start)) / CLOCKS_PER_SEC * 1000; // Convert to milliseconds

// Insertion sort
start = clock();
insertionSort(b, n);
end = clock();
insertionTime = ((double)(end - start)) / CLOCKS_PER_SEC * 1000; // Convert to milliseconds

// Selection sort
start = clock();
selectionSort(c, n);
end = clock();
selectionTime = ((double)(end - start)) / CLOCKS_PER_SEC * 1000; // Convert to milliseconds

// Print the results


printf("%3ld | %11.2f ms | %14.2f ms | %14.2f ms\n",
n,
bubbleTime,
insertionTime,
selectionTime);
}

return 0;
}
N | Bubble Sort | Insertion Sort | Selection Sort
----|-------------|----------------|----------------
10 | 0.00 ms | 0.00 ms | 0.00 ms
15 | 0.00 ms | 0.00 ms | 0.00 ms
20 | 0.00 ms | 0.00 ms | 0.00 ms
25 | 0.00 ms | 0.00 ms | 0.00 ms
30 | 0.00 ms | 0.00 ms | 0.00 ms
35 | 0.01 ms | 0.00 ms | 0.00 ms
40 | 0.01 ms | 0.00 ms | 0.01 ms
45 | 0.01 ms | 0.00 ms | 0.00 ms
50 | 0.01 ms | 0.00 ms | 0.01 ms

Or
#include <stdio.h>
#include <math.h>

void displayTimeComplexity() {
int N;
printf(" N | Bubble Sort | Selection Sort | Insertion Sort | Merge Sort | Quick Sort (Avg) | Quick
Sort (Worst) | Heap Sort \n");
printf("---|-------------|----------------|----------------|------------|------------------|---------------------|------
------\n");

for (N = 10; N <= 50; N += 5) {


double bubbleSort = pow(N, 2);
double selectionSort = pow(N, 2);
double insertionSort = pow(N, 2);
double mergeSort = N * log2(N);
double quickSortAvg = N * log2(N);
double quickSortWorst = pow(N, 2);
double heapSort = N * log2(N);

printf("%2d | %11.1f | %14.1f | %14.1f | %10.1f | %16.1f | %19.1f | %10.1f\n",


N, bubbleSort, selectionSort, insertionSort, mergeSort, quickSortAvg, quickSortWorst,
heapSort);
}
}

int main() {
displayTimeComplexity();
return 0;
}
N | Bubble Sort | Selection Sort | Insertion Sort | Merge Sort | Quick Sort (Avg) | Quick Sort
(Worst) | Heap Sort
---|-------------|----------------|----------------|------------|------------------|---------------------|------------
10 | 100.0 | 100.0 | 100.0 | 33.2 | 33.2 | 100.0 | 33.2
15 | 225.0 | 225.0 | 225.0 | 58.1 | 58.1 | 225.0 | 58.1
20 | 400.0 | 400.0 | 400.0 | 86.4 | 86.4 | 400.0 | 86.4
25 | 625.0 | 625.0 | 625.0 | 116.2 | 116.2 | 625.0 | 116.2
30 | 900.0 | 900.0 | 900.0 | 148.8 | 148.8 | 900.0 | 148.8
35 | 1225.0 | 1225.0 | 1225.0 | 183.3 | 183.3 | 1225.0 | 183.3
40 | 1600.0 | 1600.0 | 1600.0 | 220.0 | 220.0 | 1600.0 | 220.0
45 | 2025.0 | 2025.0 | 2025.0 | 259.0 | 259.0 | 2025.0 | 259.0
50 | 2500.0 | 2500.0 | 2500.0 | 300.4 | 300.4 | 2500.0 | 300.4

```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

You might also like