0% found this document useful (0 votes)
37 views3 pages

Design and Analysis of Algorithms - 20ISL57A Program 2 - Implement and Analyze Merge Sort Algorithm

The document implements and analyzes the merge sort algorithm. It includes the code to perform merge sort on an array of random numbers. It times the sorting and outputs the sorted array and time required.

Uploaded by

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

Design and Analysis of Algorithms - 20ISL57A Program 2 - Implement and Analyze Merge Sort Algorithm

The document implements and analyzes the merge sort algorithm. It includes the code to perform merge sort on an array of random numbers. It times the sorting and outputs the sorted array and time required.

Uploaded by

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

Design and Analysis of Algorithms – 20ISL57A

Program 2 - Implement and analyze merge sort algorithm

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

void simple_merge(int a[],int low, int mid, int high)

int i=low, j=mid+1, k=low, c[10000];

while(i<=mid && j<=high)

if(a[i]<a[j])

c[k]=a[i];

i++;

k++;

else

c[k]=a[j];

j++;

k++;

while(i<=mid)

c[k++]=a[i++];

while(j<=high)

c[k++]=a[j++];
for(i=low;i<=high;i++)

a[i]=c[i];

void merge_sort(int a[],int low,int high)

int mid;

if(low<high)

mid=(low+high)/2;

merge_sort(a,low,mid);

merge_sort(a,mid+1,high);

simple_merge(a,low,mid,high);

int main()

int a[10000],i=0,n;

clock_t st,end;

printf("Enter the value of n\n");

scanf("%d",&n);

printf("Random numbers generated are\n");

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

a[i]=rand()%100;

printf("%d\t",a[i]);

st=clock();

merge_sort(a,0,n-1);
end=clock();

printf("\nAfter Sorting\n");

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

printf("%d\t",a[i]);

printf("\nTime required to sort given elements is %f",(float)(end-st)/CLOCKS_PER_SEC);

You might also like