You are on page 1of 7

DEPARTMENT OF COMPUTER ENGINEERING

Semester

S.E. Semester IV Computer Engineering

Subject

Analysis of Algorithms

Lectures Professor In-charge

Prof. Sanjeev Dwivedi

Practicals Professor In-Charge

Prof. Ashwin Ganesan

Laboratory number

L07D

Student Name

Sakharam S. Gawade

Roll Number

14102C2015
Subject

Grade

Teachers

Signature

Experiment

02

Number
Experiment Title
Resources

To analyze merge sort and quick Sort


/

Computer, Code::Blocks(IDE)

Apparatus Used
Objectives
Set/

(Skill Algorithms of quick and merge sort, C

Knowledge

tested/imparted)

Description of the experiment:


a) Quicksort is a divide and conquer algorithm. Quicksort first divides a large array

into two smaller sub-arrays: the low elements and the high elements. Quicksort
can then recursively sort the sub-arrays.

The steps are:


1. Pick an element, called a pivot, from the array.
2. Reorder the array so that all elements with values less than the pivot come before
the pivot, while all elements with values greater than the pivot come after it
(equal values can go either way). After this partitioning, the pivot is in its final
position. This is called the partition operation.
3. Recursively apply the above steps to the sub-array of elements with smaller
values and separately to the sub-array of elements with greater values.
quicksort(A, lo, hi):
if lo < hi:
p := partition(A, lo, hi)
quicksort(A, lo, p - 1)
quicksort(A, p + 1, hi)

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

int partition(int a[127610],int p,int q)


{
int i,j,x,temp;
i=p;
j=q;
x=a[p];
while(i<j)
{
while(a[i]<=x && i<=q)
i++;
while(a[j]>x)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[p];
a[p]=a[j];
a[j]=temp;

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.

You might also like