You are on page 1of 24

Design and Analysis of Algorithm

Name:- Rishikesh Kumar


Roll No:- 20191030
Assignment-1

-****************-
Q1) Impement the merge sort algorithm using C program.
Program:-
#include<stdio.h>
void merge(int A[],int n1,int index1,int B[],int n2,int index2,int c[],int index)
{
while(n1&&n2)
{
if(A[index1]<B[index2])
{
c[index]=A[index1];
index++;
index1++;
n1--;
}
else
{
c[index]=B[index2];
index++;
index2++;
n2--;
}
}
while(n1)
{
c[index]=A[index1];
index++;
index1++;
n1--;
}
while(n2)
{
c[index]=B[index2];
index++;
index2++;
n2--;
}
}
void mergepass(int A[],int N,int L,int B[])
{
int j,LB;
int Q,S,R;
Q=N/(2*L);
S=2*L*Q;
R=N-S;
for(j=0;j<Q;j++){
LB=(2*j)*L;
merge(A,L,LB,A,L,LB+L,B,LB);
}
if(R<=L)
{
for(j=0;j<R;j++)
{
B[S+j]=A[S+j];
}
}
else{
merge(A,L,S,A,R-L,S+L,B,S);
}
}
void merge_sort(int A[],int N)
{
int L=1,B[50];
while(L<N)
{
mergepass(A,N,L,B);
mergepass(B,N,2*L,A);
L=4*L;
}
}
int main(void)
{
int i;
int a[]={11,66,88,33,66,77,99,88,22,44,55};
for(i=0;i<=10;i++)
{
printf("%d ",a[i]);
}
merge_sort(a,11);
printf("\n");
for(i=0;i<=10;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}

Analysis:-
 We tend to use Divide and Conquer approach in Merge sort algorithm.
 In the Divide and Conquer approach, we first divide the array into
smaller subarrays and then merge them together to get the sorted array.
 With the function mergepass we break the array into smaller subarray.
 With the function merge we sort the smaller array and with merge_sort
we combine back the array together in sorted order.
 Time complexibility is O(n log(n)) time in all the three cases(Best case,
Worst case and Average case).
 The overall running time of Merge sort are
T(n)=2T( ) + O(n)+ O(1)
 Worst Space complexity of Merge sort is O(n).

Q2) Compare the Performance of Bubble sort and Merge


sort on same data.
Program:-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)


{
if (l < r) {

int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void bubbleSort(int arr[], int n)


{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n\n");
}

int main()
{
FILE *fn;
int num,n,arr[150000],a,upper,lower;
clock_t start_t, end_t;
double total_t;
printf("Enter the total number of random number : ");
scanf("%d",&n);
printf("Enter lower value: ");
scanf("%d",&lower);
printf("Enter upper value: ");
scanf("%d",&upper);
fn=fopen("number.txt","w");
srand(time(0));
for (int i=0;i<n;i++) {
num=(rand()%(upper-lower+1))+ lower;
fprintf(fn," %d",num);
}
fclose(fn);
fn = fopen("number.txt", "r");
int i;
for (i = 0; i < n; i++)
{
fscanf(fn, "%d", &arr[i]);
}
printArray(arr,n);
printf("\n-------------------------");
while(1)
{
printf("\nPress 1 to Bubble Sort the given array.");
printf("\nPress 2 to Merge Sort the given array.");
printf("\nPress any number to Exit!\n\n");
printf("Enter Your Choice : ");
scanf("%d", &a);
switch(a){
case 1:
start_t=clock();
bubbleSort(arr, n);
end_t=clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken for execution: %f\n\n", total_t);
printf("\nBubble Sorted array:: ");
printArray(arr, n);
break;
case 2:
start_t=clock();
mergeSort(arr, 0, n - 1);
end_t=clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken for execution: %f\n\n", total_t );
printf("\nMerge Sorted array :: ");
printArray(arr, n);
break;
default:
exit(0);
}
}
return 0;
}

Analysis:-
 In order to anayse time we use “time.h” header file and clock() function
to analyse the time taken by merge sort and bubble sort.
 Here, we take random data and store it in the files.
 For a small number of random data the Bubble sort and Merge sort are
almost equal with respect to sorting numbers in approximately the same
time.
 For a large number of random data the time complexity rate is lower in
merge sort as compared to bubble sort.
 Average time complexity of Bubble sort is O( ) and of Merge sort is
O(nlog(n)).
 Worst space complexity of Bubble sort is O(1) and Merge sort is O(n).

Q3) Implement Quick sort Algorithm using C.


Program:-
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}

int quick(int array[], int low, int high) {


int a = array[high];
int b = (low - 1);
for (int i = low; i < high; i++) {
if (array[i] <= a) {
b++;
swap(&array[b], &array[i]);
}
}
swap(&array[b + 1], &array[high]);
return (b + 1);
}
void quickSort(int array[], int low, int high) {
if (low < high) {
int n = quick(array, low, high);
quickSort(array, low, n - 1);
quickSort(array, n + 1, high);
}
}

int main() {
int i;
int array[] = {44,33,11,55,77,90,40,60,99,22,88,66,110,121};

int n = sizeof(array) / sizeof(array[0]);

printf("Unsorted Array\n");
for (int i = 0; i < n; ++i) {
printf("%d ", array[i]);
}
printf("\n");
// perform quicksort on data
quickSort(array, 0, n - 1);

printf("Sorted array in ascending order: \n");


for (int i = 0; i < n; ++i) {
printf("%d ", array[i]);
}
}

Analysis:-
 Quicksort is a sorting algorithm which uses Divide and conquer
approach.
 In Quicksort algorithm first we chooses a pivot and then partition the
array around pivot.
 Here we choose pivot as high and perform partition(quick() function) of
array.
 In the quick() function, we swap the two element with the given
condition.
 At last apply the quicksort() function in recursive form to perform the
sorting element.
 Time complexity of Quicksort when pivot is low and high is O( ) and
when pivot is middle is O(n log(n)) and when pivot is random element
then
T(n) = T(n-i) + i*O(n).
 Worst space complexity of Quick sort is O(log(n)).

Q4) Solve Closest Pair Problem using Brute-Force Approach.


Program:-
#include<stdio.h>
#include<math.h>
void Closest(int a[],int b[],int n)
{
int i,j;
double dist;
double d=INFINITY;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
dist=sqrt((a[i]-b[i])*a[j]-b[j]);
if(dist<d)
{
d=dist;
}
}
}
printf("\nClosest distance=%lf",d);
}
int main()
{
int n,i;
int a[100],b[100];
printf("Enter the number of element in array: ");
scanf("%d",&n);
printf("Enter the element of x coordinate in array a: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element of y coordinate in array b: ");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("The Closest pair point:");
for(i=0;i<n-1;i++)
{
printf("(%d,%d),",a[i],b[i]);
}
printf("(%d,%d)",a[n-1],b[n-1]);
Closest(a,b,n);
}

Analysis:-
 In the given problem we are given point and we have to find the closest
point between them.
 We take an input of point as separate array of x and y coordinate as a
and b.
 Initilise the distance d as Infinity.
 With the two for loop calculate the distance between two point till last
and if it is less than d then put it in d and so on.
 Here the time complexity in O( ).

Q5) Implement Quick sort by taking pivot element as high,


low, middle, random using C Program.
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int partitionl(int A[],int low,int high)
{
int temp;
int pivot=A[low];
int i=low+1;
int j=high;
do{

while(A[i]<=pivot)
{
i++;
}
while(A[j]>pivot)
{
j--;
}
if(i<j)
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}while(i<j);
temp=A[low];
A[low]=A[j];
A[j]=temp;
return j;
}
void quicksortl(int A[],int low,int high)
{
int partitionIndl;
if(low<high)
{

partitionIndl=partitionl(A,low,high);
quicksortl(A,low,partitionIndl-1);
quicksortl(A,partitionIndl+1,high);
}
}
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int partitionr(int A[],int low,int high)
{

int pivot=A[high];
int i=low-1;
int j=low;
for(j=low;j<high;j++)
{
if(A[j]<=pivot)
{
i++;
swap(&A[i],&A[j]);
}
}
swap(&A[i+1],&A[high]);
return i+1;
}

void quicksortr(int A[],int low,int high)


{
int partitionInd;
if(low<high)
{

partitionInd=partitionr(A,low,high);
quicksortr(A,low,partitionInd-1);
quicksortr(A,partitionInd+1,high);
}
}
int quicksortm(int arr[],int low,int high)
{
if(low>=high)
return 0;
int mid=(low+high)/2;
int pivot=arr[mid];
int i=low,j=high;
int temp;
while(i<j)
{
if(arr[i]>=pivot && arr[j]<=pivot)
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
i++;
j--;
}
else
{
i++;
}
}
quicksortm(arr,low,mid);
quicksortm(arr,mid+1,high);
}
int partitionrandom(int A[],int low,int high)
{
srand(time(NULL));
int r=low+rand()%(high-low);
int pivot=A[r];
swap(&A[r],&A[high]);
int i=low-1;
int j=low;
for(j=low;j<high;j++)
{
if(A[j]<=pivot)
{
i++;
swap(&A[i],&A[j]);
}
}
swap(&A[i+1],&A[high]);
return i+1;
}
void quicksortrandom(int A[],int low,int high)
{
int partitionInd;
if(low<high)
{
partitionInd=partitionrandom(A,low,high);
quicksortrandom(A,low,partitionInd-1);
quicksortrandom(A,partitionInd+1,high);
}
}
int main()
{
int i,A[50000];
int n;
printf("Enter the value of range:");
scanf("%d",&n);
srand(time(NULL));
for(int i=1;i<=n;i++)
{
A[i]=rand()%1000;
printf("%d ", A[i]);
}
clock_t t1,t2,t3,t4;
t1=clock();
quicksortl(A,1,n);
t1=clock()-t1;
double t5=((double)t1)/CLOCKS_PER_SEC;
printf("\n\nTime taken by Quick sort when pivot element is low is %lf
second",t5);
printf("\n");
printf("\nSorted Array of Quick sort when pivot is low:: \n");
for(i=1;i<=n;i++)
{
printf("%d ",A[i]);
}
t2=clock();
quicksortr(A,1,n);
t2=clock()-t2;
double t6=((double)t2)/CLOCKS_PER_SEC;
printf("\n\nTime taken by Quick sort when pivot element is high is %lf
second",t6);
printf("\n");
printf("\nSorted Array of Quick sort when pivot is high:: \n");
for(i=1;i<=n;i++)
{
printf("%d ",A[i]);
}
t3=clock();
quicksortm(A,1,n);
t3=clock()-t3;
double t7=((double)t3)/CLOCKS_PER_SEC;
printf("\n\nTime taken by Quick sort when pivot is middle element is %lf
second",t7);
printf("\n");
printf("\nSorted Array of Quick sort when pivot is middle:: \n");
for(i=1;i<=n;i++)
{
printf("%d ",A[i]);
}
t4=clock();
quicksortrandom(A,1,n);
t4=clock()-t4;
double t8=((double)t4)/CLOCKS_PER_SEC;
printf("\n\nTime taken by Quick sort when pivot element is random is %lf
second",t8);
printf("\n");
printf("\nSorted Array of Quick sort when pivot is random:: \n");
for(i=1;i<=n;i++)
{
printf("%d ",A[i]);
}
return 0;
}

Analysis:-
 Quicksort is a sorting algorithm which uses Divide and Conquer
approach.
 In Quicksort algorithm first we chooses a pivot and then partition the
array around the pivot.
 We use “time.h” header file and clock() function we analyse the time
taken Quicksort when the change the pivot position to high, low, middle
and random.
 Here we print we time taken by Quicksort at different pivot position to
show the variation of time by changing the pivot position.
 Time complexity of Quicksort when pivot is low and high is O( ) and
when pivot is middle is O(n log(n)) and when pivot is random element
then
T(n) = T(n-i) + i*O(n)
 Worst space complexity of Quicksort is O(log(n)).
:- %% Thank you %%

You might also like