0% found this document useful (0 votes)
41 views8 pages

CS Experiment

The document describes 3 programs: 1. Implements quicksort to sort elements and measure time complexity for different input sizes. 2. Implements merge sort to sort elements and measure time complexity for different input sizes. 3. Implements binary search using divide and conquer to search for a key in a sorted array.

Uploaded by

Ranjith P V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

CS Experiment

The document describes 3 programs: 1. Implements quicksort to sort elements and measure time complexity for different input sizes. 2. Implements merge sort to sort elements and measure time complexity for different input sizes. 3. Implements binary search using divide and conquer to search for a key in a sorted array.

Uploaded by

Ranjith P V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Program no: 1

Sort a given set of elements using the quick sort method and determine the
time required to sort the element. Repeat the experiment for different values
of n.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#define size 500
void quick(int a[size],int,int);
int partition(int a[size],int,int);
void swap(int a[size],int *,int *);
int n;
int main()
{
clock_t begin,end;
int i,a[size];
double timetaken;
clrscr();
begin=clock();
printf("Sorting the element using sort");
printf("\nEnter the number of elements:");
scanf("%d",&n);
printf("\nEnter the element:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick(a,0,n-1);
printf("\nSorted elements are:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
end=clock();
timetaken=((double)(end-begin))/CLK_TCK;
printf("\nTotal time taken to sort the element is=%lf",timetaken);
getch();
return 0;
}
int partition(int a[size],int low,int high)
{
int pivot=a[low],i=low,j=high;
while(i<=j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
swap(a,&i,&j);
}
swap(a,&low,&j);
return j;
}
void swap(int a[size],int *i,int *j)
{
int temp=a[*i];
a[*i]=a[*j];
a[*j]=temp;
}
void quick(int a[size],int low,int high)
{
int m,i;
if(low<high)
{
m=partition(a,low,high);
quick(a,low,m-1);
quick(a,m+1,high);
}
}
OUTPUT
Program no: 2

Sort a given set of elements using merge sort method and determine the time
r.equired to sort the elements. Repeat the experiment for different of values
of n
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
void merge(int a[],int l,int m,int r)
{
int i,j,k;
int n1=m-l+1;
int n2=r-m;
int L[50],R[50];
for(i=0;i<n1;i++)
L[i]=a[l+i];
for(j=0;j<n2;j++)
R[j]=a[m+1+j];
i=0;
j=0;
k=l;
while(i<n1&&j<n2)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
a[k]=L[i];
i++;
k++;
}
while(j<n2)
{
a[k]=R[j];
j++;
k++;
}
}
void mergesort(int a[],int l,int r)
{
if(l<r)
{
int m=(l+r)/2;
mergesort(a,l,m);
mergesort(a,m+1,r);
merge(a,l,m,r);
}
}
void printarray(int a[],int size)
{
int i;
for(i=0;i<size;i++)
printf("%d\t",a[i]);
printf("\n");
}
int main()
{
clock_t begin,end;
double timetaken;
int a[]={12,10,25,65,9,69,99,72,1,45};
int array_size=sizeof(a)/sizeof(a[0]);
begin=clock();
printf("Given array is\n");
printarray(a,array_size);
mergesort(a,0,array_size-1);
printf("Sorted array is\n");
printarray(a,array_size);
delay(1000000);
end=clock();
timetaken=((double)(end-begin))/CLK_TCK;
printf("Total time taken is:%lf",timetaken);
return (0);
}
OUTPUT
Program no: 8

Write a program to implement binary search using divide and conquer


technique.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int a[20],i,n,key;
int low,mid,high;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the array element:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to be searched:");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("key element %d found at A[%d] position\n",key,mid);
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(low>high)
printf("key element is not found");
getch();
}
OUTPUT

You might also like