This repository has been archived by the owner on Jun 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.c
82 lines (69 loc) · 1.42 KB
/
merge_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
*
* Parallel bottom-up merge sort algorithm in C-OpenMP.
*
*
*/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int min(int a, int b) {
return a < b ? a : b;
}
void merge(int* a, int* temp, int left, int m, int right) {
int i, j, k;
for(i = m+1; i > left; i--) temp[i-1] = a[i-1];
for(j = m; j < right; j++) temp[right+m-j] = a[j+1];
for(k = left; k <= right; k++) {
if(temp[j] < temp[i]) a[k] = temp[j--];
else a[k] = temp[i++];
}
}
int* merge_sort(int* a, int left, int right) {
if(left < right) {
int temp[SIZE];
int m, i;
for(m = 1; m <= right - left; m += m) {
for(i = left; i <= right - m; i += m+m)
merge(a, temp, i, i+m-1, min(i+m+m-1, right));
}
}
return a;
}
void parallel_merge_sort(int* a) {
int n = SIZE >> 1;
int left = n+1;
int right = (SIZE - left) + 1;
#pragma omp sections
{
#pragma omp section
merge_sort(a, 0, left);
#pragma omp section
merge_sort(a, left, right);
}
}
void display(int* a) {
int i;
for(i = 0; i < SIZE; i++)
printf("%d ", a[i]);
printf("\n");
}
int is_sorted(int* a) {
int i;
for(i = 1; i < SIZE; i++)
if(a[i] < a[i-1]) return 0;
return 1;
}
int main (int argc, char const *argv[]) {
int a[SIZE];
a[0] = 42321;
a[1] = 32;
a[2] = 1;
a[3] = 132131;
display(a);
parallel_merge_sort(a);
display(a);
printf("is_sorted: %d\n", is_sorted(a));
return 0;
}