Linear Search
Linear Search
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int s,j,i,n,a[100],flag;
printf("enter the number");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the search no");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(s==a[i])
{
j=i+1;
flag=1;
}
}
if(flag==0)
{
printf("no. is not found");
}
else
printf("%d",j);
getch();
}
//Binary Search
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,s,mid,beg,end,a[100];
printf("enter the no");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("search no");
scanf("%d",&s);
beg=0;
end=n-1;
mid=(beg+end)/2;
while(s!=a[mid] && beg<=end)
{
if(s<a[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+mid)/2;
}
if(s==a[mid])
printf("%d",mid+1);
else
printf("not found");
getch();
}
//selection sort//
#include<stdio.h>
#include<conio.h>
void selectionsort(int a[],int n);
void main()
{
int i,n,a[10];
clrscr();
printf("enter thje size of array");
scanf("%d",&n);
printf("\nenter the element");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
printf("\n sorted array\n\n");
for(i=0;i<n;i++)
printf("\n %d",a[i]);
printf("\n");
getch();
}
void selectionsort(int a[],int n)
{
int temp, small,loc,i,j;
for(i=1;i<=n-1;i++)
{
small= a[i-1];
loc=i-1;
for(j=i;j<=n-1;j++)
{
if(a[j]<small)
{
small=a[j];
loc=j;
}
}
if(loc!=(i-1))
{
temp=a[i-1];
a[i-1]=a[loc];
a[loc]=temp;
}
}
}
// BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,temp,a[100];
printf("enter the number");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("sorted array");
for(j=0;j<n;j++)
printf("%d",a[j]);
getch();
}
//insertion sort
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j,k,key,n,a[100];
printf("enter the no");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(j=1;j<n;j++)
{
key=a[j];
i=j-1;
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
//counting sort
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,max=0,min=0,a[10],b[20],c[100],n;
clrscr();
printf("enter the number of element");
scanf("%d",&n);
printf("enter the element now");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min= a[i];
}
}
for(i=min;i<=max;i++)
{
c[i]=0;
}
for(j=0;j<n;j++)
{
c[a[j]]=c[a[j]]+1;
}
for(i=min+1;i<=max;i++)
c[i]=c[i]+c[i-1];
for(j=n-1;j>=0;j--)
{
b[c[a[j]]]=a[j];
c[a[j]]=c[a[j]]-1;
}
printf("sortted array");
for(i=0;i<=n;i++)
printf("%d\t",b[i]);
getch();
}
//heap sort
#include<stdio.h>
#include<conio.h>
int arr[100],n,heapsize,count=0;
void buildmax();
void maxheapify(int);
void heapsort();
void display();
void main()
{
clrscr();
int i;
printf("enter the element");
scanf("%d",&n);
printf("enter the element");
heapsize=n;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
buildmax();
heapsort();
display();
getch();
}
void buildmax()
{
int a,i;
a=heapsize/2;
for(i=a-1;i>=0;i--)
maxheapify(i);
}
void maxheapify(int i)
{
int l,r,largest,c;
l=2*i+1;
r=2*i+2;
if((l<=heapsize-1)&& (arr[l]>arr[i]))
{
largest=l;
}
else
{
largest=i;
}
if((r<=heapsize-1) && (arr[r]>arr[largest]))
{
largest=r;
}
if(largest!=i)
{
c=arr[i];
arr[i]=arr[largest];
arr[largest]=c;
maxheapify(largest);
}
}
void heapsort()
{
int i,c;
for(i=heapsize-1;i>=0;i--)
{
c=arr[0];
arr[0]=arr[i];
arr[i]=c;
heapsize=heapsize-1;
maxheapify(0);
}
}
void display()
{
int i;
printf("the sortted array is ");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
//merge sort
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[10],b[10],n;
void get_data()
{
int i;
printf("enter trhe length of list");
scanf("%d",&n);
printf("\n enter the list");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display_data()
{
int i;
printf("\n\n the sortted array...\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void combine(int low,int mid,int high)
{
int i,l,j,k;
l=low;
i=low;
j=mid+1;
while(l<=mid && j<=high)
{
if(a[l]<=a[j])
b[i]=a[l++];
else
b[i]=a[j++];
i++;
}
if(l>mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=l;k<=mid;k++)
b[i++]=a[k];
for(k=low;k<=high;k++)
a[k]=b[k];
}
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
combine(low,mid,high);
}
}
void main()
{
int low, high;
clrscr();
printf("\n merge sort");
get_data();
low=0;
high=n-1;
merge_sort(low,high);
display_data();
getch();
}
//quick sort
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 10;
int get_data(int);
void quicksort(int,int);
int partition(int,int);
void swap(int,int) ;
int arr[10],temp[10];
int main()
{
int n, i ;
clrscr();
printf("\n\t\t quick sort method \n");
n=get_data(n);
quicksort(0,n-1);
printf("\n\n sortted array");
for(i=0;i<n;i++)
printf("\t%d",arr[i]);
getch();
return 0;
}
int get_data(int n)
{
int i;
printf("\n enter the total no sort");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter %d number ",i+1);
scanf("%d",&arr[i]);
}
return n;
}
void quicksort(int p,int q)
{
int j;
if(p<q)
{
j=partition(p,q+1);
quicksort(p,j-1);
quicksort(j+1,q);
}
}
int partition(int m,int p)
{
int pivot=arr[m],i=m,j=p;
do
{
do
{
i++;
}
while(arr[i]<pivot);
do
{
j--;
}
while(arr[j]>pivot);
if(i<j)
swap(i,j);
}
while(i<j);
arr[m]=arr[j];
arr[j]=pivot;
return j;
}
void swap(int i, int j)
{
int p;
p=arr[i];
arr[i]=arr[j];
arr[j]=p;
}
// minimum spaining tree krushkal algo
#include<stdio.h>
#include<conio.h>
#define size 20
#define infinite 32767
void prim(int g[][size],int nodes)
{
int tree[size], i ,j,k;
int min_dist, v1,v2,total=0;
for(i=0;i<nodes;i++)
tree[i]=0;
printf("\n\n the minimum spaining tree is :\n");
tree[0]=1;
for(k=1;k<=nodes-1;k++)
{
min_dist=infinite;
for(i=0;i<=nodes-1;i++)
{
for(j=0;j<=nodes-1;j++)
{
if(g[i][j] && (( tree[i] && !tree[j])|| (!tree[i] && tree[j])))
{
if(g[i][j] < min_dist)
{
min_dist=g[i][j];
v1=i;
v2=j;
}
}
}
}
printf("\n edge (%d %d)and weight=%d",v1,v2,min_dist);
tree[v1]=tree[v2]=1;
total= total+min_dist;
}
printf("\n\n\t total path length is = %d",total);
}
void main()
{
int g[size][size],nodes;
int v1, v2, length,i,j,n;
clrscr();
printf("\n\t prims algo\n");
printf("\n enter the number of nodes in graph");
scanf("%d",&nodes);
printf("enter the number of edge in the graph");
scanf("%d",&n) ;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
g[i][j]=0;
printf("\n enter the edge and weight \n");
for(i=0;i<n;i++)
{
printf("\nenter the edge by v1 and v2 : ");
printf("[read the graph starting node 0]") ;
scanf("%d %d",&v1,&v2);
printf("\nenter the corresponding weight :") ;
scanf("%d",&length);
g[v1][v2]=g[v2][v1] =length;
}
getch();
printf("\n\t");
clrscr();
prim(g,nodes);
getch();
}
// all pair shortest parth ( Floyd warshal algo)
#include<conio.h>
#include<stdio.h>
void main()
{
int wt[10][10],n,i,j;
void floyd_shortest_path(int matrix[10][10],int n);
clrscr();
printf("\n create a graph using adjacency matrix");
printf("\n\n how many vertices there");
scanf("%d",&n);
printf("\nenter the element");
printf("[enter 999 the infinite value]");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\nwt[%d][%d]",i,j);
scanf("%d",&wt[i][j]);
}
}
printf("\n\tcomputing all pair shortest path....\n");
floyd_shortest_path(wt,n);
getch();
}
void floyd_shortest_path(int wt[10][10],int n)
{
int d[5][10][10],i,j,k;
int min(int,int);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[0][i][j]=wt[i][j];
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[k][i][j]=min(d[k-1][i][j],(d[k-1][i][k]+d[k-1][k][j]));
}
}
}
for(k=0;k<=n;k++)
{
printf("D(%d)=\n",k);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",d[k][i][j]);
}
printf("\n");
}
}
}
int min(int a,int b)
{
if(a<b)
return a;
else return b;
}
//travelling salesman problem
#include<stdio.h>
#include<conio.h>
#define max 10
typedef struct
{
int nodes[10];
int vertex;
int min;
}
path_node;
path_node tsp(int source,path_node list,int element[][max],int max_no_cities)
{
int i,j;
path_node new_list,new_path,new_min;
if(list.vertex==0)
{
new_min.min=element[source][1];
new_min.nodes[max_no_cities-1]=source;
new_min.vertex=max_no_cities;
return new_min;
}
for(i=0;i<list.vertex;i++)
{
new_list.vertex=0;
for(j=0;j<list.vertex;j++)
if(i!=j)
new_list.nodes[new_list.vertex++]=list.nodes[j];
new_path =tsp(list.nodes[i],new_list,element,max_no_cities);
new_path.min=element[source][list.nodes[i]]+new_path.min;
new_path.nodes[max_no_cities-list.vertex-1]=source;
if(i==0)
new_min=new_path;
else
if(new_path.min<new_min.min)
new_min=new_path;
}
return new_min;
}
void display(path_node path)
{
int i;
printf("\n\n the minimum cost is %d\n",path.min);
printf("\n the path is ....\n");
for(i=0;i<path.vertex;i++)
printf("%d--",path.nodes[i]);
printf("%d",path.nodes[0]);
}
main()
{
int i,j,element[max][max],max_no_cities;
path_node graph, path;
clrscr();
printf("\n how many number cities there");
scanf("%d",&max_no_cities);
if(max_no_cities==0)
{
printf("error there is no cities procesing");
}
else
{
for(i=1;i<=max_no_cities;i++)
{
for(j=1;j<=max_no_cities;j++)
if(i==j)
element[i][i]=0;
else
{
printf("enter the distance from cities %d to %d ",i,j);
scanf("%d",&element[i][j]);
}
if(i>1)
graph.nodes[i-2]=i;
}
graph.vertex=max_no_cities-1;
path=tsp(1,graph,element,max_no_cities);
display(path);}
getch();
return 1;
}
#include<stdio.h>
#include<conio.h>
void knapsack(int n,float m,float w[],float p[] );
void main()
{
int i,j,n;
float p[15],w[15],c[15],temp,m;
clrscr();
printf("\nenter of object ");
scanf("%d",&n);
printf("\nenter the weights");
for(i=0;i<n;i++)
scanf("%f",&w[i]);
flushall();
printf("\nenter the profit");
for(i=0;i<n;i++)
scanf("%f",&p[i]);
printf("\nenter the knapsack size");
scanf("%f",&m);
for(i=0;i<n;i++)
c[i]=p[i]/w[i];
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(c[j]<c[j+1])
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
printf("\n the item are arranged...\n");
printf("\n\nitem\tweight\tprofit");
{
for(i=0;i<n;i++)
printf("\nx[%d]\t%.0f\t\t%.0f",i,w[i],p[i]);
}
knapsack(n,m,w,p);
getch();
}
void knapsack(int n,float m,float w[],float p[])
{
float x[15],u,profit=0.0,weight=0.0;
int i;
u=m;
for(i=0;i<n;i++)
x[i]=0.0;
for(i=0;i<n;i++)
{
if(w[i]>u)
break;
x[i]=1.0;
u=u-w[i];
}
if(i<n)
x[i]=u/w[i];
printf("\nsolution vector is");
for(i=0;i<n;i++)
printf("\n%d\t%.2f",i,x[i]);
for(i=0;i<n;i++)
{
w[i]=w[i]*x[i];
p[i]=p[i]*x[i];
}
for(i=0;i<n;i++)
{
profit=profit+p[i];
weight=weight+w[i];
}
printf("\nmaximum profit is");
printf("\n\t\t%.2f",profit);
printf("\nweight is");
printf("\n\t\t%.2f",weight);
}
int main(void)
{
int row, col, i;
char board[100][100], response;
clrscr();
printf("@@ This is n-queen problem. Enter the number of queens(say n) and watch
how computer places them in (n x n) matrix such that none can meet another moving
along horizontally, vertically or digonally. ");
printf(" Enter the number of queens : ");
scanf("%d", &no_of_queens);
if(no_of_queens > 23)
{
printf("@@ Thought the program is OK for any queen value.But due the
configuration of the output screen the output will be tranketed (A very large queen
number may cause the system stack overflow). So it is highly recommended that you
run the program with maximum queen number 23...");
printf("Want to continue(Y/N)?");
fflush(stdin);
scanf("%c", &response);
if(toupper(response) == 'N')
return (0);
}
else if(no_of_queens < 3)
{
printf("The number of Queen must be greater than 3.");
getch();
return (0);
}
clrscr();
printf("M/c in work ! Please Wait...");
// This for-loop is used for checking all the columns of row 0 only...
_setcursortype(_NOCURSOR);
for(col = 0; col < no_of_queens; col++)
{
memset(board, '_', sizeof(board));
check( 0, col, board );
}
clrscr();
printf("Thank you for seeing this program through.");
getch();
return (0);
}
ch = (char)getch();
clrscr();
if(ch == 'e')
exit (0);
printf("M/c in work ! Please Wait...");
}
// Vertical check...
for(i = 0; i < r; i++)
{
if ( board[i][c] == queen)
return;
}
// Horizontal check...
for(j = 0; j < c; j++)
{
if ( board[r][j] == queen)
return;
}
// Left-Diagonal check...
i = r; j = c;
do
{
if ( board[i][j] == queen )
return;
i--; j--;
}
while( i >= 0 && j >= 0 );
// Right-Diagonal check...
i = r; j = c;
do
{
if ( board[i][j] == queen )
return;
i--; j++;
}
while( i >= 0 && j < no_of_queens );
// This for-loop is used for checking all the columns for each row
//starting from 1 upto the end...
for(int p = 0; p < no_of_queens; p++)
check(r, p, board);
for(int h = 0; h < no_of_queens; h++)
board[r - 1][h] = '_ ';
{
if(flagrow)
printf(" \n ");
for(i = 0; i < no_of_queens; i++)
printf("%3d", i+1);
}
gotoxy(62, 1);
printf("Press E to exit.");
textcolor(7 );
}