1) Linear Search & Binary Search
1) Linear Search & Binary Search
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
int linear(int[],int,int);
int binary(int[],int,int,int);
int i=0;
int mid;
void main()
{
int n,a[10],pos,i,ch,ele,low,high;
clock_t st,et;
clrscr();
printf("\nEnter number of array elements: ");
scanf("%d",&n);
printf("\nEnter the array in ascending order: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n1. Linear Search\n2. Binary Search\nEnter your choice: ");
scanf("%d",&ch);
printf("\nEnter element to be searched for: ");
scanf("%d",&ele);
low=0;
high=n;
switch(ch)
{
case 1: st=clock();
pos=linear(a,n,ele);
et=clock();
if(pos!=-1)
printf("\nElement found at position %d",pos);
else
printf("\nElement not found.");
printf("\nTime taken for execution of the algorithm
= %f ",(et-st)/CLK_TCK,"s");
break;
case 2: st=clock();
pos=binary(a,low,high,ele);
et=clock();
if(pos!=-1)
printf("\nElement found at posititon %d",pos);
else
printf("\nElement not found. ");
printf("\nTime taken for execution of the algorithm
= %f ",(et-st)/CLK_TCK,"s");
break;
default: printf("\nError. ");
}
getch();
}
int linear(int a[],int n,int key)
{
sleep(1);
if(i<=n)
{
if(key==a[i])
{
return (i+1);
}
else
{
i++;
return(linear(a,n,key));
}
}
else
return (-1);
}
1. Linear Search
2. Binary Search
Enter your choice: 1
1. Linear Search
2. Binary Search
Enter your choice: 2
void main()
{
float t;
clock_t et,st;
int n,a[100],i,j;
printf("enter array limit\n");
scanf("%d",&n);
printf("enter %d nos.\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
st=clock();
sort(a,n);
et=clock();
t=(et-st)/CLK_TCK;
printf("time:%f\n",t);
getch();
}
void sort(int a[20],int n)
{
int i,j,min,temp;
delay(1000);
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if(a[min]>a[j])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("sorted:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
OUTPUT:
#include<all.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,n;
clrscr();
printf("enter limit\n");
scanf("%d",&n);
printf("enter nos.\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,0,n-1);
printf("sorted:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
void sort(int a[100],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
sort(a,low,mid);
sort(a,mid+1,high);
merge(a,low,high,mid);
}
}
void merge(int a[100],int low,int high,int mid)
{
int c[100],i,j,k;
i=low;
j=mid+1;
k=low;
while(i<=mid&&j<=high)
{
if(a[i]<a[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=a[j];
k++;
j++;
}
}
if(i<=mid)
{
c[k]=a[i];
k++;
i++;
}
if(j<=high)
{
c[k]=a[j];
k++;
j++;
}
for(i=low;i<=high;i++)
{
a[i]=c[i];
}
OUTPUT:
enter limit:
5
enter nos.
12
32
24
54
13
Sorted array:
12
13
24
32
54
6) INSERTION SORT
#include<all.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
void main()
{
float t;
clock_t et,st;
int n,a[100],i,j;
clrscr();
printf("limit?\n");
scanf("%d",&n);
printf("enter nos.\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
st=clock();
sort(a,n);
et=clock();
t=(et-st)/CLK_TCK;
printf("time:%f\n",t);
getch();
}
void sort(int a[100],int n)
{
int i,j,item;
delay(1000);
for(i=1;i<=n-1;i++)
{
item=a[i];
j=i-1;
while(j>=0 && a[j]>item)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=item;
}
printf("sorted:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
OUTPUT:
Enter array limit:
5
enter nos.
34
12
22
67
54
Sorted array:
12
22
34
54
67
time:0.989011 secs
4) DFS AND BFS:
#include<all.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
void dfs(int);
void bfs(int);
int vis[10],a[20][20],n,vis1[10];
void main()
{
int i,j,ch;
clrscr();
printf("enter no. of nodes\n");
scanf("%d",&n);
printf("enter the matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
vis[i]=vis1[i]=0;
}
}
while(1)
{
printf("do u wanna traverse by\n1)dfs\n2)bfs\n3)exit\n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=1;i<=n;i++)
{
if(vis[i]==0)
{
dfs(i);
}
}
break;
case 2:
for(i=1;i<=n;i++)
{
if(vis1[i]==0)
{
bfs(i);
}
}
break;
case 3: exit(0);
}
}
}
void dfs(int v)
{
int u;
vis[v]=1;
printf("%d\n",v);
for(u=1;u<n;u++)
{
if(a[v][u]==1 && vis[u]==0)
{
dfs(u);
}
}
}
void bfs(int v)
{
int r=0,f=0,q[20],u;
vis1[v]=1;
q[r]=v;
printf("%d\n",v);
while(f<=r)
{
v=q[f++];
for(u=1;u<=n;u++)
{
if(a[v][u]==1 && vis[u]==0)
{
vis1[u]=1;
q[++r]=u;
printf("%d\n",u);
}
}
}
}
OUTPUT:
1
2
4
3
5
1
2
3
4
5
for(i=0;i<n;i++)
{
if(vis[i]==0)
dfs(i);
}
printf("TOPOLOGICAL SORTED:\n");
for(i=n-1;i>=0;i--)
{
printf("%d ",exp[i]);
}
getch();
}
void dfs(int x)
{ int j;
vis[x]=1;
for(j=0;j<n;j++)
{
if(a[x][j]==1&&vis[j]==0)
{
dfs(j);
}
}
exp[t++]=x;
}
OUTPUT:
ENTER THE NO. OF VERTICES:
7
0 1 1 0 0 0 0
0 0 0 0 1 0 1
0 0 0 0 0 1 0
1 1 1 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 1 0
TOPOLOGICAL SORTING:
3 0 2 1 6 5 4
NAME: MADHURI DIVYA N
SEMESTER: 4 ‘A’
USN: 1BM09CS030
TEACHER’S SIGNATURE: