You are on page 1of 24

Dr.

Jivraj Mehta Institute of Technology


Mogar, Anand

CERTIFICATE

This is to certify that MR.DEVANSHU.R.THAKKAR of


Semester 5TH Enrollment Number 180820107055 of
Computer Engineering Department has satisfactorily
completed his/her Lab Work in ADA for the term ending in
2020-21 in the Subject of ANALYSIS AND DESIGN OF
ALGORITHM.

Date: _____________

Signature of Teacher Head of the Department

Page | 1
INDEX
Sr Page Date of Date of
Name of Experiment Sign
No. No. Experiment Submission
1 IMPLEMENTATION OF LINEAR 3 26-6-20 3-7-20
SEARCH USING C PROGRAMMING

2 IMPLEMENTATION OF BINARY 4 3-7-20 10-7-20


SEARCH USING C PROGRAMMING

3 IMPLEMENTATION OF PRIM'S 6 10-7-20 17-7-20


ALGORITHM USING C
PROGRAMMING

4 IMPLEMENTATION OF KRUSKAL'S 9 17-7-20 7-8-20


ALGORITHM USING C
PROGRAMMING

5 IMPLEMENTATION OF BUBBLE 11 7-8-20 14-8-20


SORT USING C PROGRAMMING

6 IMPLEMENTATION OF SELECTION 12 14-8-20 21-8-20


SORT USING C PROGRAMMING

7 IMPLEMENTATION OF INSERTION 13 21-8-20 4-9-20


SORT USING C PROGRAMMING

8 IMPLEMENTATION OF MERGE 14 4-9-20 11-9-20


SORT USING C PROGRAMMING

9 IMPLEMENTATION OF QUICK 16 11-9-20 18-9-20


SORT USING C PROGRAMMING

10 IMPLEMENTATION AND TIME 17 18-9-20 25-9-20


ANALYSIS OF FRACTORIAL
PROGRAM USING ITERATIVE AND
RECURSIVE METHOD.

PRACTICAL:1
Page | 2
IMPLEMENTATION OF LINEAR SEARCH USING C
PROGRAMMING

#include <stdio.h>

int main()
{
  int array[100], search, c, n;

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


  scanf("%d", &n);

  printf("Enter %d integer(s)\n", n);

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


    scanf("%d", &array[c]);

  printf("Enter a number to search\n");


  scanf("%d", &search);

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


 {
    if (array[c] == search)    /* If required element is found */
  {
      printf("%d is present at location %d.\n", search, c+1);
      break;
  }
 }
  if (c == n)
    printf("%d isn't present in the array.\n", search);

  return 0;
}

OUTPUT:
Enter number of elements in array
Page | 3
5
Enter 5 integer(s)
25
32
5
1
8

Enter a number to search:8


8 is present at location 5.

PRACTICAL:2
Page | 4
IMPLEMENTATION OF BINARY SEARCH USING C
PROGRAMMING
#include <stdio.h>

int main()
{
  int c, first, last, middle, n, search, array[100];

  printf("Enter number of elements\n");


  scanf("%d", &n);

  printf("Enter %d integers\n", n);

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


    scanf("%d", &array[c]);

  printf("Enter value to find\n");


  scanf("%d", &search);

  first = 0;
  last = n - 1;
  middle = (first+last)/2;

  while (first <= last) {


    if (array[middle] < search)
      first = middle + 1;
    else if (array[middle] == search) {
      printf("%d found at location %d.\n", search, middle+1);
      break;
  }
    else
      last = middle - 1;

    middle = (first + last)/2;


 }
  if (first > last)
    printf("Not found! %d isn't present in the list.\n", search);

  return 0;
}
Page | 5
OUTPUT:
Enter number of elements in array
5
Enter 5 integer(s)
25
32
5
1
8
Enter a value to find
8
8 is found at location 5.

PRACTICAL NO:3
IMPLEMENTATION OF PRIM’S ALGORITHM USING C
LANGUAGE
#include<stdio.h>
Page | 6
#include<stdlib.h>
 
#define infinity 9999
#define MAX 20
 
int G[MAX][MAX],spanning[MAX][MAX],n;
 
int prims();
 
int main()
{
int i,j,total_cost;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
total_cost=prims();
printf("\nspanning tree matrix:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",spanning[i][j]);
}
printf("\n\nTotal cost of spanning tree=%d",total_cost);
return 0;
}
 
int prims()
{
int cost[MAX][MAX];
int u,v,min_distance,distance[MAX],from[MAX];
int visited[MAX],no_of_edges,i,min_cost,j;
//create cost[][] matrix,spanning[][]
for(i=0;i<n;i++)

Page | 7
for(j=0;j<n;j++)
{
if(G[i][j]==0)
cost[i][j]=infinity;
else
cost[i][j]=G[i][j];
spanning[i][j]=0;
}
//initialise visited[],distance[] and from[]
distance[0]=0;
visited[0]=1;
for(i=1;i<n;i++)
{
distance[i]=cost[0][i];
from[i]=0;
visited[i]=0;
}
min_cost=0; //cost of spanning tree
no_of_edges=n-1; //no. of edges to be added
while(no_of_edges>0)
{
//find the vertex at minimum distance from the tree
min_distance=infinity;
for(i=1;i<n;i++)
if(visited[i]==0&&distance[i]<min_distance)
{
v=i;
min_distance=distance[i];
}
u=from[v];
//insert the edge in spanning tree
spanning[u][v]=distance[v];
spanning[v][u]=distance[v];
no_of_edges--;
visited[v]=1;
//updated the distance[] array
for(i=1;i<n;i++)

Page | 8
if(visited[i]==0&&cost[i][v]<distance[i])
{
distance[i]=cost[i][v];
from[i]=v;
}
min_cost=min_cost+cost[u][v];
}
return(min_cost);}

OUTPUT:
Enter no. of vertices:6

Enter the adjacency matrix:


031600
305030
150564
605002
036006
004260

spanning tree matrix:

031000
300030
100004
000002
030000
004200

Total cost of spanning tree=13

PRATICAL NO:4
IMPLEMENTATION OF KRUSKAL’S ALGORITHM USING C
PROGRAMMING.
Page | 9
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
const int MAX = 1e4 + 5;
int id[MAX], nodes, edges;
pair <long long, pair<int, int> > p[MAX];

void init()
{
for(int i = 0;i < MAX;++i)
id[i] = i;
}

int root(int x)
{
while(id[x] != x)
{
id[x] = id[id[x]];
x = id[x];
}
return x;
}

void union1(int x, int y)


{
int p = root(x);
int q = root(y);
id[p] = id[q];
}

long long kruskal(pair<long long, pair<int, int> > p[])


{
int x, y;
long long cost, minimumCost = 0;
for(int i = 0;i < edges;++i)
{
x = p[i].second.first;
Page | 10
y = p[i].second.second;
cost = p[i].first;
if(root(x) != root(y))
{
minimumCost += cost;
union1(x, y);
}
}
return minimumCost;
}

int main()
{
int x, y;
long long weight, cost, minimumCost;
init();
cout <<"Enter Nodes and edges";
cin >> nodes >> edges;
for(int i = 0;i < edges;++i)
{
cout<<"Enter the value of X, Y and edges";
cin >> x >> y >> weight;
p[i] = make_pair(weight, make_pair(x, y));
}
sort(p, p + edges);
minimumCost = kruskal(p);
cout <<"Minimum cost is "<< minimumCost << endl;
return 0;}

OUTPUT:
Enter Nodes and edges5
5
Enter the value of X, Y and edges5
4
Page | 11
3
Enter the value of X, Y and edges2
3
1
Enter the value of X, Y and edges1
2
3
Enter the value of X, Y and edges5
4
3
Enter the value of X, Y and edges23
3
4
Minimum cost is 11

Page | 12
PRACTICAL:5
IMPLEMENTATION OF BUBBLE SORT USING C PROGRAMMING
#include<stdio.h>

int main(){

int count, temp, i, j, number[30];

printf("How many numbers are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d numbers: ",count);

for(i=0;i<count;i++)
scanf("%d",&number[i]);

/* This is the main logic of bubble sort algorithm


*/
for(i=count-2;i>=0;i--){
for(j=0;j<=i;j++){
if(number[j]>number[j+1]){
temp=number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}

printf("Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
Page | 13
}

OUTPUT:
HOW MANY NUMBERS ARE YOU GOING TO ENTER: 6.

ENTER 6 NUMBERS: 66 0 2 13 56 34

SORTED ELEMENTS: 0 2 13 34 56 66

Page | 14
PRACTICAL NO:6
IMPLEMENTATION OF SELECTION SORT USING C
PROGRAMMING
#include<stdio.h>
int main(){

int i, j, count, temp, number[25];

printf("How many numbers u are going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


// Loop to get the elements stored in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);

for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(number[i]>number[j]){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}

printf("Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

Page | 15
return 0;

OUTPUT:

HOW MANY NUMBERS ARE U GOING TO ENTER: 6

ENTER 6 ELEMENTS: 0 45 66 36 22 9

SORTED ELEMENTS: 0 9 22 36 45 66

PRACTICAL NO:7
Page | 16
IMPLEMENTATION OF INSERTION SORT USING C PROGRAMMING
#include<stdio.h>
int main(){
int i, j, count, temp, number[25];

printf("How many numbers u are going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


// This loop would store the input numbers in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);

for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}

Page | 17
OUTPUT:
HOW MANY NUMBERS ARE U GOING TO ENTER: 6

ENTER 6 ELEMENTS: 0 45 66 36 22 9

SORTED ELEMENTS: 0 9 22 36 45 66

Page | 18
PRACTICAL:8
IMPLEMENTATION OF MERGE SORT USING C LANGUAGE
#include <stdio.h>

#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];

void merging(int low, int mid, int high) {


int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];

while(l2 <= high)


b[i++] = a[l2++];

for(i = low; i <= high; i++)


a[i] = b[i];
}

void sort(int low, int high) {


int mid;

if(low < high) {


mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
Page | 19
return;
}
}

int main() {
int i;

printf("List before sorting\n");

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


printf("%d ", a[i]);

sort(0, max);

printf("\nList after sorting\n");

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


printf("%d ", a[i]);
}

OUTPUT:
List before sorting
10 14 19 26 27 31 33 35 42 44 0

List after sorting


0 10 14 19 26 27 31 33 35 42 44

Page | 20
PRACTICAL NO:9
IMPLEMENTATION OF QUICK SORT USING C LANGUAGE
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}

temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main(){
int i, count, number[25];

printf("How many elements are u going to enter?: ");


scanf("%d",&count);
Page | 21
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
OUTPUT:
HOW MANY NUMBERS ARE U GOING TO ENTER: 6

ENTER 6 ELEMENTS: 0 45 66 36 22 -9

SORTED ELEMENTS: -9 0 22 36 45 66

Page | 22
PRACTIAL: 10
IMPLEMENTATION AND TIME ANALYSIS OF FACTORIAL
PROGRAM USING ITERATIVE AND RECURSIVE METHOD.

ITERATIVE METHOD:
 
#include<stdio.h>
long int fact(int n);
long int Ifact(int n);
 
int main( )
{
        int num;
        printf("Enter a number : ");
        scanf("%d", &num);

 printf("\nUsing Iterative :: \n");


 
        if(num<0)
                printf("No factorial for negative number\n");
        else
                printf("Factorial of %d is %ld\n", num, Ifact(num) );
 
                return 0;

long int Ifact(int n)


{
        long fact=1;
        while(n>0)
        {
                fact = fact*n;
                n--;
        }
        return fact;

Page | 23
RECURSIVE METHOD:
#include<stdio.h>
long int fact(int n);
long int Ifact(int n);
 
int main( )
{
        int num;
        printf("Enter a number : ");
        scanf("%d", &num);

 printf("\n Using RECURSIVE :: \n");


 
        if(num<0)
                printf("No factorial for negative number\n");
        else
                printf("Factorial of %d is %ld\n", num, Ifact(num) );
 
                return 0;

/*Recursive*/
long int fact(int n)
{
        if(n == 0)
                return(1);
        return(n * fact(n-1));

________________________________________________
OUTPUT:

ENTER A POSITIVE INTEGER :6

FACTORIAL OF 6 = 720

Page | 24

You might also like