- /******* SELECTION SORT *******/
- #include <stdio.h>
- void selection_sort(int a[], int size);
- int main(void)
- {
- int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
- int i = 0;
- clrscr();
- printf("before:\n");
- for(i = 0; i < 10; i++) printf("%d ", arr[i]);
- printf("\n");
- selection_sort(arr, 10);
- printf("after:\n");
- for(i = 0; i < 10; i++) printf("%d ", arr[i]);
- printf("\n");
- getch();
- return 0;
- }
- void selection_sort(int a[], int size)
- {
- int i = 0;
- int j = 0;
- int large = 0;
- int index = 0;
- for(i = size-1; i > 0; i--)
- {
- large = a[0];
- index = 0;
- for(j = 1; j <= i; j++)
- if(a[j] > large)
- { large = a[j];
- index = j; }
- a[index] = a[i];
- a[i] = large;
- }
- }
- /******* HEAP SORT *******/
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXARRAY 5
- void heapsort(int ar[], int len);
- void heapbubble(int pos, int ar[], int len);
- int main(void)
- {
- int array[MAXARRAY];
- int i = 0;
- clrscr();
- for(i = 0; i < MAXARRAY; i++)
- array[i] = rand() % 100;
- printf("Before heapsort: ");
- for(i = 0; i < MAXARRAY; i++)
- {
- printf(" %d ", array[i]);
- }
- printf("\n");
- heapsort(array, MAXARRAY);
- printf("After heapsort: ");
- for(i = 0; i < MAXARRAY; i++)
- {
- printf(" %d ", array[i]);
- }
- printf("\n");
- getch();
- return 0;
- }
- void heapbubble(int pos, int array[], int len)
- {
- int z = 0;
- int max = 0;
- int tmp = 0;
- int left = 0;
- int right = 0;
- z = pos;
- for(;;)
- {
- left = 2 * z + 1;
- right = left + 1;
- if(left >= len)
- return;
- else if(right >= len)
- max = left;
- else if(array[left] > array[right])
- max = left;
- else
- max = right;
- if(array[z] > array[max])
- return;
- tmp = array[z];
- array[z] = array[max];
- array[max] = tmp;
- z = max;
- }
- }
- void heapsort(int array[], int len)
- {
- int i = 0;
- int tmp = 0;
- for(i = len / 2; i >= 0; --i)
- heapbubble(i, array, len);
- for(i = len - 1; i > 0; i--)
- {
- tmp = array[0];
- array[0] = array[i];
- array[i] = tmp;
- heapbubble(0, array, i);
- }
- }
- /******* MERGE SORT *******/
- #include<stdio.h>
- #include<conio.h>
- #define MAXARY 100
- void mergesort(int x[],int end,int start);
- int main(void)
- {
- int ary[MAXARY];
- int j=0,n;
- printf("Enter the number of elements in array:");
- scanf("%d",&n);
- printf("\n\nEnter the elements to be sorted: \n");
- for(j=0;j<n;j++)
- scanf("%d",&ary[j]);
- printf("Before :");
- for(j=0;j<n;j++)
- printf(" %d",ary[j]);
- printf("\n");
- mergesort(ary,0,n-1);
- printf("After Merge Sort :");
- for(j=0;j<n;j++)
- printf("%d",ary[j]);
- printf("\n");
- getch();
- return 0;
- }
- void mergesort(int x[],int end,int start)
- {
- int j=0;
- const int size=start-end+1;
- int mid=0;
- int mrg1=0;
- int mrg2=0;
- int executing[MAXARY];
- if(end==start)
- return;
- mid=(end+start)/2;
- mergesort(x,end,mid);
- mergesort(x,mid+1,start);
- for(j=0;j<size;j++)
- executing[j]=x[end+j];
- mrg1=0;
- mrg2=mid-end+1;
- for(j=0;j<size;j++)
- {
- if(mrg2<=start-end)
- if(mrg1<=mid-end)
- if(executing[mrg1]>executing[mrg2])
- x[j+end]=executing[mrg2++];
- else
- x[j+end]=executing[mrg1++];
- else
- x[j+end]=executing[mrg2++];
- else
- x[j+end]=executing[mrg1++];
- }
- }
- /******* QUICK SORT *******/
- #include<stdio.h>
- #include<conio.h>
- #define MAXARRAY 10
- void quicksort(int arr[],int low,int high);
- int main(void)
- {
- int array[MAXARRAY]={0};
- int i=0;
- clrscr();
- for(i=0;i<MAXARRAY;i++)
- array[i]=rand () %100;
- printf("Before quicksort:");
- for(i=0;i<MAXARRAY;i++)
- {
- printf("%d ",array[i]);
- }
- printf("\n");
- quicksort(array,0,(MAXARRAY-1));
- printf("After quicksort: ");
- for(i=0;i<MAXARRAY;i++)
- {
- printf("%d ",array[i]);
- }
- printf("\n");
- getch();
- return 0;
- }
- void quicksort(int arr[],int low,int high)
- {
- int i=low;
- int j=high;
- int y=0;
- int z=arr[(low+high)/2];
- do
- {
- while(arr[i]<z) i++;
- while(arr[j]>z) j--;
- if(i<=j)
- {
- y=arr[i];
- arr[i]=arr[j];
- arr[j]=y;
- i++;
- j--;
- }
- }while(i<=j);
- if(low<j)
- quicksort(arr,low,j);
- if(i<high)
- quicksort(arr,i,high);
- }
- /******* BST *******/
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int mid,num,lower=0,upper=8,flag=1,n,i,arr[10];
- clrscr();
- printf("\nEnter the elements:");
- scanf("%d",&n);
- printf("\nEnter the elements in sorted order");
- for(i=0;i<n;i++)
- scanf("%d",&arr[i]);
- printf("\nEnter the elements to be searched:");
- scanf("%d",&num);
- for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
- {
- if(arr[mid]==num)
- {
- printf("\nThe number is at location %d",mid);
- flag=0;
- break;
- }
- if(arr[mid]<num)
- lower=mid+1;
- else
- upper=mid-1;
- }
- if(flag==1)
- printf("\nElement not in the list");
- getch();
- }
- /******* KRUSKALS *******/
- #include<stdio.h>
- #include<conio.h>
- int parent[10];
- int a,b,u,v,i,j,n,noofedges=1,min,mincost=0,cost[10][10];
- void kruskals(int cost[][10],int n);
- void main()
- {
- clrscr();
- printf("\nEnter the no. of vertices");
- scanf("%d",&n);
- printf("\nEnter the Adjacency Matrix\n");
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- {
- scanf("%d",&cost[i][j]);
- if(cost[i][j]==0)
- cost[i][j]=999;
- }
- kruskals(cost,n);
- getch();
- }
- void kruskals(int cost[][10],int n)
- {
- printf("\nThe Minimum Cost Edges are:\n");
- while(noofedges<n)
- {
- for(i=1,min=999;i<=n;i++)
- for(j=1;j<=n;j++)
- if(cost[i][j]<min)
- {
- min=cost[i][j];
- a=u=i;
- b=v=j;
- }
- while(parent[u])
- u=parent[u];
- while(parent[v])
- v=parent[v];
- if(u!=v)
- {
- noofedges++;
- printf("\nEdge (%d->%d)=%d",a,b,min);
- mincost+=min;
- parent[v]=u;
- }
- cost[a][b]=cost[b][a]=999;
- }
- printf("\nMinimum Cost=%d",mincost);
- }
- /******* PRIMS *******/
- #include<stdio.h>
- #include<conio.h>
- int a,b,u,v,i,j,n,noofedge=1;
- int visited[10],min,minicost=0,cost[10][10];
- void prims(int cost[][10],int n)
- {
- for(i=0;i<=n;i++)
- visited[i]=0;
- printf("\n edges in the spanning tree are\n");
- visited[1]=1;
- while(noofedge<n)
- {
- for(i=1,min=999;i<=n;i++)
- for(j=1;j<=n;j++)
- if(cost[i][j]<min)
- if(visited[i]==0)
- continue;
- else
- {
- min=cost[i][j];
- a=u=i;
- b=v=j;
- }
- if(visited[u]==0||visited[v]==0)
- {
- noofedge++;
- printf("\n edge(%d->%d)=%d",a,b,min);
- minicost+=min;
- visited[b]=1;
- }
- cost[a][b]=cost[b][a]=999;
- }
- printf("\nMinimumcost Spanning Tree %d",minicost);
- }
- void main()
- {
- clrscr();
- printf("\n prims algorithm\n");
- printf("\nenter the no of vertices");
- scanf("%d",&n);
- printf("\nenter the adjacency matrix");
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=n;j++)
- {scanf("%d",&cost[i][j]);
- if(cost[i][j]==0)
- cost[i][j]=999;
- }
- }
- prims(cost,n);
- getch();
- }
- /******* KNAPSACK *******/
- #include<stdio.h>
- #include<conio.h>
- int i,j,n,temp=0,index[20];
- float p[20],w[20],x[20],max,capacity;
- void read();
- void knapsack(float[],float[],float[],float,int);
- void display();
- void main()
- {
- clrscr();
- read();
- knapsack(p,w,x,max,n);
- display();
- getch();
- }
- void read()
- {
- printf("\nEnter maximum weight:\t");
- scanf("%f",&max);
- printf("\nEnter no of objects:\t");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- printf("\nEnter weight of %d item w[%d]:\t",i+1,i+1);
- scanf("%f",&w[i]);
- printf("\nEnter profit of %d item p[%d]:\t",i+1,i+1);
- scanf("%f",&p[i]);
- }
- }
- void knapsack(float p[],float w[],float x[],float max,int n)
- {
- for(i=0,index[temp]=i;i<n;i++)
- for(temp=0,j=0;j<n;j++)
- if((i!=j)&&(p[i]/w[i])<(p[j]/w[j]))
- temp++;
- capacity=max;
- for(i=0;i<n;i++)
- {
- if(w[index[i]]>capacity)
- break;
- x[index[i]]=1.0;
- capacity=capacity-w[index[i]];
- }
- if(i<n)
- x[index[i]]=capacity/w[index[i]];
- }
- void display()
- {
- float profit=0.0,max_cap=0.0;
- for(i=0;i<n;i++)
- profit=profit+(x[i]*p[i]);
- for(i=0;i<n;i++)
- max_cap=max_cap+(w[i]*x[i]);
- printf("\nThe optimal solution is\n");
- printf("\n%9s %9s %9s %7s"," " ," Weight","Profit","X");
- printf("\n\n%9s %9s %9s %9s"," i","w[i]","p[i] ","x[i]");
- printf("\n------------------------------------------------\n");
- for(i=0;i<n;i++)
- printf("\n%9d%9.2f %9.2f %9.2f",i+1,w[i],p[i],x[i]);
- printf("\n\nThe sum of p[i]*x[i]=%0.2f",profit);
- printf("\n\nThe sum of w[i]*x[i]=%0.2f",max_cap);
- }
- /******* MULTISTAGE GRAPH *******/
- #include<stdio.h>
- #include<conio.h>
- int G[50][50],n,i,j,h,k;
- void FGraph();
- int findR();
- void main ()
- {
- clrscr();
- printf("\t\t\t\tMultistage Graph");
- printf("\nEnter the number of the vertices: ");
- scanf("%d",&n);
- printf("\nEnter the total number of stages in your graph: ");
- scanf("%d",&k);
- printf("\nIf there is a edge between the following vertices enter its weight else 0:\n");
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- {
- G[i][j] = 0;
- if((i!=j)&&(i<j))
- {
- printf("%d and %d :",i,j);
- scanf("%d",&G[i][j]);
- }
- }
- FGraph();
- getch();
- }
- void FGraph()
- {
- int cost[50],d[50],p[50],r;
- for (i = 1; i<=n; i++)
- cost[i] = 0;
- for(j=n-1; j>=1; j--)
- {
- r = findR(j+1);
- cost[j] = G[j][r]+cost[r];
- d[j] = r;
- }
- p[1] = 1;
- p[k] = n;
- for(j = 2; j<k; j++)
- p[j] = d[p[j-1]];
- printf("%d-",d[1]);
- for(j=2; j<=n; j++)
- {
- if((d[j] == d[j-1]) || (d[j] == 0))
- continue;
- if(d[j]<=n)
- printf("%d-",d[j]);
- }
- printf("%d",n);
- }
- int findR(int cu)
- {
- int r1 = n+1;
- for(h =1; h<=n; h++)
- {
- if( (G[h][cu] != 0) && ( r1 == n+1 ) )
- {
- r1 = h;
- continue;
- }
- if (G[h][cu] != 0)
- {
- if(G[h][cu] < G[r1][cu] )
- r1 = h;
- }
- }
- return r1;
- }
- /******* 8 QUEENS *******/
- #include<stdio.h>
- #include<conio.h>
- int n,count,a[18][18];
- void refresh(int k)
- {
- int i;
- for(i=0;i<=n;i++)
- a[i][k]=0;
- }
- void display()
- {
- int i,j;
- count++;
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- if(a[i][j])
- printf("Q");
- else
- printf("#");
- printf("\n\n");
- }
- printf("\n\n\n\n");
- }
- void eq(int k)
- {
- int i,j,r,count;
- if(k==n)
- {
- display();
- return;
- }
- r=0;
- while(r<n)
- {
- count=1;
- for(j=0;j<=n;j++)
- if(a[r][j]==1)
- count=0;
- i=r;
- j=k;
- while(count&&(i<n)&&(j<n))
- {
- if(a[i][j])
- count=0;
- i++;
- j++;
- }
- i=r;
- j=k;
- while(count&&(i>-1)&&(j>-1))
- {
- if(a[i][j])
- count=0;
- i--;
- j--;
- }
- i=r;
- j=k;
- while(count&&(i>-1)&&(j<n))
- {
- if(a[i][j])
- count=0;
- i--;
- j++;
- }
- i=r;
- j=k;
- while(count&&(i<n)&&(j>-1))
- {
- if(a[i][j])
- count=0;
- i++;
- j--;
- }
- if(count)
- {
- a[r][k]=1;
- eq(k+1);
- refresh(k);
- }
- r++;
- }
- }
- void main()
- {
- clrscr();
- printf("\nEnter the no. of queens:");
- scanf("%d",&n);
- printf("\n");
- eq(0);
- printf("\nThe total no. of combinations is %d",count);
- getch();
- }
- /******* GRAPH COLOURING *******/
- #include<stdio.h>
- #include<conio.h>
- #define MAX 10
- int g[MAX][MAX],m,edges,color_tab[MAX];
- void main()
- {
- int i,j,n;
- void gen_col_value(int,int);
- void gr_coloring(int,int);
- int get_data();
- void display(int);
- clrscr();
- printf("\n\n\t\tGRAPH COLORING...\n\n");
- for(i=0;i<MAX;i++)
- {
- for(j=0;j<MAX;j++)
- {
- g[i][j]=0;
- color_tab[i]=0;
- }
- }
- n=get_data();
- gr_coloring(1,n);
- display(n);
- getch();
- }
- void gen_col_value(int k,int n)
- {
- int j,a,b;
- while(1)
- {
- a=color_tab[k]+1;
- b=m+1;
- color_tab[k]=a%b;
- if(color_tab[k]==0)
- return;
- for(j=1;j<=n;j++)
- {
- if(g[k][j]&&color_tab[k]==color_tab[j])
- break;
- }
- if(j==n+1)
- return;
- }
- }
- void gr_coloring(int k,int n)
- {
- gen_col_value(k,n);
- if(color_tab[k]==0)
- return;
- if(k==n)
- return;
- else
- gr_coloring(k+1,n);
- }
- int get_data()
- {
- int v1,v2,i,n;
- printf("Enter the no.of nodes:");
- scanf("%d",&n);
- printf("\nEnter the no.of edges:");
- scanf("%d",&edges);
- m=n-1;
- printf("\n\nEnter the edges of the graph:");
- printf("By given values of v1 & v2 \n\n");
- for(i=1;i<=edges;i++)
- {
- scanf("%d %d",&v1,&v2);
- g[v1][v2]=g[v2][v1]=1;
- }
- return n;
- }
- void display(int n)
- {
- int i;
- printf("\nThe vertices to be colored as.....\n\n");
- for(i=1;i<=n;i++)
- {
- printf("\n\n");
- printf("\n\tvertices %d=",i);
- if(color_tab[i]==1)
- {
- printf("RED");
- }
- if(color_tab[i]==2)
- {
- printf("GREEN");
- }
- if(color_tab[i]==3)
- {
- printf("BLUE");
- }
- if(color_tab[i]==4)
- {
- printf("YELLOW");
- }
- }
- }
- /******* TSP *******/
- #include"stdio.h"
- int x[15],used[15];
- int adj[15][15]={0};
- int path[15][15],wght[15];
- int c,min;
- int path_ok(int k,int n)
- {
- if(used[x[k]])
- return 0;
- if(k<n-1)
- return(adj[x[k-1]][x[k]]);
- else
- return(adj[x[k-1]][x[k]] && adj[x[k]][x[0]]);
- }
- void TSP(int k,int n)
- {
- int i,sum;
- for(x[k]=1;x[k]<n;x[k]++)
- {
- if(path_ok(k,n))
- {
- used[x[k]]=1;
- if(k==n-1)
- {
- sum=0;
- printf("\n\n\tPOSSIBLE PATH %d : ",c+1);
- for(i=0;i<n;i++)
- {
- printf("%d\t",x[i]);
- path[c][i]=x[i];
- sum+=adj[x[i]][x[i+1]];
- }
- printf(" : %d",sum);
- wght[c]=sum;
- if(c==0 || sum<min)
- min=sum;
- c++;
- used[x[k]]=0;
- getch();
- }
- else
- TSP(k+1,n);
- used[x[k]]=0;
- }
- }
- }
- void findmin(int n)
- {
- int i,j;
- for(i=0;i<c;i++)
- if(wght[i]==min)
- {
- printf("\n\n\tMINIMUM PATH : ");
- for(j=0;j<n;j++)
- printf("%d\t",path[i][j]);
- }
- }
- void main()
- {
- int i,n,j;
- int edg;
- clrscr();
- printf("\n\n\t\tTRAVELLING SALESMAN PROBLEM\n\n");
- printf("\n\tEnter the no. of Cities : ");
- scanf("%d",&n);
- printf("\n\n Enter the Cost if path Exist Between cities.:{c1,c2}.Else Enter 0\n\n");
- printf("\n\tCITIES\t\tCOST\n\n");
- for(i=0;i<n;i++)
- for(j=i+1;j<n;j++)
- {
- printf("\n\t %d------ %d \t:",i,j);
- scanf("%d",&edg);
- if(edg)
- adj[i][j]=adj[j][i]=edg;
- }
- used[0]=1;
- TSP(1,n);
- if(!c)
- printf("\n\n\t\tNO PATH FOUND TO COVER ALL THE CITIES\n\n");
- else
- {
- printf("\n\n\t\tMINIMUM COST IS %d AND THE PATHS ARE \n\n",min);
- findmin(n);
- }
- getch();
- }