0% found this document useful (0 votes)
75 views

1) Linear Search & Binary Search

The document discusses various sorting and searching algorithms: 1) It compares linear search and binary search algorithms, providing code implementations and an example output showing binary search runs faster. 2) It presents selection sort algorithm code along with example output sorting an array of numbers. 3) Merge sort is described and code provided to sort an array, recursively dividing and merging subarrays. 4) Insertion sort code is given along with example output sorting a sample array. 5) Depth-first search (DFS) and breadth-first search (BFS) algorithms are explained and code provided to traverse a graph represented by an adjacency matrix. 6) Topological sort algorithm is described to sort

Uploaded by

Leanne Pai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

1) Linear Search & Binary Search

The document discusses various sorting and searching algorithms: 1) It compares linear search and binary search algorithms, providing code implementations and an example output showing binary search runs faster. 2) It presents selection sort algorithm code along with example output sorting an array of numbers. 3) Merge sort is described and code provided to sort an array, recursively dividing and merging subarrays. 4) Insertion sort code is given along with example output sorting a sample array. 5) Depth-first search (DFS) and breadth-first search (BFS) algorithms are explained and code provided to traverse a graph represented by an adjacency matrix. 6) Topological sort algorithm is described to sort

Uploaded by

Leanne Pai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

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

int binary(int a[],int low,int high,int key)


{
sleep(1);
mid=(low+high)/2;
if(low>high)
{
return (-1);
}
if(a[mid]==key)
{
return (mid+1);
}
if(key>a[mid])
{
return(binary(a,mid,high,key));
}
else
{
return(binary(a,low,mid,key));
}
}
OUTPUT:
Enter number of array elements: 5

Enter the array in ascending order:


1
3
5
7
9

1. Linear Search
2. Binary Search
Enter your choice: 1

Enter element to be searched for: 5

Element found at position 3


Time taken for execution of the algorithm = 2.995000

Enter number of array elements: 5

Enter the array in ascending order:


1
3
5
7
9

1. Linear Search
2. Binary Search
Enter your choice: 2

Enter element to be searched for: 7

Element found at posititon 4


Time taken for execution of the algorithm = 1.997000
2) SELECTION SORT
#include<all.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>

void sort(int [],int);

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:

enter array limit


5
enter 5 nos.
12
26
1
78
45
Sorted array:
1
12
26
45
78
time:0.989011 secs
3) MERGE SORT:

#include<all.h>
#include<stdio.h>
#include<conio.h>

void sort(int [],int ,int);


void merge(int [],int,int,int);

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 sort(int [],int );

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:

do u want to traverse by:


1)dfs
2)bfs
3)exit
enter choice
1

1
2
4
3
5

do u want to traverse by:


1)dfs
2)bfs
3)exit
enter choice
2

1
2
3
4
5

do u wanna traverse by:


1)dfs
2)bfs
3)exit
enter choice
3
5) TOPOLOGICAL SORT
#include<stdio.h>
#include<conio.h>
void dfs(int);
int a[10][10],vis[10],exp[10],n,t=0;
void main()
{ int i,j;
clrscr();
printf("ENTER NUMBER OF VERTICES:\n");
scanf("%d",&n);
printf("ENTER ADJACENCY MATRIX:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
vis[i]=0;
}

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

ENTER ADJACENCY MATRIX:

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

BRANCH: COMPUTER SCIENCE

SEMESTER: 4 ‘A’

USN: 1BM09CS030

TEACHER’S SIGNATURE:

You might also like