Uploaded by kiddobackbencher740

CSE222 LAB MID AS 022

advertisement
Experiment: Write a program to compare the complexity analysis of
Merge, Heap and Quick Sort
1) Theoretical Analysis:
Quick Sort:
Best Case
The data elements are partitioned uniformly, such a pivot
is selected which divides data elements into two equal
parts. So, the time complexity T(n) would be:
 T(n)= 2T(n/2)+Cn, solving it we get,
 T(n)= O(nlgn)
Suppose 60% of the data elements are on the right side of
the pivot and rest 40% on the left. So T(n) would be:
 T(n)= 6T(n/10)+4T(n/10)+Cn, solving it we get,
 T(n)= O(nlgn)
Worst Case The data is not partitioned uniformly, pivot selected is an
extreme large or small value. This means if there are n
elements, the pivot partitions the data into (n-1) elements.
 T(n) = T(n-1)+Cn
=T(n-2)+C(n-1)+Cn….=T(1)+C*2+C*3+…+Cn,
solving it we get,
 T(n)=O(n2)
Average
razia
Merge Sort:
Base Case
Worst
Case
The array is sorted in this case, merge sort divides array
in two halves taking linear time to merge two halves so
T(n)= O(nlgn), which is the same for its average and
worst case as well.
In every iteration, we divide the elements into further 2
sub elements. Leading lg n operations and for n iteration
resulting in n log n operations. So T(n)= O(nlogn)
Heap Sort:
Base Case
Best case occurs when all elements are equal,
 T(n)= Ω(n log n)
Worst and
Average
Case
Construct_heap (A, n) :- nlgn
for(i=0;i<n-1;i++)
:- n
delete(A, n-i)
:- (n-1)*lgn
Total complexity for Heap_Sort (A[],n):
 T(n)=nlgn+n+(n-1)lgn
 T(n) =O(nlgn)
From the theoretical analysis we can see that heap sort has a smaller
time complexity, it has worst case complexity of O(nlgn) that is much
smaller than n2, which is the normal case complexity for many other
sorting algorithms.
razia
2) Source Code in C language:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void Merge_sort( int A[], int p, int r);
void merge(int A[], int p, int q, int r);
void quicksort(int A1[], int left, int right);
int partition(int A1[], int left,int right);
void delete(int A2[], int n);
int A[100000], L[500000],R[500000] ;
int A1[100000];
int A2[100000];
int main ()
{
time_t s,e;
int t,n,i,p,j,temp=0;
printf("No of elements: ");
scanf("%d", &n);
for(i=0; i<n; i++)
A[i]=1+rand()%1000;
printf("\nAlgorithm Name\t\tExecution time(ms)");
printf("\n------------------------------------");
s= clock();
Merge_sort(A,0,n-1);
e= clock();
t= (e-s)*1000/CLOCKS_PER_SEC;
printf("\nMerge sort\t\t%d", t);
for(i=0; i<n;i++){
A1[i]=1+rand()%1000;
razia
}
s= clock();
quicksort(A1,0,n-1);
e= clock();
t= (e-s)*1000/CLOCKS_PER_SEC;
printf("\nQuick sort\t\t%d", t);
srand(time(NULL));
for(i=0; i<n;i++){
A2[i]=1+rand()%n;
}
s= clock();
for(j=0; j<n;j++){
i=j;
while(i>0){
p=(i-1)/2;
if (A2[p]>A2[i]) break;
else {
temp=A2[i];
A2[i]=A2[p];
A2[p]=temp;
i=p;}
}
}
for(i=n; i>1;i--){
delete(A2,i);
}
e= clock();
t= (e-s)*1000/CLOCKS_PER_SEC;
printf("\nHeap sort\t\t%d", t);
}
razia
void quicksort(int A1[], int left,int right){
int pi;
if(left>=right) return;
pi= partition(A1,left, right);
quicksort(A1,left,pi-1);
quicksort(A1,pi+1,right);}
int partition(int A1[], int left,int right){
int pivot,i,j,temp=0;
pivot=A1[right];
i=left-1;
for(j=left;j<right;j++)
{
if(A1[j]<pivot){
i++;
temp=A1[i];
A1[i]=A1[j];
A1[j]=temp;}
}
i++;
temp=A1[i];
A1[i]=A1[right];
A1[right]=temp;
return i;
}
void Merge_sort (int A[], int p, int r)
{
int q;
if (p>=r)
return 0;
q=(p+r)/2;
razia
Merge_sort (A,p,q);
Merge_sort (A,q+1,r);
merge(A,p,q,r);
}
void merge(int A[], int p, int q, int r)
{
int n1,n2,i,j,k;
n1=q-p+1;
n2=r-q;
for(i=0; i<n1; i++)
L[i]=A[p+i];
for(i=0; i<n2; i++)
R[i]=A[q+i+1];
i=0;
j=0;
k=p;
while(i!=n1&&j!=n2)
{
if(L[i]<R[j])
{
A[k++]=L[i++];}
else
{
A[k++]= R[j++];
}
}
if(i<n1){
for(j=i; j<n1; j++)
A[k++]= L[j];
}
else{
razia
for(i=j; i<n2; i++)
A[k++]= R[i];
}
}
void delete(int A2[], int n){
int l,r,i, parent, largest=0, temp=0;
temp=A2[0];
A2[0]=A2[n-1];
A2[n-1]=temp;
n--;
i=0;
while(i<n/2){
l= 2*i+1;
r= 2*i+2;
if(l<n&&A2[l]>A2[i])
largest=l;
else largest=i;
if (r<n&&A2[r]>A2[largest]){
largest=r;
}
if(i==largest) break;
temp=A2[i];
A2[i]=A2[largest];
A2[largest]=temp;
i=largest;
}
return 0;
}
razia
3) Sample Output:
4) Table
No of elements
Merge Sort
Quick Sort
Heap Sort
Execution Time Execution Time Execution Time
in ms
in ms
in ms
10000
9
4
4
30000
18
18
13
40000
23
18
15
50000
29
31
17
60000
33
44
26
razia
5) Graph:
50
45
40
35
30
25
20
15
10
5
0
10000
30000
40000
Merge
Quick
50000
60000
Heap
X axis represents No of elements
Y axis represents Execution time of Merge, Heap, Quick Sort (ms)
From the above table and graph we can conclude that the practical
output satisfies the theoretical analysis, that is, execution time of
heap sort is smallest than the other two sorting algorithm. Merge
sort has a relatively higher execution time than quick, heap sort, as
number of data increases execution time increases. In case of quick
sort, randomized or Median of Median algorithm was not applied,
so there were possibility of imbalanced partition leading the
execution time of Quick sort to be comparatively higher.
razia
Download