Sie sind auf Seite 1von 23

PROGRAM 1

Name : Gaurav Kumar Roll No.:1109110035 Date : 06-08-2013 Objective: Write a program to sort an array using bubble sort. Input
#include<stdio.h> #include<conio.h> void main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d ", array[c]); getch(); }

Output
Enter number of elements 5 Enter 5 integers 51623 Sorted list in ascending order: 1 2 3 5 6

PROGRAM 2
Name : Gaurav Kumar Roll No.:1109110035 Date : 06-08-2013 Objective: Write a program to sort an array using insertion sort. Input
#include <stdio.h> #include<conio.h> void main() { int n, array[1000], c, d, t; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t= array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:\n"); for (c = 0; c <= n - 1; c++) printf("%d ", array[c]); getch(); }

Output
Enter number of elements 5 Enter 5 integers 6 4 7 3 8 Sorted list in ascending order: 3 4 6 7 8

PROGRAM 3
Name : Gaurav Kumar Roll No.:1109110035 Date : 13-08-2013 Objective: Write a program to sort an array using selection sort. Input
#include <stdio.h> #include<conio.h> void main() { int array[100], n, c, d, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) if ( array[position] > array[d] ) position = d; if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d ", array[c]); getch(); }

Output
Enter number of elements 5 Enter 5 integers 6 4 3 2 1 Sorted list in ascending order: 1 2 3 4 6

PROGRAM 4
Name : Gaurav Kumar Roll No.:1109110035 Date : 13-08-2013 Objective: Write a program to print saddle point in matrix. Input
#include<stdio.h> #include<conio.h> void main() { int a[10][10],i,j,k,m,n,min,max,col; printf("enter order m,n of mxn matrix"); scanf("%d %d",&m,&n); printf("enter elements row-wise"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) { min=a[i][0]; for(j=0;j<n;j++) { if(a[i][j]<=min) { min=a[i][j]; col=j; } } max=a[0][col]; for(k=0;k<m;k++) if(a[k][col]>=max) max=a[k][col]; if(max==min) printf("saddle pt.at (%d,%d)& equals %d",i+1,col+1,a[i][col]); } getch(); }

Output
enter order m,n of mxn matrix 33 enter elements row-wise 1 2 3 4 5 6 7 8 9 saddle pt.at (3,1)& equals 7

PROGRAM 5
Name : Gaurav Kumar Roll No.:1109110035 Date : 20-08-2013 Objective: Write a program to sort an array using quick sort. Input
#include<stdio.h> #include<conio.h> int partition(int a[],int p,int r) { int i,j,x,k; x=a[r]; i=p-1; for(j=p;j<=r-1;j++) { if(a[j]<=x) { i=i+1; k=a[i]; a[i]=a[j]; a[j]=k; } } k=a[i+1]; a[i+1]=a[r]; a[r]=k; return i+1; } void quicksort(int a[],int p,int r) { if(p<r) { int q=partition(a,p,r); quicksort(a,p,q-1); quicksort(a,q+1,r); } } void main() { int a[100],n,i,j; clrscr(); printf("------QUICK SORT-----\n"); printf("\nEnter no. of elements\n"); scanf("%d",&n); printf("\nEnter element : "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nArray before sorting\n");

for(i=0;i<n;i++) printf("%d ",a[i]); quicksort(a,0,n-1); printf("\nArray after sorting\n"); for(i=0;i<n;i++) printf("%d ",a[i]); getch(); }

Output
------QUICK SORT----Enter no. of elements 5 Enter element: 7 3 8 2 6 Array before sorting 7 3 8 2 6 Array before sorting 2 3 8 7 6

PROGRAM 6
Name : Gaurav Kumar Roll No.:1109110035 Date : 26-08-2013 Objective: Write a program to sort an array using merge sort. Input
#include<stdio.h> #include<conio.h> void merge(int a[],int p,int q,int r) { int i,j,k,m,mid=q,high=r,l,temp[100]; m=mid+1; i=p; l=p; while(l<=mid && m<=high) { if(a[l]<=a[m]) { temp[i]=a[l]; l++; } else { temp[i]=a[m]; m++; } i++; } if(l>mid) { for(k=m;k<=high;k++) { temp[i]=a[k]; i++; } } else { for(k=l;k<=mid;k++) { temp[i]=a[k]; i++; } } for(k=p;k<=r;k++) a[k]=temp[k]; } void mergesort(int a[],int p,int r) { if(p<r) { int q;

q=((p+r)/2); mergesort(a,p,q); mergesort(a,q+1,r); merge(a,p,q,r); } } void main() { int i,n,a[100]; clrscr(); printf("\n---------MERGE SORT-----------\n"); printf("\nEnter no of elements\n"); scanf("%d",&n); printf("\nEnter element: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nArray before sorting\n"); for(i=0;i<n;i++) printf(" %d",a[i]); mergesort(a,0,n-1); printf("\nArray after sorting\n"); for(i=0;i<n;i++) printf(" %d",a[i]); getch(); }

Output
------MERGE SORT----Enter no. of elements 5 Enter element : 7 3 8 2 6 Array before sorting 7 3 8 2 6 Array before sorting 2 3 8 7 6

PROGRAM 7
Name : Gaurav Kumar Roll No.:1109110035 Date : 26-08-2013 Objective: Write a program to multiply 2 matrices using strassan matrix multiplication . Input
#include<stdio.h> #include<conio.h> void main() { int a[2][2],b[2][2],c[2][2],i,j; int m1,m2,m3,m4,m5,m6,m7; clrscr(); printf("Enter the 4 elements of first matrix: "); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); printf("Enter the 4 elements of second matrix: "); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&b[i][j]); printf("\nThe first matrix is\n"); for(i=0;i<2;i++) { printf("\n"); for(j=0;j<2;j++) printf("%d\t",a[i][j]); } printf("\nThe second matrix is\n"); for(i=0;i<2;i++) { printf("\n"); for(j=0;j<2;j++) printf("%d\t",b[i][j]); } m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]); m2= (a[1][0]+a[1][1])*b[0][0]; m3= a[0][0]*(b[0][1]-b[1][1]); m4= a[1][1]*(b[1][0]-b[0][0]); m5= (a[0][0]+a[0][1])*b[1][1]; m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]); m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=m1+m4-m5+m7; c[0][1]=m3+m5; c[1][0]=m2+m4; c[1][1]=m1-m2+m3+m6; printf("\nAfter multiplication using strassan multiplication \n"); for(i=0;i<2;i++) { printf("\n"); for(j=0;j<2;j++) printf("%d\t",c[i][j]); } getch(); }

Output
Enter the 4 elements of first matrix:1 2 3 4 Enter the 4 elements of second matrix:5 6 7 8 The first matrix is 1 2 3 4 The second matrix is 5 6 7 8 After multiplication using strassan multiplication 19 22 43 50

PROGRAM 8
Name : Gaurav Kumar Roll No.:1109110035 Date : 26-08-2013 Objective: Write a program to sort an array using radix sort . Input
#include<stdio.h> #include<conio.h> #define MAX 100 void print(int *a, int n) { int i; for (i = 0; i < n; i++) printf("%d\t", a[i]); } void radix_sort(int *a, int n) { int i, b[MAX], m = 0, exp = 1; for (i = 0; i < n; i++) if (a[i] > m) m = a[i]; while (m / exp > 0) { int box[10] = { 0 }; for (i = 0; i < n; i++) box[a[i] / exp % 10]++; for (i = 1; i < 10; i++) box[i] += box[i - 1]; for (i = n - 1; i >= 0; i--) b[--box[a[i] / exp % 10]] = a[i]; for (i = 0; i < n; i++) a[i] = b[i]; exp *= 10; } } void main() { int arr[MAX]; int i, num; clrscr(); printf("\nEnter total elements (num < %d) : ", MAX); scanf("%d", &num); printf("\nEnter %d Elements : ", num); for (i = 0; i < num; i++) scanf("%d", &arr[i]);

printf("\nARRAY : "); print(&arr[0], num); radix_sort(&arr[0], num); printf("\n\nSORTED : "); print(&arr[0], num); getch(); }

Output

PROGRAM 9
Name : Gaurav Kumar Roll No.:1109110035 Date : 26-08-2013 Objective: Write a program to sort an array using heap sort . Input
#include <stdio.h> #include <conio.h> void makeheap(int x[ ],int n) { int i,val,s,f; for(i=1;i<n;i++) { val=x[i] ; s=i ; f=(s-1)/2; while(s>0 && x[f]<val) { x[s]=x[f]; s=f; f=(s-1)/2; } x[s]=val; } } void heapsort(int x[ ],int n) { int i,s,f,ivalue; for(i=n-1;i>0;i--) { ivalue=x[i]; x[i]=x[0]; f=0; if(i==1) s=-1; else s=1; if(i>2 && x[2]>x[1]) s=2; while(s>=0 && ivalue<x[s]) { x[f]=x[s]; f=s; s=2*f+1; if(s+1<=i-1 && x[s]<x[s+1]) s++ ; if(s>i-1) s=-1;

} x[f]=ivalue; } } void main( ) { int arr[10],i,n; clrscr(); printf("Heap Sort.\n"); printf("\nEnter no of element"); scanf("%d",&n); printf("\nEnter elements\n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); makeheap(arr,n); printf("\nBefore Sorting:\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); heapsort(arr,n); printf("\nAfter Sorting:\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); getch(); }

Output

PROGRAM 10
Name : Gaurav Kumar Roll No.:1109110035 Date : 26-08-2013 Objective: Write a program to find kth smallest element of array . Input
#include <stdio.h> #include<conio.h> int positionPivot(int* array, int first, int last) { int tmp ,pivot,movingUp,movingDown; if (first == last) return -1; else if (first + 1 == last) return first; tmp = (first + last) / 2; pivot = array[tmp]; movingUp = first + 1; movingDown = last - 1; array[tmp] = array[first]; array[first] = pivot; while (movingUp <= movingDown) { while (movingUp <= movingDown && array[movingUp] < pivot) ++movingUp; while (pivot < array[movingDown]) --movingDown; if (movingUp <= movingDown) { tmp = array[movingUp]; array[movingUp] = array[movingDown]; array[movingDown] = tmp; ++movingUp; --movingDown; } } array[first] = array[movingDown]; array[movingDown] = pivot; return movingDown; } void positionKthElement(int* array, int first, int last, int k) { int index; while ((index = positionPivot(array, first, last)) != k) { if (k < index) last = index;

else first = index + 1; } } void main() { int array[10],i,n; clrscr(); printf("\nEnter no of elements\n"); scanf("%d",&n); printf("\nEnter elements\n"); for(i=0;i<n;i++) scanf("%d",&array[i]); for (i=0;i<n;i++) { positionKthElement(array,0,n,i); printf(" %d ", array[i]); } getch(); }

Output

PROGRAM 11
Name : Gaurav Kumar Roll No.:1109110035 Date : 26-08-2013 Objective: Write a program to sort an array using counting sort. Input
#include<stdio.h> #include<conio.h> void main() { int a[20],c[20],b[20]; int n,i,max=0; clrscr(); printf("Enter the length of array "); scanf("%d",&n); printf("Enter the elements\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); if(a[i]>max) max=a[i]; } for(i=0;i<=max;i++) c[i]=0; for(i=0;i<n;i++) c[a[i]]++; for(i=1;i<=max;i++) c[i]=c[i]+c[i-1]; for(i=n-1;i>=0;i--) { b[c[a[i]]]=a[i]; c[a[i]]--; } printf(\nSorted array\n); for(i=1;i<=n;i++) printf("%d,",b[i]); getch(); }

Output
Enter the length of array 5 Enter the elements 5 1 4 2 3 Sorted array 1,2,3,4,5

PROGRAM 12
Name : Gaurav Kumar Roll No.:1109110035 Date : 01-10-2013 Objective: Write a program to implement dijkstras algorihm. Input
#include<stdio.h> #include<conio.h> #define infinity 999 void dij(int n,int v,int cost[10][10],int dist[]) { int i,u,count,w,flag[10],min; for(i=1;i<=n;i++) flag[i]=0,dist[i]=cost[v][i]; count=2; while(count<=n) { min=99; for(w=1;w<=n;w++) if(dist[w]<min && !flag[w]) min=dist[w],u=w; flag[u]=1; count++; for(w=1;w<=n;w++) if((dist[u]+cost[u][w]<dist[w]) && !flag[w]) dist[w]=dist[u]+cost[u][w]; } } void main() { int n,v,i,j,cost[10][10],dist[10]; clrscr(); printf("\n Enter the number of nodes:"); scanf("%d",&n); printf("\n Enter the cost 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]=infinity; } printf("\n Enter the source matrix:"); scanf("%d",&v); dij(n,v,cost,dist);

printf("\n Shortest path:\n"); for(i=1;i<=n;i++) if(i!=v) printf("%d->%d,cost=%d\n",v,i,dist[i]); getch(); }

Output
Enter the number of nodes:5 Enter the cost matrix: 0 2 1 5 0 2 0 0 1 3 1 0 0 1 0 5 1 1 0 2 0 3 0 2 0 Enter the source rnatrix:1 Shortest path: 1>2 ,cost=2 1>3cost=1 1>4 ,cos=2 1>5 ,cost=4

PROGRAM 13
Name : Gaurav Kumar Roll No.:1109110035 Date : 01-10-2013 Objective: Write a program to implement kruskal algorihm. Input
#include<stdio.h> #include<conio.h> #include<stdlib.h> int i,j,k,a,b,u,v,n,ne=1; int min,mincost=0,cost[9][9],parent[9]; int find(int i) { while(parent[i]) i=parent[i]; return i; } int uni(int i,int j) { if(i!=j) { parent[j]=i; return 1; } return 0; } void main() { clrscr(); printf("\n\n\tImplementation of Kruskal's algorithm\n\n"); printf("\nEnter the no. of vertices\n"); scanf("%d",&n); printf("\nEnter the cost 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; } } printf("\nThe edges of Minimum Cost Spanning Tree are\n\n"); while(ne<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;

} } } u=find(u); v=find(v); if(uni(u,v)) { printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min); mincost +=min; } cost[a][b]=cost[b][a]=999; } printf("\n\tMinimum cost = %d\n",mincost); getch(); }

Output
Implementation of Xruskals algorithm Enter the no. of vertices 5 Enter the cost adjacency matrix 0 5 4 4 4 5 0 3 2 4 4 3 0 4 0 4 2 4 0 3 4 4 0 3 0 The edges of Minimum Cost Spanning Tree are 1 edge (2,4) =2 2 edge (2,3)= 3 3 edge (4,5) =3 4 edge (1,3) =4 Minimum cost = 12

PROGRAM 14
Name : Gaurav Kumar Roll No.:1109110035 Date : 01-10-2013 Objective: Write a program to implement prims algorihm. Input
#include<stdio.h> #include<conio.h> int a,b,u,v,n,i,j,ne=1; int visited[10]={0},min,mincost=0,cost[10][10]; void main() { clrscr(); printf("\n Enter the number of nodes:"); scanf("%d",&n); printf("\n Enter 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; } visited[1]=1; printf("\n"); while(ne<n) { for(i=1,min=999;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]<min) if(visited[i]!=0) { min=cost[i][j]; a=u=i; b=v=j; } if(visited[u]==0 || visited[v]==0) { printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min); mincost+=min; visited[b]=1; } cost[a][b]=cost[b][a]=999; } printf("\n Minimun cost=%d",mincost); getch();

Output
Enter the number of nodes:5 Enter the adjacency matrix: 0 5 4 4 4 5 0 3 2 4 4 3 0 4 0 4 2 4 0 3 4 4 0 3 0 Edge 1:(1 3) cost:4 Edge 2:(3 2) cost:3 Edge 3:(2 4) cost:2 Edge 4:(4 5) cost:3 Minimum cost12

Das könnte Ihnen auch gefallen