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

0% found this document useful (0 votes)
3 views1 page

Bubble Sort

The document contains a C++ program that implements both sequential and parallel bubble sort algorithms. It prompts the user to input the size and elements of an array, sorts the array using bubble sort, and measures the time taken for both sorting methods. Finally, it outputs the sorted array and compares the execution times of the two sorting approaches.

Uploaded by

Yashodhan Gupta
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)
3 views1 page

Bubble Sort

The document contains a C++ program that implements both sequential and parallel bubble sort algorithms. It prompts the user to input the size and elements of an array, sorts the array using bubble sort, and measures the time taken for both sorting methods. Finally, it outputs the sorted array and compares the execution times of the two sorting approaches.

Uploaded by

Yashodhan Gupta
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/ 1

#include <iostream>

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

using namespace std;

void bubbleSort(int arr[], int n) {


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

void printArray(int arr[], int n) {


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

int main() {
int n;
cout << "Enter size of the array: ";
cin >> n;

int arr[n];
cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; ++i) cin >> arr[i];

int* bubbleArr = new int[n];


copy(arr, arr + n, bubbleArr);

// Sequential Bubble Sort


clock_t start = clock();
bubbleSort(bubbleArr, n);
double bubbleTime = double(clock() - start) / CLOCKS_PER_SEC;
cout << "Bubble Sorted Array: ";
printArray(bubbleArr, n);

// Parallel Bubble Sort


start = clock();
#pragma omp parallel
{
#pragma omp single
bubbleSort(bubbleArr, n);
}
double parallelBubbleTime = double(clock() - start) / CLOCKS_PER_SEC;

// Time Comparison
cout << fixed << setprecision(6);
cout << "\n--- Time Comparison ---" << endl;
cout << "Bubble Sort Time: " << bubbleTime << " sec" << endl;
cout << "Parallel Bubble Sort Time: " << parallelBubbleTime << " sec" << endl;

delete[] bubbleArr;
return 0;
}

You might also like