|
@@ -1,7 +1,8 @@
|
1
|
1
|
#include "common.h"
|
2
|
2
|
#include <stdio.h>
|
|
3
|
+#include <stdlib.h>
|
3
|
4
|
|
4
|
|
-struct timespec get_time()
|
|
5
|
+struct timespec get_time(void)
|
5
|
6
|
{
|
6
|
7
|
struct timespec start_time;
|
7
|
8
|
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
|
@@ -35,3 +36,68 @@ void print_time(struct timespec* ts)
|
35
|
36
|
long t = (s * 1000000000) + (ms * 1000000) + (us * 1000) + ns;
|
36
|
37
|
printf("%3lds %3ldms %3ldus %3ldns %12ld", s, ms, us, ns, t);
|
37
|
38
|
}
|
|
39
|
+
|
|
40
|
+int* read_int_array(unsigned n)
|
|
41
|
+{
|
|
42
|
+ int* array = (int*)malloc(n * sizeof(unsigned));
|
|
43
|
+ for (unsigned i = 0; i < n; ++i) {
|
|
44
|
+ scanf("%i", &array[i]);
|
|
45
|
+ }
|
|
46
|
+ return array;
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+void read_input(int** array, unsigned* n)
|
|
50
|
+{
|
|
51
|
+ printf("Enter n: ");
|
|
52
|
+ scanf("%u", n);
|
|
53
|
+ printf("Enter %u integers: ", 2 * *n);
|
|
54
|
+ *array = read_int_array(2 * *n);
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+void print_array(int* array, unsigned n)
|
|
58
|
+{
|
|
59
|
+ for (unsigned i = 0; i < n; ++i) {
|
|
60
|
+ printf("%i ", array[i]);
|
|
61
|
+ }
|
|
62
|
+ printf("\n");
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+void merge_sort_merge_seq(int* array, unsigned l, unsigned m, unsigned h)
|
|
66
|
+{
|
|
67
|
+ unsigned i = l, j = m + 1, k = 0;
|
|
68
|
+ int tmp[h - l + 1];
|
|
69
|
+ while (i <= m && j <= h) {
|
|
70
|
+ if (array[i] <= array[j]) {
|
|
71
|
+ tmp[k++] = array[i++];
|
|
72
|
+ }
|
|
73
|
+ else {
|
|
74
|
+ tmp[k++] = array[j++];
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ while (i <= m) {
|
|
79
|
+ tmp[k++] = array[i++];
|
|
80
|
+ }
|
|
81
|
+ while (j <= h) {
|
|
82
|
+ tmp[k++] = array[j++];
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ for (--k; k != (unsigned)-1; --k) {
|
|
86
|
+ array[l + k] = tmp[k];
|
|
87
|
+ }
|
|
88
|
+}
|
|
89
|
+
|
|
90
|
+void merge_sort_sub_seq(int* array, unsigned l, unsigned h)
|
|
91
|
+{
|
|
92
|
+ if (l < h) {
|
|
93
|
+ unsigned m = (l + h) / 2;
|
|
94
|
+ merge_sort_sub_seq(array, l, m);
|
|
95
|
+ merge_sort_sub_seq(array, m + 1, h);
|
|
96
|
+ merge_sort_merge_seq(array, l, m, h);
|
|
97
|
+ }
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+void merge_sort_seq(int* array, unsigned s)
|
|
101
|
+{
|
|
102
|
+ merge_sort_sub_seq(array, 0, s - 1);
|
|
103
|
+}
|