Professional Documents
Culture Documents
CERTIFICATE
Date: _____________
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
PRACTICAL:1
Page | 2
IMPLEMENTATION OF LINEAR SEARCH USING C
PROGRAMMING
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
OUTPUT:
Enter number of elements in array
Page | 3
5
Enter 5 integer(s)
25
32
5
1
8
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];
first = 0;
last = n - 1;
middle = (first+last)/2;
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
031000
300030
100004
000002
030000
004200
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;
}
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(){
for(i=0;i<count;i++)
scanf("%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(){
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;
}
}
}
Page | 15
return 0;
OUTPUT:
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];
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;
}
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];
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++];
}
int main() {
int i;
sort(0, max);
OUTPUT:
List before sorting
10 14 19 26 27 31 33 35 42 44 0
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];
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);
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);
/*Recursive*/
long int fact(int n)
{
if(n == 0)
return(1);
return(n * fact(n-1));
________________________________________________
OUTPUT:
FACTORIAL OF 6 = 720
Page | 24