-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenMP_Bubble_Sort.c
67 lines (64 loc) · 1.3 KB
/
OpenMP_Bubble_Sort.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
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
int main()
{
int n;
int i, j;
/*printf("Enter no of elements you want in your array: ");
scanf("%d\n",&n);
int arr[n];
*/
clock_t begin, end;
double total_t;
begin = clock();
FILE * fp;
fp = fopen("1-25000.txt","r");
fscanf(fp, "%d", &n);
int *arr = malloc(n * sizeof(int));
#pragma omp parallel num_threads(2)
{
#pragma omp for
for (i=0; i < n; i++)
{
fscanf(fp, "%d", &arr[i]);
}
}
#pragma omp parallel num_threads(2)
{
#pragma omp for
for (i = 0; i < n-1; i++)
{
#pragma omp parallel for
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
int temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
printf("Sorted array: \n");
#pragma omp parrallel num_threads(2)
{
#pragma omp for
for (i=0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
printf("\n");
end= clock();
total_t = (double)(end - begin );
//printf("Total Numbers : %d\n",n);
printf("Initial time taken by CPU: %d \n", begin );
printf("End time taken by CPU: %d \n", end );
printf("Total time taken by CPU: %f ms\n", total_t );
free(arr);
return 0;
}