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

Parallel Bubble Sort in C++

The document provides a C++ implementation of both sequential and parallel bubble sort algorithms using OpenMP for parallelization. It includes functions for sorting an array, swapping elements, and measuring execution time for both algorithms. The main function handles user input, executes the sorting algorithms, and displays the sorted array 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)
7 views2 pages

Parallel Bubble Sort in C++

The document provides a C++ implementation of both sequential and parallel bubble sort algorithms using OpenMP for parallelization. It includes functions for sorting an array, swapping elements, and measuring execution time for both algorithms. The main function handles user input, executes the sorting algorithms, and displays the sorted array 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

##################################### parallel bubble Sort

####################################33

#include <iostream>
#include <omp.h>

using namespace std;

void sequentialBubbleSort(int *, int);


void parallelBubbleSort(int *, int);
void swap(int &, int &);

void sequentialBubbleSort(int *a, int n)


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

if (!swapped)
break;
}
}

void parallelBubbleSort(int *a, int n)


{
int swapped;
for (int i = 0; i < n; i++)
{
swapped = 0;
int first=i%2;
#pragma omp parallel for shared(a,first)
for (int j = first; j < n - 1; j++)
{
if (a[j] > a[j + 1])
{
swap(a[j], a[j + 1]);
swapped = 1;
}
}

if (!swapped)
break;
}
}

void swap(int &a, int &b)


{
int test;
test = a;
a = b;
b = test;
}

int main()
{
int *a, n;
cout << "\n enter total no of elements=>";
cin >> n;
a = new int[n];
cout << "\n enter elements=>";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}

double start_time = omp_get_wtime(); // start timer for sequential algorithm


sequentialBubbleSort(a, n);
double end_time = omp_get_wtime(); // end timer for sequential algorithm

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


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

cout << "Time taken by sequential algorithm: " << end_time - start_time << "
seconds" << endl;

start_time = omp_get_wtime(); // start timer for parallel algorithm


parallelBubbleSort(a, n);
end_time = omp_get_wtime(); // end timer for parallel algorithm

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


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

cout << "Time taken by parallel algorithm: " << end_time - start_time << "
seconds" << endl;

delete[] a; // Don't forget to free the allocated memory

return 0;
}

You might also like