Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views2 pages

Parallel Merge Sort

The document contains C++ code implementing both parallel and sequential merge sort algorithms using OpenMP for parallelization. It generates a random array of integers, sorts it using both methods, and measures the time taken for each sort. The results, including the sorted arrays and time taken, are printed to the console.

Uploaded by

AARTI. SUTAR
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)
27 views2 pages

Parallel Merge Sort

The document contains C++ code implementing both parallel and sequential merge sort algorithms using OpenMP for parallelization. It generates a random array of integers, sorts it using both methods, and measures the time taken for each sort. The results, including the sorted arrays and time taken, are printed to the console.

Uploaded by

AARTI. SUTAR
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/ 2

#include <iostream>

#include <omp.h>
#include <cstdlib>
#include <ctime>

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;

int* L = new int[n1];


int* R = new int[n2];

for (int i = 0; i < n1; ++i) L[i] = arr[left + i];


for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];

delete[] L;
delete[] R;
}

void parallelMergeSort(int arr[], int left, int right, int depth = 0) {


if (left < right) {
int mid = (left + right) / 2;

if (depth <= 3) {
#pragma omp parallel sections
{
#pragma omp section
{
int tid = omp_get_thread_num();
std::cout << "Thread " << tid << " sorting left: " << left << "
to " << mid << "\n";
parallelMergeSort(arr, left, mid, depth + 1);
}

#pragma omp section


{
int tid = omp_get_thread_num();
std::cout << "Thread " << tid << " sorting right: " << mid + 1
<< " to " << right << "\n";
parallelMergeSort(arr, mid + 1, right, depth + 1);
}
}
} else {
// Sequential fallback
parallelMergeSort(arr, left, mid, depth + 1);
parallelMergeSort(arr, mid + 1, right, depth + 1);
}

merge(arr, left, mid, right);


}
}
void sequentialMergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
sequentialMergeSort(arr, left, mid);
sequentialMergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

int main() {
const int n = 20;
int arr1[n], arr2[n];

std::srand(std::time(nullptr));
for (int i = 0; i < n; i++) {
int val = std::rand() % 100;
arr1[i] = val;
arr2[i] = val;
}

std::cout << "Original array: ";


for (int i = 0; i < n; i++) std::cout << arr1[i] << " ";
std::cout << "\n\n";

int numThreads = 4;
omp_set_num_threads(numThreads);
std::cout << "Number of threads used: " << numThreads << "\n";

// Parallel Merge Sort


double parallelStart = omp_get_wtime();
parallelMergeSort(arr1, 0, n - 1);
double parallelEnd = omp_get_wtime();

std::cout << "\nSorted array (Parallel): ";


for (int i = 0; i < n; i++) std::cout << arr1[i] << " ";
std::cout << "\n";
std::cout << "Time taken (Parallel): " << parallelEnd - parallelStart << "
seconds\n";

// Sequential Merge Sort


double seqStart = omp_get_wtime();
sequentialMergeSort(arr2, 0, n - 1);
double seqEnd = omp_get_wtime();

std::cout << "\nSorted array (Sequential): ";


for (int i = 0; i < n; i++) std::cout << arr2[i] << " ";
std::cout << "\n";
std::cout << "Time taken (Sequential): " << seqEnd - seqStart << " seconds\n";

return 0;
}

You might also like