Escolar Documentos
Profissional Documentos
Cultura Documentos
Tanmay Baid
Roll
Number: 0829cs071112
Bubble Sort
# include<stdio.h>
# include<conio.h>
void main()
{
int a[5], i, j, temp;
clrscr();
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;
}
}
}
getch();
}
1
Name:
Tanmay Baid
Roll
Number: 0829cs071112
Output
2
Name:
Tanmay Baid
Roll
Number: 0829cs071112
Merge Sort
#include<stdio.h>
#include<conio.h>
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<=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
5
Name:
Tanmay Baid
Roll
Number: 0829cs071112
#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
7
Name:
Tanmay Baid
Roll
Number: 0829cs071112
#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();
}
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
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();
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();
}
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
12
Name:
Tanmay Baid
Roll
Number: 0829cs071112
Quick Sort
#include<stdio.h>
#include<conio.h>
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();
quicksort(a,l,h);
getch();
}
14
Name:
Tanmay Baid
Roll
Number: 0829cs071112
Output
15
Name:
Tanmay Baid
Roll
Number: 0829cs071112
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];
}
Output
17
Name:
Tanmay Baid
Roll
Number: 0829cs071112
25
38
95
16
24
31
Knapsack
18
Name:
Tanmay Baid
Roll
Number: 0829cs071112
#include <stdio.h>
#define MAXWEIGHT 100
void fill_sack()
{
int a[MAXWEIGHT]; /* a[i] holds the maximum value that can be obtained
using at most i weight */
int last_added[MAXWEIGHT]; /* I use this to calculate which object were
added */
int i, j;
int aux;
a[0] = 0;
for (i = 1; i <= W; ++i)
for (j = 0; j < n; ++j)
if ((c[j] <= i) && (a[i] < a[i - c[j]] + v[j]))
{
a[i] = a[i - c[j]] + v[j];
last_added[i] = j;
}
printf("---\n");
aux = W;
while ((aux > 0) && (last_added[aux] != -1))
{
printf("Added object %d (%d$ %dKg). Space left: %d\n", last_added[aux] + 1,
v[last_added[aux]], c[last_added[aux]], aux - c[last_added[aux]]);
aux -= c[last_added[aux]];
19
Name:
Tanmay Baid
Roll
Number: 0829cs071112
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);
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
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
28