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

Merge Sort

The document provides a program that implements Parallel Bubble Sort and Merge Sort using OpenMP. It includes code for the Merge Sort algorithm, demonstrating both sequential and parallel execution, along with performance measurement for each method. The program prompts the user for input, sorts the array, and displays the sorted results along with the time taken for each sorting method.

Uploaded by

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

Merge Sort

The document provides a program that implements Parallel Bubble Sort and Merge Sort using OpenMP. It includes code for the Merge Sort algorithm, demonstrating both sequential and parallel execution, along with performance measurement for each method. The program prompts the user for input, sorts the array, and displays the sorted results along with the time taken for each sorting method.

Uploaded by

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

parallel bubble Sort/ Merge Sort

Write a program to implement Parallel Bubble Sort and Merge sort using OpenMP. Use
existing
algorithms and measure the performance of sequential and parallel algorithms.

################################################### Merge Sort


##########################

$$------------------>>g++ -fopenmp FileName.cpp -o Filename


$$------------------>>./FileName

#include<iostream>
#include<stdlib.h>
#include<omp.h>
using namespace std;

void mergesort(int a[],int i,int j);


void merge(int a[],int i1,int j1,int i2,int j2);

void mergesort(int a[],int i,int j)


{
int mid;
if(i<j)
{
mid=(i+j)/2;

#pragma omp parallel sections


{

#pragma omp section


{
mergesort(a,i,mid);
}

#pragma omp section


{
mergesort(a,mid+1,j);
}
}

merge(a,i,mid,mid+1,j);
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[1000];
int i,j,k;
i=i1;
j=i2;
k=0;

while(i<=j1 && j<=j2)


{
if(a[i]<a[j])
{
temp[k++]=a[i++];
}
else
{
temp[k++]=a[j++];
}
}

while(i<=j1)
{
temp[k++]=a[i++];
}

while(j<=j2)
{
temp[k++]=a[j++];
}

for(i=i1,j=0;i<=j2;i++,j++)
{
a[i]=temp[j];
}
}

int main()
{
int *a,n,i;
double start_time, end_time, seq_time, par_time;

cout<<"\n enter total no of elements=>";


cin>>n;
a= new int[n];

cout<<"\n enter elements=>";


for(i=0;i<n;i++)
{
cin>>a[i];
}

// Sequential algorithm
start_time = omp_get_wtime();
mergesort(a, 0, n-1);
end_time = omp_get_wtime();
seq_time = end_time - start_time;
cout << "\nSequential Time: " << seq_time << endl;

// Parallel algorithm
start_time = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single
{
mergesort(a, 0, n-1);
}
}
end_time = omp_get_wtime();
par_time = end_time - start_time;
cout << "\nParallel Time: " << par_time << endl;

cout<<"\n sorted array is=>";


for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}

return 0;
}

You might also like