Ada 09
Ada 09
INDEX
Student Name:
Enrollment Number:
Subject Name (Code): Analysis and Design of Algorithms (3150711)
PRACTICAL 1
AIM: Implementation and Time analysis of sorting algorithms.
Bubble sort, Selection sort & Insertion sort.
1) Bubble Sort
Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps
them if they are in the wrong order. This process is repeated until the list becomes sorted.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 900
int main()
{
int i, j, a[size], t;
clock_t start, end;
double cpu;
start = clock();
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (a[j + 1] < a[j]) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
end = clock();
printf("\nBUBBLE SORT\n");
printf("Sorted data:\n");
for (i = 0; i < size; i++) {
printf("%d ", a[i]);
}
printf("\n\n");
printf("Time taken by Bubble Sort: %lf seconds\n", cpu);
2|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
return 0;
}
Output:
2) Selection Sort
Selection Sort divides the array into a sorted and unsorted region. It repeatedly selects the
smallest element from the unsorted region and places it at the end of the sorted region.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 900
int main() {
int i, j, a[size], t, min;
clock_t start, end;
double cpu;
start = clock();
for (i = 0; i < size - 1; i++) {
min = i;
for (j = i + 1; j < size; j++) {
if (a[min] > a[j])
min = j;
}
if (min != i) {
t = a[i];
a[i] = a[min];
a[min] = t;
}
}
3|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
end = clock();
printf("\nSELECTION SORT\n");
cpu = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Time taken by Selection Sort: %lf seconds\n", cpu);
printf("\nSorted data\n");
for (i = 0; i < size; i++)
printf("%d ", a[i]);
return 0;
}
Output:
3) Insertion Sort
Insertion Sort builds the sorted list one element at a time by picking elements from the
unsorted list and inserting them at their correct position.
int main() {
int i, j, a[size], p;
clock_t start, end;
double cpu;
start = clock();
for (i = 1; i < size; i++) {
p = a[i];
j = i - 1;
while (j >= 0 && a[j] > p) {
a[j + 1] = a[j];
4|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
j = j - 1;
}
a[j + 1] = p;
}
end = clock();
printf("\nINSERTION SORT");
cpu = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("\nTime taken by insertion Sort %lf", cpu);
printf("\nSorted data\n");
for (i = 0; i < size; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
Output:
5|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 2
AIM: Implementation and Time analysis of factorial program using
iterative and recursive method
Factorial Program
The factorial of a non-negative integer n is the product of all positive integers less than or equal to
n. The factorial function is commonly denoted as n!. Factorial can be computed using both
iterative and recursive methods.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int fact(int x)
{
double f = x;
if (x != 1)
f *= fact(x - 1);
else
return 1;
return f;
}
void main()
{
int i, n;
double a, fa = 1;
clock_t end, start;
double cpu;
start = clock();
for (i = n; i > 0; i--) {
fa *= i;
}
end = clock();
cpu = ((double)(end - start) / CLOCKS_PER_SEC);
printf("\nTime for iterative is %lf", cpu);
printf("\nFactorial of %d is %lf\n", n, fa);
start = clock();
a = fact(n);
end = clock();
cpu = ((double)(end - start) / CLOCKS_PER_SEC);
printf("\nTime for recursive is %lf", cpu);
6|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
Output:
7|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 3
AIM: Implementation and Time analysis of linear search algorithm.
Linear Search
Linear Search scans each element of the array sequentially until the desired element is found
or the end of the array is reached.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 500
void main()
{
int e, a[500], i, c = 0;
clock_t end, start;
double cpu;
start = clock();
8|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
Output:
9|P a ge
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 4
AIM: Implementation and Time analysis of Binary search algorithm.
Binary Search
Binary Search works by repeatedly dividing the sorted array in half. It compares the middle
element with the target value and narrows the search to the left or right half based on the
comparison.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 200
int main()
{
clock_t start, end;
double cpu_time;
int arr[size], i, j, temp, key, result;
printf("Sorted Array:\n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n\nEnter number to search: ");
scanf("%d", &key);
start = clock();
result = search(arr, key, 0, size - 1);
10 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
end = clock();
if (result == -1) {
printf("Number not found\n");
} else {
printf("Number found at index %d\n", result);
}
return 0;
}
11 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 5
AIM: Implementation and Time analysis of Merge sort and Quick sort
algorithms.
1) Merge Sort
Merge Sort is a divide-and-conquer algorithm that splits the array into two halves,
recursively sorts them, and merges the two halves..
int main()
{
int arr[size], i;
clock_t start, end;
double cpu;
printf("\n\n");
for (i = 0; i < size; i++) {
arr[i] = rand() % 100;
printf("%d ", arr[i]);
}
start = clock();
mergeSort(arr, 0, size - 1);
end = clock();
cpu = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("\n");
printf("\nSorted array is \n\n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("\nTime taken by Merge Sort is : %lf\n", cpu);
return 0;
}
Output:
13 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
2) Quick Sort
Quick Sort is a divide-and-conquer algorithm that selects a pivot and partitions the array
such that elements less than the pivot are on the left and greater elements are on the right.
int a[size];
void quick(int a[], int le, int ri);
int partition(int a[], int l, int r);
int main()
{
int i;
clock_t start, end;
double cpu;
printf("\nData before sorting\n\n");
for (i = 0; i < size; i++) {
a[i] = rand() % 100;
}
for (i = 0; i < size; i++) {
printf("%d ", a[i]);
}
start = clock();
quick(a, 0, size - 1);
end = clock();
printf("\n\nData after sorting\n\n");
for (i = 0; i < size; i++) {
printf("%d ", a[i]);
}
cpu = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("\n\nTime taken is %lf\n", cpu);
printf("\n\n");
return 0;
}
Output:
15 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 6
AIM: Implementation of a knapsack problem using dynamic programming.
Knapsack Problem
The Knapsack Problem is a classic optimization problem where you have a knapsack with a weight
limit and a set of items, each with a weight and value. The goal is to determine the maximum value
of items that can be placed in the knapsack without exceeding the weight limit.
Also known as 0/1 knapsack.
#include <stdio.h>
int main()
{
int values[] = {60, 100, 120};
int weights[] = {10, 20, 30};
int W = 50;
int n = sizeof(values) / sizeof(values[0]);
return 0;
}
Output:
16 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 7
AIM: Implementation of making a change problem using dynamic
programming.
Implementation of Program :
#include <stdio.h>
#include <stdlib.h>
int main() {
int m, n, i, j;
printf("Enter the amount of which you want change\n");
scanf("%d", &m);
printf("\nEnter the number of determinants\n");
scanf("%d", &n);
int c[n + 1][m + 1], dc[n + 1];
printf("\nEnter the determinants\n");
dc[0] = 0;
for (i = 1; i < n + 1; i++)
scanf("%d", &dc[i]);
17 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
printf("\n");
for (i = 0; i < n + 1; i++) {
printf("%d ", dc[i]);
for (j = 0; j < m + 1; j++) {
printf(" %d", c[i][j]);
}
printf("\n");
}
Output:
18 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 8
Kanpsack Problem
The Greedy Algorithm for the Knapsack Problem aims to maximize the total value in the knapsack
by selecting items based on their value-to-weight ratio. Unlike the dynamic programming approach,
this method does not always guarantee an optimal solution for all cases, but it works well for certain
types of knapsack problem
Also known as Fractional knapsack.
Implementation of Program :
#include <stdio.h>
#include <stdlib.h>
int main() {
int m, n, i, j;
printf("Enter maximum weight of knapsack ");
scanf("%d", &m);
printf("\nEnter number of objects ");
scanf("%d", &n);
int wt = 0, k = 0;
float cal[n], p[n], w[n], x[n], prof = 0;
printf("\nEnter weights\n");
for (i = 0; i < n; i++) {
printf("w[%d] = ", i);
scanf("%f", &w[i]);
}
printf("\nEnter profits\n");
for (i = 0; i < n; i++) {
printf("p[%d] = ", i);
scanf("%f", &p[i]);
}
Output:
20 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
21 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 9
AIM: Implementation of max-heap sort algorithm
Max–Heap Sort
Heap Sort is a comparison-based sorting technique based on a Binary Heap data structure. A Max-
Heap is a complete binary tree where the value of each node is greater than or equal to its child
nodes. The Max-Heap Sort algorithm builds the heap, extracts the maximum element (root), and
places it at the end of the array, repeating the process until the array is sorted..
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int main() {
int i, t, n;
int *a = calloc(MAX, sizeof(int));
int *m = calloc(MAX, sizeof(int));
m = buildmaxheap(a, n);
printf("The heap is\n");
for (t = 0; t < n; t++) {
printf("%d\n", m[t]);
}
free(a);
free(m);
return 0;
}
if (largest != i) {
temp = a[i];
a[i] = a[largest];
a[largest] = temp;
maxheapify(a, largest, heapsize);
}
}
Output:
23 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
PRACTICAL 10
The Longest Common Subsequence (LCS) problem aims to find the longest subsequence that appears
in both sequences in the same order. It does not require the subsequence to be contiguous. This
problem can be efficiently solved using dynamic programming.
Implementation of Program :
#include <stdio.h>
b[i][j] = 2;
} else {
printf("\n\n\t");
for (i = 1; i < k + 1; i++) {
printf("\t %c", s2[i]);
}
printf("\n");
for (i = 0; i < l + 1; i++) {
printf("%c\t", s1[i]);
for (j = 0; j < k + 1; j++) {
printf("%d(%d)\t", c[i][j], b[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("Longest common substring of X and Y is ");
lcs(b, s1, l, k);
printf("\n\n");
}
void lcs(int arr[l + 1][k + 1], char ar[l + 1], int a, int p)
{
if (a == 0 || p == 0)
return;
if (arr[a][p] == 1) {
lcs(arr, ar, a - 1, p - 1);
printf("%c ", ar[a]);
} else if (arr[a][p] == 2) {
lcs(arr, ar, a - 1, p);
} else if (arr[a][p] == 3) {
lcs(arr, ar, a, p - 1);
}
}
Output:
25 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009
26 | P a g e