-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bubble_sort_Serial_Parallel.c
63 lines (55 loc) · 1.32 KB
/
Bubble_sort_Serial_Parallel.c
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
/*
Author : Gopal Panigrahi
Date : 19 September 2020
Description : In this program, Bubble sort is implemented using Odd - even Transposition
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void main(){
srand(time(0));
int size = 500000;
double end,start;
int *array = (int*)calloc(size,sizeof(int));
// Initializes array with random values
for(int i=0;i<size;i++){
int rn = rand()%(i+1);
array[i]=i;
swap(&array[rn],&array[i]);
}
// SERIAL REGION
start = omp_get_wtime();
for(int i=0;i<size;i++){
int first = i&1;
for(int j=first;j<size-1;j+=2){
if(array[j]>array[j+1])
swap(&array[j],&array[j+1]);
}
}
end = omp_get_wtime();
printf("Serial Time %lf\n",end-start);
// Initializes array with random values
for(int i=0;i<size;i++){
int rn = rand()%(i+1);
array[i]=i;
swap(&array[rn],&array[i]);
}
// PARALLEL REGION
start = omp_get_wtime();
for(int i=0;i<size;i++){
int first = i&1;
#pragma omp parallel for default(none) shared(size,array,first)
for(int j=first;j<size-1;j+=2){
if(array[j]>array[j+1])
swap(&array[j],&array[j+1]);
}
}
end = omp_get_wtime();
printf("Parallel Time %lf\n",end-start);
}