You are on page 1of 28

Name: Tanmay Baid Roll Number: 0829cs071112

Bubble Sort
# include<stdio.h> # include<conio.h> void main() { int a[5], i, j, temp; clrscr(); printf("Enter 5 elements of array: "); for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { for(j=0;j<4;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("Sorted array: "); for(i=0;i<5;i++) { printf("\n%d",a[i]); } getch(); }

1

Name: Tanmay Baid Roll Number: 0829cs071112

Output
Enter 5 elements of array: 1 8 5 2 7 Sorted array: 1 2 5 7 8

2

Name: Tanmay Baid Roll Number: 0829cs071112

Merge Sort
#include<stdio.h> #include<conio.h> void mergesort(int x[],int n) { int aux[10],i,j,k,l1,u1,l2,u2,size; size=1; while(size<n) { l1=0; k=0; while(l1+size<n) { l2=l1+size; u1=l2-1; if(l2+size-1<n) u2=l2+size-1; else u2=n-1; for(i=l1,j=l2;i<=u1 && j<=u2;k++) { if(x[i]<x[j]) aux[k]=x[i++]; else aux[k]=x[j++]; } for(;i<=u1;k++) aux[k]=x[i++]; for(;j<=u2;k++) aux[k]=x[j++]; l1=u2+1; }

3

Name: Tanmay Baid Roll Number: 0829cs071112
for(i=l1;k<n;k++) aux[k++]=x[i]; for(i=0;i<n;i++) x[i]=aux[i]; size++; } } void main() { clrscr(); int a[10],i,n; printf("\n How many elements you want to enter in array:-"); scanf("%d",&n); printf("\n Enter the elements of array:-"); for(i=0;i<n;i++) scanf("\n%d",&a[i]); mergesort(a,n); printf("\n The sorted array is:-"); for(i=0;i<n;i++) printf("\n%d",a[i]); getch(); }

4

Name: Tanmay Baid Roll Number: 0829cs071112

Output How many elements you want to enter in array:-5 Enter the elements of array:5 3 4 2 1 The sorted array is:1 2 3 4 5

5

Name: Tanmay Baid Roll Number: 0829cs071112

Recursive Binary Search
#include<stdio.h> #include<conio.h> void main() { int Binsrch(int [],int,int,int); int a[100],i,n,item,res; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } printf("Enter the element to be searched:"); scanf("%d",&item); res=Binsrch(a,0,n-1,item); if(res==0) printf("The element is not present in the array"); else printf("The element is found at position=%d",res+1); getch(); } int Binsrch(int a[],int i,int l, int x) { int mid; if(l==i) { if(x==a[i])

6

Name: Tanmay Baid Roll Number: 0829cs071112
return i; else return 0; } else { mid=(i+l)/2; if(x==a[mid]) return mid; else if(x<a[mid]) return Binsrch(a,i,mid-1,x); else return Binsrch(a,mid+1,l,x); } } Output How many elements:5 Enter the element of the array: 12 13 14 16 18 Enter the element to be searched: 16 The element is found at position=4 How many elements:5 Enter the element of the array: 12 13 14 16 18 Enter the element to be searched: 17 The element is not present in the array

7

Name: Tanmay Baid Roll Number: 0829cs071112

Iterative Binary Search
#include<stdio.h> #include<conio.h> void main() { int Binsrch(int [],int,int); int a[100],i,n,item,res; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } printf("Enter the element to be searched:"); scanf("%d",&item); res=Binsrch(a,n-1,item); if(res==0) printf("The element is not present in the array");

8

Name: Tanmay Baid Roll Number: 0829cs071112
else printf("The element is found at position=%d",res+1); getch(); } int Binsrch(int a[],int n,int x) { int mid; int low=0,high=n; while(low<=high) { mid=(low+high)/2; if(x<a[mid]) high=mid-1; else if(x>a[mid]) low=mid+1; else return mid; } return 0; } Output How many elements:5 Enter the element of the array: 11 21 31 41 51 Enter the element to be searched: 31 The element is found at position=3

9

Name: Tanmay Baid Roll Number: 0829cs071112

Heap Sort
#include<stdio.h> #include<conio.h> int a[100],i,j; void Heapify(int [],int); void HeapSort(int [],int); void Adjust(int [],int,int); void main() { int n; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) {

10

Name: Tanmay Baid Roll Number: 0829cs071112
scanf("%d",&a[i]); } HeapSort(a,n); printf("THE SORTED ARRAY IS:\n"); for(i=0;i<=n-1;i++) { printf("%d\t",a[i]); } getch(); } void HeapSort(int a[],int n) { int t; Heapify(a,n); for(i=n-1;i>=1;i--) { t=a[i]; a[i]=a[0]; a[0]=t; Adjust(a,0,i-1); } }

void Heapify(int a[],int n) { for(i=(n-1)/2;i>=0;i--) Adjust(a,i,n); } void Adjust(int a[],int i,int n) { int item; j=2*i; item=a[i]; while(j<=n-1) { if((j<=n-1)&&(a[j]<a[j+1])) j=j+1; if(item>=a[j]) break; a[j/2]=a[j];

11

Name: Tanmay Baid Roll Number: 0829cs071112
j=2*j; } a[j/2]=item; }

Output How many elements:6 Enter the element of the array: 85 56 69 21 10 25 THE SORTED ARRAY IS: 10 21 25 56 69 85

12

Name: Tanmay Baid Roll Number: 0829cs071112

Quick Sort
#include<stdio.h> #include<conio.h> void quicksort(int a[],int l,int h) { int temp,low,high,key; low=l; high=h; key=a[(low+high)/2];

13

Name: Tanmay Baid Roll Number: 0829cs071112
while(low<=high) { while(a[low]<key) low++; while(a[high]>key) high--; if(low<=high) { temp=a[low]; a[low]=a[high]; a[high]=temp; low++; high--; } } if(l<high) quicksort(a,l,high); if(low<h) quicksort(a,low,h); } void main() { int i,j,a[10],n,l,h; clrscr(); printf("\n How many elements you want to enter in array:-"); scanf("%d",&n); printf("\n Enter the elements of array:-"); for(i=0;i<n;i++) scanf("\n%d",&a[i]); l=0; h=n-1; quicksort(a,l,h); printf("\n The sorted array is:-"); for(i=0;i<n;i++) printf("\n %d",a[i]); getch(); }

14

Name: Tanmay Baid Roll Number: 0829cs071112

Output
How many elements you want to enter in array:-5 Enter the elements of array:5 3 4 2 1

15

Name: Tanmay Baid Roll Number: 0829cs071112
The sorted array is:1 2 3 4 5

Max-Min
#include<stdio.h> #include<conio.h> int max,min; void main()

16

Name: Tanmay Baid Roll Number: 0829cs071112
{ void StraightMaxMin(int [],int,int,int); int a[100],i,n; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } max=min=a[0]; StraightMaxMin(a,n,max,min); getch(); } void StraightMaxMin(int a[],int n,int max,int min) { int i; for(i=1;i<=n-1;i++) { if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; } printf("THE MAXIMUM ELEMENT IN THE ARRAY IS:%d",max); printf("\nTHE MINIMUM ELEMENT IN THE ARRAY IS:%d",min); }

Output
How many elements:7 Enter the elements of the array: 54

17

Name: Tanmay Baid Roll Number: 0829cs071112
25 38 95 16 24 31 THE MAXIMUM ELEMENT IN THE ARRAY: 95 THE MINIMUM ELEMENT IN THE ARRAY: 16

Knapsack

18

Name: Tanmay Baid Roll Number: 0829cs071112

19

Name: Tanmay Baid Roll Number: 0829cs071112
} printf("Total value added: %d\$\n", a[W]); } void main(int argc, char *argv[]) { fill_sack(); }

20

Name: Tanmay Baid Roll Number: 0829cs071112

Output

21

Name: Tanmay Baid Roll Number: 0829cs071112

Multiplication of Matrix
#include<stdio.h> #include<conio.h> int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; void arr_prod(int [10][10],int [10][10]); void main() { printf("ENTER THE NO OF ROWS OF MATRIX A:"); scanf("%d",&m); printf("ENTER THE NO OF COLUMNS OF MATRIX A:"); scanf("%d",&n); printf("ENTER THE NO OF ROWS OF MATRIX B:"); scanf("%d",&p); printf("ENTER THE NO OF COLUMNS OF MATRIX B:"); scanf("%d",&q); if(n!=p) printf("\nMARTIX MULTIPLICATION NOT POSSIBLE"); else { printf("\nENTER ELEMENTS FOR FIRST MATRIX:\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { scanf("%d",&a[i][j]); } } printf("\nENTER ELEMENTS FOR SECOND MATRIX:\n"); for(i=0;i<=p-1;i++) { for(j=0;j<=q-1;j++) {

22

Name: Tanmay Baid Roll Number: 0829cs071112
scanf("%d",&b[i][j]); } } arr_prod(a,b); printf("PRODUCT OF MARIX A AND B IN MATRIX FORM IS:\n\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=q-1;j++) { printf("%d\t",c[i][j]); } printf("\n"); } } } void arr_prod(int a[10][10],int b[10][10]) { int k; for(i=0;i<=m-1;i++) { for(j=0;j<=q-1;j++) { c[i][j]=0; for(k=0;k<=p-1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } }

23

Name: Tanmay Baid Roll Number: 0829cs071112

Output

24

Name: Tanmay Baid Roll Number: 0829cs071112

Factorial
#include<stdio.h> #include<conio.h> int fact(int); void main() { int n,result; clrscr(); printf("Enter the number: "); scanf("%d",&n); result=fact(n); printf("\nThe factorial of %d is: %d",n,result); getch(); } int fact(int x) { int f; if(x==1) { return(x); } else { return(x*fact(x-1)); } }

25

Name: Tanmay Baid Roll Number: 0829cs071112

Output
Enter the number: 5 The factorial of 5 is:120

26

Name: Tanmay Baid Roll Number: 0829cs071112

Fibonacci
#include<stdio.h> #include<conio.h> int fibnum(int); void main() { int p,num; clrscr(); printf("Enter the position of element you want from Fibonacci series: "); scanf("%d",&p); num=fibnum(p); printf("The required Fibonacci number is: %d",num); getch(); } int fibnum(int p) { if(p==1||p==0) { return(p); } else { return(fibnum(p-1)+fibnum(p-2)); } }

27

Name: Tanmay Baid Roll Number: 0829cs071112

Output
Enter the position of element you want from Fibonacci series: 6 The required Fibonacci number is: 8

28