Professional Documents
Culture Documents
Semester
Subject
Analysis of Algorithms
Laboratory number
L07D
Student Name
Sakharam S. Gawade
Roll Number
14102C2015
Subject
Grade
Teachers
Signature
Experiment
02
Number
Experiment Title
Resources
Computer, Code::Blocks(IDE)
Apparatus Used
Objectives
Set/
Knowledge
tested/imparted)
into two smaller sub-arrays: the low elements and the high elements. Quicksort
can then recursively sort the sub-arrays.
Quick sort has worst-case (n2) and average complexity as O(n logn), where n is the
number of items being sorted.
b) MergeSort is a Divide and Conquer algorithm. It divides input array in two halves,
calls itself for the two halves and then merges the two sorted halves.
Conceptually, a merge sort works as follows:
1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1
element is considered sorted).
2. Repeatedly merge sublists to produce new sorted sublists until there is only 1
sublist remaining. This will be the sorted list.
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Merge sort has worst-case and average complexity both (n logn), where n is the
number of items being sorted.
Program:
/* Find time complexity of merge and quick sort */
#include<stdio.h>
#include<time.h>
//Partition
element
function
used
to
divide
the
data
into
two
parts
using
pivot
return j;
}
//Sorts the data by recursively calling itself which in turn calls partition.
void quicksort(int a[127610],int left, int right)
{
int p;
if(left<right)
{
p=partition(a,left,right);
quicksort(a,left,p-1);
quicksort(a,p+1,right);
}
}
//Merges the data of partitions
void simplemerge(int a[127610], int f, int s,int t)
{
int i,j,w,k;
int temp[127610];
k=-1;
i=f;
j=s;
while(i<=s-1 && j<=t)
{
if(a[i]<a[j])
{
temp[++k]=a[i];
i++;
}
else
{
temp[++k]=a[j];
j++;
}
}
if(i>s-1)
{
for(w=j;w<=t;w++)
temp[++k]=a[w];
}
else
{
for(w=i;w<=s-1;w++)
temp[++k]=a[w];
}
for(w=0;w<=k;w++)
a[f+w]=temp[w];
}
//Divides the data into two equal parts recursively and calls the sorting
function
void merge_sort(int a[127610],int left,int right)
{
int mid;
if(left<right)
{
mid=(left+right)/2;
merge_sort(a,left,mid);
merge_sort(a,mid+1,right);
simplemerge(a,left,mid+1,right);
}
}
void main()
{
FILE *f;
clock_t t;
double time_taken;
long i,n=10;
int a[127610];
printf("No of I/P \t Quick Sort \t\t Merge Sort \n\n");
f=fopen("GenRan.txt","r");
while(n<=100000)
{
printf("%d\t\t",n);
for(i=0;i<n;i++)
fscanf(f,"%d",&a[i]);
//Time Complexity of quick sort
t=clock();
quicksort(a,0,n);
t=clock()-t;
time_taken=((double)t)/CLOCKS_PER_SEC;
printf("%f\t\t",time_taken);
for(i=0;i<n;i++)
fscanf(f,"%d",&a[i]);
//Time Complexity of merge sort
t=clock();
merge_sort(a,0,n-1);
t=clock()-t;
time_taken=((double)t)/CLOCKS_PER_SEC;
printf("%f\t\t\n",time_taken);
n=n*10;
}
}
Output run:
Conclusions:
We analyzed two divide and conquer sorting algorithms quick and merge sort and we
can conclude that quick sort is faster than merge sort. They have the same complexity. If
the set of data is already sorted then quick sort takes lot of time. Quick Sort is said to be
quick because of its less memory space usage.