0% found this document useful (0 votes)
13 views26 pages

Ada 09

Uploaded by

lashkariram99
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)
13 views26 pages

Ada 09

Uploaded by

lashkariram99
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/ 26

PACIFIC SCHOOL OF ENGINEERING

DEPARTMENT OF COMPUTER ENGINEERING

INDEX

Student Name:
Enrollment Number:
Subject Name (Code): Analysis and Design of Algorithms (3150711)

Sr. Page Date Grade Sign


AIM No.
No.
Implementation and Time analysis of sorting
1. algorithms. 2-5
Bubble sort, Selection sort, & Insertion sort
Implementation and Time analysis of
2. factorial program using iterative and 6-7
recursive method
Implementation and Time analysis of Linear
3. 8-9
search algorithm.
Implementation and Time analysis of Binary
4. 10-11
search algorithm.
Implementation and Time analysis of Merge
5. 12-15
sort and Quick sort algorithms.
Implementation of a knapsack problem using
6. 16
dynamic programming.
Implementation of making a change problem
7. 17-18
using dynamic programming
Implementation of a knapsack problem using
8. 19-20
greedy algorithm
9. Implementation of max-heap sort algorithm 21-22
10. Implement LCS problem. 23-24
Analysis & Design of Algorithms Enrollment No.: 221120107009

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.

 Implementation of Bubble Sort:

#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;

for (i = 0; i < size; i++) {


a[i] = rand() % 100;
}

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();

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

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.

 Implementation of Selection Sort:

#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;

for (i = 0; i < size; i++)


a[i] = rand() % 100;

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.

 Implementation of Insertion Sort:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 900

int main() {
int i, j, a[size], p;
clock_t start, end;
double cpu;

for (i = 0; i < size; i++)


a[i] = rand() % 100;

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.

 Implementation of Factorial Program :

#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;

printf("\nEnter a number to find factorial: ");


scanf("%d", &n);

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

printf("\nFactorial of %d is %lf", n, a);


printf("\n");
}

 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.

 Implementation of Linear Search:

#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;

printf("Enter element to search: ");


scanf("%d", &e);
printf("\n");

for (i = 0; i < 500; i++) {


a[i] = rand() % 100;
}

for (i = 0; i < 500; i++) {


printf("%d ", a[i]);
}
printf("\n\nThe found elements are:\n");

start = clock();

for (i = 0; i < 500; i++) {


if (a[i] == e) {
printf("%d ", a[i]);
c++;
}
}
end = clock();

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

printf("\n\nThe number of elements found are: %d", c);


printf("\n\nThe time taken is: %lf seconds\n\n", cpu);

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.

 Implementation of Binary Search:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 200

int search(int arr[], int key, int low, int high);

int main()
{
clock_t start, end;
double cpu_time;
int arr[size], i, j, temp, key, result;

for (i = 0; i < size; i++) {


arr[i] = rand() % 100;
}

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


for (j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

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);
}

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


printf("Time = %lf seconds\n", cpu_time);

return 0;
}

int search(int arr[], int key, int low, int high)


{
if (low > high) {
return -1;
}

int mid = (low + high) / 2;


if (arr[mid] == key) {
return mid;
} else if (arr[mid] > key) {
return search(arr, key, low, mid - 1);
} else {
return search(arr, key, mid + 1, high);
}
}
 Output:

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..

 Implementation of Merge Sort:


#include <stdio.h>
#include <time.h>
#define size 500

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
12 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009

void mergeSort(int arr[], int l, int r)


{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

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.

 Implementation of Quick Sort:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 100

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;
}

void quick(int a[], int le, int ri)


{
int q;
if (le < ri) {
q = partition(a, le, ri);
quick(a, le, q - 1);
quick(a, q + 1, ri);
}
}
14 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009

int partition(int a[], int l, int r)


{
int pivot = a[l];
int lo = l + 1;
int hi = r;
while (hi >= lo) {
while (a[hi] > pivot)
hi -= 1;
while (a[lo] <= pivot && lo <= hi)
lo += 1;
if (hi >= lo) {
int k = a[lo];
a[lo] = a[hi];
a[hi] = k;
lo += 1;
hi -= 1;
}
}
int p = a[l];
a[l] = a[hi];
a[hi] = p;
return hi;
}

 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.

 Implementation of Knapsack problem :

#include <stdio.h>

#define MAX 100

int knapsack(int W, int weights[], int values[], int n)


{
int dp[MAX][MAX] = {0};

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


for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else if (weights[i - 1] <= w) {
dp[i][w] = (values[i - 1] + dp[i - 1][w - weights[i - 1]] > dp[i - 1][w]) ?
values[i - 1] + dp[i - 1][w - weights[i - 1]] : dp[i - 1][w];
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}

int main()
{
int values[] = {60, 100, 120};
int weights[] = {10, 20, 30};
int W = 50;
int n = sizeof(values) / sizeof(values[0]);

int max_value = knapsack(W, weights, values, n);


printf("Maximum value in Knapsack = %d\n", max_value);

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.

Making Change Problem


 The Making Change Problem aims to determine the minimum number of coins needed to make a
given amount of money using available denominations. This problem can be solved efficiently 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]);

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


for (j = 0; j <= m; j++)
c[i][j] = 0;
}
for (i = 0; i < n + 1; i++)
c[i][0] = 0;
for (i = 0; i < m + 1; i++) {
c[0][i] = i;
}

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


for (j = 1; j < m + 1; j++) {
if (dc[1] > c[0][j])
c[i][j] = 0;
else if (dc[i] <= j) {
if ((c[i][j - dc[i]] + 1) < c[i - 1][j])
c[i][j] = (c[i][j - dc[i]] + 1);
else
c[i][j] = c[i - 1][j];
} else
c[i][j] = c[i - 1][j];
}

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");
}

printf("\nThe change you get is \n");


i = n;
j = m;
while (i != 1) {
if (c[i][j] == c[i - 1][j])
i = i - 1;
else {
j = j - dc[i];
printf(" %d ", dc[i]);
}
}
printf("\n\n");
return 0;
}

 Output:

18 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009

PRACTICAL 8

AIM: Implementation of a knapsack problem using greedy algorithm

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;

for (i = 0; i < n; i++)


x[i] = 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]);
}

for (i = 0; i < n; i++)


cal[i] = p[i] / w[i];

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


for (j = i + 1; j < n; j++) {
if (cal[i] < cal[j]) {
19 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009

float t1, t2, t3;


t1 = cal[i]; cal[i] = cal[j]; cal[j] = t1;
t2 = w[i]; w[i] = w[j]; w[j] = t2;
t3 = p[i]; p[i] = p[j]; p[j] = t3;
}
}
}

printf("\n\n p[i]\t\t w[i]\t\t cal[i]\n");


for (i = 0; i < n; i++)
printf("%f\t %f\t %f\t\n", p[i], w[i], cal[i]);

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


if ((wt + w[i]) <= m) {
k++; x[i] = 1; wt += w[i]; prof += p[i];
} else {
k++; x[i] = (m - wt) / w[i]; w[i] = m - wt; wt = m; prof += (x[i] * p[i]); p[i] = (x[i] * p[i]);
break;
}
}
printf("\nThe selected weights are \n\ni\t w[i]\t\t p[i]\n");
for (i = 0; i < k; i++)
printf("%d\t%f\t%f\n", i + 1, w[i], p[i]);

printf("\n\nThe total profit is %f\n\n", prof);


return 0;
}

 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..

 Implementation of Max-Heap Sort:

#include <stdio.h>
#include <stdlib.h>
#define MAX 20

void maxheapify(int *, int, int);


int* buildmaxheap(int *, int);

int main() {
int i, t, n;
int *a = calloc(MAX, sizeof(int));
int *m = calloc(MAX, sizeof(int));

printf("Enter no of elements in the array\n");


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

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;
}

int* buildmaxheap(int a[], int n) {


int heapsize = n;
for (int j = n / 2; j >= 0; j--) {
maxheapify(a, j, heapsize);
}
return a;
}
22 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009

void maxheapify(int a[], int i, int heapsize) {


int temp, largest, left, right;
left = (2 * i + 1);
right = (2 * i + 2);

if (left >= heapsize)


return;

if (a[left] > a[i])


largest = left;
else
largest = i;

if (right < heapsize && a[right] > a[largest])


largest = right;

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

AIM: Implement LCS problem.


Longest Common Subsequence

 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>

void lcs(int arr[][10], char ar[], int, int);


int l, k;
void main()
{
char s1[20], s2[20];
int i, j;
printf("\nEnter the strings\nX = ");
s1[0] = 0;
char ch = getchar();
while (ch != '\n') {
s1[++l] = ch;
ch = getchar();
}
printf("Y = ");
s2[0] = 0;
char c1 = getchar();
while (c1 != '\n') {
s2[++k] = c1;
c1 = getchar();
}

int c[l + 1][k + 1], b[l + 1][k + 1];


for (i = 0; i < l + 1; i++) {
for (j = 0; j < k + 1; j++) {
b[i][j] = 0;
c[i][j] = 0;
}
}
for (i = 1; i < l + 1; i++) {
for (j = 1; j < k + 1; j++) {
if (s1[i] == s2[j]) {
c[i][j] = c[i - 1][j - 1] + 1;
b[i][j] = 1;
} else if (c[i - 1][j] >= c[i][j - 1]) {
c[i][j] = c[i - 1][j];
24 | P a g e
Analysis & Design of Algorithms Enrollment No.: 221120107009

b[i][j] = 2;
} else {

c[i][j] = c[i][j - 1];


b[i][j] = 3;
}
}
}

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

You might also like