-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathparallelbubblesort.cpp
83 lines (66 loc) · 1.47 KB
/
parallelbubblesort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<bits/stdc++.h>
#include<omp.h>
#include<chrono>
using namespace std;
using namespace std::chrono;
typedef long long ll;
void sbubble(int arr[],int n){
time_point<system_clock> start,end;
start=system_clock::now();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
end=system_clock::now();
duration<double> time=end-start;
cout<<"Serial Bubble Sort time is: "<<time.count()*1000<<"\n";
}
void pbubble(int arr[],int n){
omp_set_num_threads(2);
int first=0;
time_point<system_clock> start,end;
start=system_clock::now();
for(int i=0;i<n;i++){
first=i%2;
#pragma omp parallel for default(none),shared(arr,first,n)
for(int j=first;j<n;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
end=system_clock::now();
duration<double> time=end-start;
cout<<"Paralel Bubble Sort time is: "<<time.count()*1000<<"\n";
}
int main(){
int n;
cout<<"Enter the size of the random array to be generated\n";
cin>>n;
int *a=new int[n];
int *b=new int[n];
int in=0,in1=0;
for(int i=n-1;i>=0;i--){
a[in++]=i;
b[in1++]=i;
}
sbubble(a,n);
pbubble(b,n);
return 0;
}
/* Execution:
1. Open terminal
2. g++ -std=c++14 -fopenmp parallelbubblesort.cpp
3. ./a.out
Enter the size of the random array to be generated
20000
Serial Bubble Sort time is: 2406.04 milliseconds
Paralel Bubble Sort time is: 1529.21 milliseconds
*/