You are on page 1of 13

Exp1.

Write a program to implement recursive Binary and Linear Search


#include<stdio.h>
#include<conio.h>
int count=0;
int bsearch(int a[],int lb,int ub,int n)
{
int mid;
if(lb<=ub)
{
count++;
mid=(lb+ub)/2;
if(a[mid]==n)
return mid;
else if(a[mid]<n)
return bsearch(a,mid+1,ub,n);
else
return bsearch(a,lb,mid-1,n);
}
else
return-1;
}
void linear(int a[],int n,int size)
{
int flag=0,c=0,i=0;
for(i=0;i<size;i++)
{
c++;
if(a[i]==n)
{
flag=1;
break;
}
}
if(flag==1)
printf(\n Element found at position: %d,i+1);
else
printf(\n Element not found);
printf(\n Complexity of linear search: %d,c);
}
void main()
{
clrscr();
int a[100],size,I,j,temp,pos;

printf(Enter the size of the array: );


scanf(%d,&size);
printf(Enter the elements of the array: \n);
for(i=0;i<size;i++)
{
scanf(%d,&a[i]);
}
printf(\n Enter the element to be searched: );
scanf(%d,&n);
printf(\n Linear Search);
linear(a,n,size);
printf(\n Binary Search);
for(i=0;i<size;i++)
{
for(j=j+1;j<size;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
pos=bsearch(a,0,size-1,n);
if(pos==-1)
printf(\n Element not found);
else
printf(\n Element found at position: %d,pos+1);
printf(\n Complexity of Binary Search: %d,count);
getch();
}

Exp2. Write a program to implement Heap Search


#include<stdio.h>
#include<conio.h>
#define MAX 10
void RestoreHeapUp(int *Heap,int index)
{
int val=Heap[index];
while((index>1)&&(Heap[index/2]<val)
{
Heap[index]=Heap[index/2];
index/=2;
}
Heap[index]=val;
}
void RestoreHeapDown(int *Heap,int index,int n)
{
int val=Heap[index];
int j=index*2;
while(j<=n)
{
if((j<n)&&(Heap[j]<Heap[j+1]))
j++;
if(Heap[j]<Heap[j/2])
break;
Heap[j/2]=Heap[j];
j=j*2;
}
Heap[j/2]=val;
}
void main()
{
int Heap[MAX},n,i,j;
clrscr();
printf(\n Enter the no. of elements: );
scanf(%d,&n);
printf(\n Enter the elements: );
for(i=1;i<=n;i++)
{
scanf(%d,&Heap[i]);
RestoreHeapUp(Heap,i);
}
j=n;
for(i=1;i<=j;i++)
{
int temp;
temp=Heap[1];

Heap[1]=Heap[n];
Heap[n]=temp;
n=n-1;
RestoreHeapDown(Heap,1,n);
}
n=j;
printf(\n The sorted elements are: \n);
for(i=1;i<=n;i++)
printf(%d\t,Heap[i]);
getch();
}

Exp3. Write a program to implement Merge Sort


#include<stdio.h>
#include<conio.h>
int comp=0;
void merge(int a[],int beg,int mid,int end)
{
int i=beg,j=mid+1,index=beg,tmp[10],k;
while((i<=mid)&&(j<=end))
{
comp++;
if(a[i]<a[j]
{
tmp[index]=a[i];
i++;
}
else
{
tmp[index]=a[j];
j++;
}
index++;
}
if(i>mid)
{
while(j<=end)
{
comp++;
tmp[index]=a[j];
j++;
index++;
}
}
else
{
while(i<=mid)
{
comp++;
tmp[index]=a[i];
i++;
index++;
}
}
for(k=beg;k<index;k++)
a[k]=tmp[k];
}

void mergeSort(int a[],int beg,int end)


{
int mid;
if(beg<end)
{
mid=(beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
}
void main()
{
clrscr();
int a[10],i,n;
printf(Enter the size of the array: );
scanf(%d,&n);
printf(Enter the elements of the array: \n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
mergeSort(a,0,n-1);
printf(\n Sorted array: \n);
for(i=0;i<n;i++)
printf(%d\t,a[i]);
printf(\n Complexity is: %d,comp);
getch();
}

Exp4. Write a program to implement Selection Sort


#include<stdio.h>
#include<conio.h>
int comp=0;
int smallest(int a[],int k,int n)
{
int pos=k,small=a[k],I;
for(i=k+1;i<n;i++)
{
comp++;
if(a[i]<small)
{
small=a[i];
pos=i;
}
}
return pos;
}
void selectionSort(int a[],int n)
{
int k,pos,tmp;
for(k=0;k<n;k++)
{
comp++;
pos=smallest(a,k,n);
tmp=a[k];
a[k]=a[pos];
a[pos]=tmp;
}
}
void main()
{
int a[100],n,I;
clrscr();
printf(Enter the size of the array: );
scanf(%d,&n);
printf(Enter the elements of the array: \n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
selectionSort(a,n);
printf(\n Sorted array: \n);
for(i=0;i<n;i++)
printf(%d\t,a[i]);
printf(\n Complexity is: %d,comp);
getch();
}

Exp5. Write a program to implement Insertion Sort


#include<stdio.h>
#include<conio.h>
int comp=0;
void insertionSort(int a[],int n)
{
int i,j;,temp;
for(i=1;i<n;i++)
{
temp=a[i];
j=j-1;
while((temp<a[j])&&(j>=0))
{
comp++;
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
void main()
{
int a[100],i,n;
clrscr();
printf(Enter the size of the array: );
scanf(%d,&n);
printf(Enter the elements of the array: \n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
insertionSort(a,n);
printf(\n Sorted array: \n);
for(i=0;i<n;i++)
printf(%d\t,a[i]);
printf(\n Complexity is: %d,comp);
getch();
}

Exp6. Write a program to implement Quick Sort


#include<stdio.h>
#include<conio.h>
int comp=0;
int partition(int a[],int beg,int end)
{
int left,right,loc,tmp,flag=0;
loc=beg;
left=beg;
right=end;
while(flag!=1)
{
comp++;
while((a[loc]<=a[right])&&(loc!=right))
right--;
if(loc==right)
flag=1;
else if(a[loc]>a[right])
{
tmp=a[loc];
a[loc]=a[right];
a[right]=tmp;
loc=right;
}
if(flag!=1)
{
while((a[loc]>=a[left])&&(loc!=left))
left++;
if(loc==left)
flag=1;
else if(a[loc]<a[left])
{
tmp=a[loc];
a[loc]=a[left];
a[left]=tmp;
}
}
}
return loc;
}
void quicksort(int a[],int beg,int end)
{
int loc;
if(beg<end)
{
loc=partition(a,beg,end);

quickSort(a,beg,loc-1);
quicksort(a,loc+1,end);
}
}

void main()
{
clrscr();
int a[10],i,n;
printf(Enter the size of the array: );
scanf(%d,&n);
printf(Enter the elements of the array: \n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
quickSort(a,0,n-1);
printf(\n Sorted array: \n);
for(i=0;i<n;i++)
printf(%d\t,a[i]);
printf(\n Complexity is: %d,comp);
getch();
}

Exp7. Write a program to implement Counting Sort


#include<stdio.h>
#include<conio.h>
int comp=0;
void countingSort(int a[],int k,int n)
{
int i,j,b[20],c[100];
for(i=0;i<=k;i++)
c[i]=0;
for(j=1;j<=n;j++)
c[a[j]]=c[a[j]]+1;
for(i=1;i<=k;i++)
c[i]=c[i]+c[i-1];
for(j=n;j>=1;j--)
{
b[c[a[j]]]=a[j];
c[a[j]]=c[a[j]]-1;
comp++;
}
printf(Sorted Array is: \n);
for(i=1;i<=n;i++)
printf(%d\t,b[i]);
printf(\nComplexity is: %d,comp);
}

void main()
{
clrscr();
int n,i,k=0,a[20];
printf(\n Enter no. of elements: \n);
scanf(%d,&n);
printf(\n Enter elements to be sorted: \n);
for(i=1;i<n;i++)
{
scanf(%d,&a[i]);
if(a[i]>k)
k=a[i];
}
countingSort(a,k,n);
getch();
}

Exp8. Write a program to implement Radix Sort


#include<stdio.h>
#include<conio.h>
int comp=0;
int largest(int a[],int n)
{
int i,large=a[0];
for(i=1;i<n;i++)
{
comp++;
if(a[i]>large)
large=a[i];
}
return large;
}
void radixSort(int a[],int n)
{
int i,j,k,rem,NOP=0,divisor=1,large,pass;
int bCount[10],b[10][10];
large=largest(a,n);
while(large>0)
{
NOP++;
large/=10;
}
for(pass=0;pass<NOP;pass++)
{
for(i=0;i<10;i++)
bCount[i]=0;
for(i=0;i<n;i++)
{
rem=(a[i]/divisor)%10;
b[rem][bCount[rem]]=a[i];
bCount[rem]+=1;
comp++;
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bCount[k];j++)
{
comp++;
a[i]=b[k][j];
i++;
}
}

divisor*=10;
}
}

void main()
{
int i,n,a[10];
clrscr();
printf(Enter the size of the array: );
scanf(%d,&n);
printf(Enter the elements of the array: \n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
radixSort(a,n);
printf(\n Sorted array: \n);
for(i=0;i<n;i++)
printf(%d\t,a[i]);
printf(\n Complexity is: %d,comp);
getch();
}

You might also like