This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequential.c
73 lines (60 loc) · 1.6 KB
/
sequential.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "helper.h"
#include "loader.h"
#include "corank.h"
#include "merge_sequential.h"
int main(int argc, char** argv) {
struct merge_sample sample;
int errorCode = handleArguments(argc, argv, &sample);
if(errorCode != 0) {
exit(errorCode);
}
printf("Array 1: \n");
if(sample.size1 > 30) {
echoArray(sample.array1, 30);
printf("Output truncated because it is too large. (size=%d)\n", sample.size1);
} else {
echoArray(sample.array1, sample.size1);
}
printf("Array 2: \n");
if(sample.size2 > 30) {
echoArray(sample.array2, 30);
printf("Output truncated because it is too large. (size=%d)\n", sample.size2);
} else {
echoArray(sample.array2, sample.size2);
}
printf("\n");
int n = sample.size1 + sample.size2;
INPUTTYPE *output = malloc(sizeof(INPUTTYPE) * n);
// set up timer:
struct timespec starttime, endtime;
clock_gettime(CLOCK_REALTIME, &starttime);
int i, j, x;
i = j = x = 0;
while(i < sample.size1 && j < sample.size2) {
if(sample.array1[i] < sample.array2[j]) {
output[x++] = sample.array1[i++];
} else {
output[x++] = sample.array2[j++];
}
}
while(i < sample.size1) {
output[x++] = sample.array1[i++];
}
while(j < sample.size2) {
output[x++] = sample.array2[j++];
}
clock_gettime(CLOCK_REALTIME, &endtime);
printTimeDiff(starttime, endtime);
printf("\nchecking result-array: ");
if(checkSorted(output, n)==1) {
printf("Correct\n");
} else {
printf("Failed\n");
}
free(output);
freesample(&sample);
return 0;
}