-
Notifications
You must be signed in to change notification settings - Fork 4
/
merge.c
104 lines (87 loc) · 1.96 KB
/
merge.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#define max 100
typedef struct Data{
int low;
int high;
}Data;
int arr[max];
void merge(int low, int mid, int high)
{
int i, j, k;
int n1 = mid - low + 1;
int n2 = high - mid;
int left[n1], right[n2];
for (i = 0; i < n1; i++)
left[i] = arr[low + i];
for (j = 0; j < n2; j++)
right[j] = arr[mid+ 1+ j];
i = 0;
j = 0;
k = low;
while (i < n1 && j < n2)
{
if (left[i] <= right[j])
{
arr[k] = left[i];
i++;
}
else
{
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = left[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = right[j];
j++;
k++;
}
}
void* mergeSort(void* arg)
{
Data data = *((Data*)arg);
int low = data.low;
int high = data.high;
if (low < high)
{
int mid = low+(high-low)/2;
pthread_t thread1;
Data d1 = {low, mid};
pthread_t thread2;
Data d2 = {mid+1, high};
pthread_create(&thread1, NULL, mergeSort, &d1);
pthread_create(&thread2, NULL, mergeSort, &d2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
merge(low, mid, high);
}
}
int main()
{ int i, r1;
FILE* f;
f = fopen("input.txt", "r");
fscanf(f, "%d", &r1);
printf("Size of array is:\n %d \n",r1);
for(i=0; i<r1; i++)
fscanf(f, "%d", &arr[i]);
fclose(f);
Data data = {0, r1};
pthread_t t;
pthread_create(&t, NULL, mergeSort, &data);
pthread_join(t, NULL);
printf("Sorted array is: \n");
for (i=0; i < r1; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}