-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenMP_Insertion_Sort.c
55 lines (50 loc) · 1.13 KB
/
OpenMP_Insertion_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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <time.h>
int main()
{
int n;
clock_t begin, end;
double total_t;
begin = clock();
/*printf("Enter no of elements you want in your array: ");
scanf("%d\n",&n);
int arr[n];*/
FILE * fp;
fp = fopen("1-25000.txt","r");
fscanf(fp, "%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
int key, j,i;
for ( i=0;i<n;i++)
{
fscanf(fp, "%d", &arr[i]);
}
#pragma omp parallel num_threads(3)
{
#pragma omp parallel for private(i,j,key)
for ( i = 0; i < n; i++)
{
key = arr[i];
for( j=i ; j>0 && arr[j-1] > key ; j-- )
#pragma omp critical
arr[j]=arr[j-1];
arr[j]=key;
}
}
#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 seconds\n", total_t );
free(arr);
return 0;
}