0% found this document useful (0 votes)
8 views5 pages

Ada 1.2

GTU Sem - 5 ckpcet ADA practicals

Uploaded by

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

Ada 1.2

GTU Sem - 5 ckpcet ADA practicals

Uploaded by

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

CKPCET(IT) ANALYSIS AND DESIGN OF ALGORITHMS (3150703)

Practical: 1.2

❖ Implementation and Time analysis of Selection sort


➢ Iterative method:
● CODE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
double measureSortingTime(int arr[], int n) {
clock_t start = clock();
selectionSort(arr, n);
clock_t end = clock();
return ((double) (end - start)) / CLOCKS_PER_SEC;
}

int main() {
int n = 10000;
int *arr = (int*)malloc(n * sizeof(int));

// Best Case: Already sorted array


printf("Best Case:\n");
for (int i = 0; i < n; i++) {
arr[i] = i;
}
double best_case_time = measureSortingTime(arr, n);
printf("Time taken: %f seconds\n\n", best_case_time);

// Worst Case: Reverse sorted array


printf("Worst Case:\n");
for (int i = 0; i < n; i++) {
arr[i] = n - i - 1;
}
double worst_case_time = measureSortingTime(arr, n);
printf("Time taken: %f seconds\n\n", worst_case_time);

// Average Case: Random array, averaged over 10 runs


printf("Average Case:\n");
1
CKPCET(IT) ANALYSIS AND DESIGN OF ALGORITHMS (3150703)

double total_time = 0;
srand(time(NULL));
for (int j = 0; j < 10; j++) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 10000;
}
total_time += measureSortingTime(arr, n);
}
double average_case_time = total_time / 10;
printf("Average time taken: %f seconds\n\n", average_case_time);
free(arr);
return 0;
}

1) Best Case:
● OUTPUT:

2) Average Case:
● OUTPUT:

3) Worst Case:
● OUTPUT:

➢ Recursive method:

● CODE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

void selectionSortRecursive(int arr[], int n, int index) {


if (index == n - 1)
return;

2
CKPCET(IT) ANALYSIS AND DESIGN OF ALGORITHMS (3150703)

int min_idx = index;


for (int j = index + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

swap(&arr[min_idx], &arr[index]);
selectionSortRecursive(arr, n, index + 1);
}

double measureSortingTime(int arr[], int n) {


clock_t start = clock();
selectionSortRecursive(arr, n, 0);
clock_t end = clock();
return ((double) (end - start)) / CLOCKS_PER_SEC;
}

int main() {
int n = 10000;
int *arr = (int*)malloc(n * sizeof(int));

// Best Case: Already sorted array


printf("Best Case:\n");
for (int i = 0; i < n; i++) {
arr[i] = i;
}
double best_case_time = measureSortingTime(arr, n);
printf("Time taken: %f seconds\n\n", best_case_time);

// Worst Case: Reverse sorted array


printf("Worst Case:\n");
for (int i = 0; i < n; i++) {
arr[i] = n - i - 1;
}
double worst_case_time = measureSortingTime(arr, n);
printf("Time taken: %f seconds\n\n", worst_case_time);

// Average Case: Random array, averaged over 10 runs


printf("Average Case:\n");
double total_time = 0;
srand(time(NULL));
for (int j = 0; j < 10; j++) {
for (int i = 0; i < n; i++) {
arr[i] = rand() % 10000;
}
total_time += measureSortingTime(arr, n);
}
double average_case_time = total_time / 10;
printf("Average time taken: %f seconds\n\n", average_case_time);

free(arr);
return 0;
}

3
CKPCET(IT) ANALYSIS AND DESIGN OF ALGORITHMS (3150703)

1) Best Case:
● OUTPUT:

2) Average Case:
● OUTPUT:

3) Worst Case:
● OUTPUT:

▪ Comparison Table:
CASE Iterative Recursive
Best Case 0.051 Seconds 0.051 Seconds
Worst Case 0.048 Seconds 0.04 Seconds
Average Case 0.056 Seconds 0.056 Seconds

4
CKPCET(IT) ANALYSIS AND DESIGN OF ALGORITHMS (3150703)

You might also like