In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.

Watch related videos of your favorite subject.

Connect with students from different parts of the world.

Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE

See Our team

Wondering how we keep quality?

Got unsolved questions? Ask Questions

GATE
GMAT
CBSE
NCERT
Career
Interview
Railway
UPSC
NID
NIFT-UG
NIFT-PG
PHP
AJAX
JavaScript
Node Js
Shell Script
Research

You are here:Open notes-->VTU-->Design-and-Analysis-of-Algorithms-Subject-Code--10CSL47-Lab-Manual-PROGRAM-2

**Design and Analysis of Algorithms Subject Code : 10CSL47 Lab Manual PROGRAM-2**

2. Using OpenMP, implement a parallelized Merge Sort algorithm to sort a given set of elements and determine the time required to sort the elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a graph of the time taken versus n. The elements can be read from a file or can be generated using the random number generator.

# include <stdio.h>

# include <conio.h>

#include<time.h>

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

{

int i, j, k, b[20];

i=low; j=mid+1; k=low;

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

{

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

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

else

b[k++] = a[j++] ;

}

while (i<=mid) b[k++] = a[i++] ;

while (j<=high) b[k++] = a[j++] ;

for(k=low; k<=high; k++)

a[k] = b[k];

}

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

{

int mid;

if(low >= high)

return;

mid = (low+high)/2 ;

MergeSort(a, low, mid);

MergeSort(a, mid+1, high);

Merge(a, low, mid, high);

}

void main()

{

int n, a[2000],k;

clock_t st,et;

double ts;

clrscr();

printf("\n Enter How many Numbers:");

scanf("%d", &n);

printf("\nThe Random Numbers are:\n");

for(k=1; k<=n; k++)

{

a[k]=rand();

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

}

st=clock();

MergeSort(a, 1, n);

et=clock();

ts=(double)(et-st)/CLOCKS_PER_SEC;

printf("\n Sorted Numbers are : \n ");

for(k=1; k<=n; k++)

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

printf("\nThe time taken is %e",ts);

getch();

}