Browse Source

merge sort seq

master
Robin Thoni 7 years ago
parent
commit
ee0f662173
5 changed files with 83 additions and 6 deletions
  1. 2
    2
      .gitignore
  2. 2
    2
      Makefile
  3. 67
    1
      common.c
  4. 7
    1
      common.h
  5. 5
    0
      d2s.c

+ 2
- 2
.gitignore View File

@@ -1,3 +1,3 @@
1
-omp-fusion-sort-seq
2
-omp-fusion-sort-par
1
+omp-merge-sort-seq
2
+omp-merge-sort-par
3 3
 *.o

+ 2
- 2
Makefile View File

@@ -3,10 +3,10 @@ LDLIBS = -lrt
3 3
 CC = gcc
4 4
 SOURCES_COMMON = common.c
5 5
 SOURCES_SEQ = $(SOURCES_COMMON) d2s.c
6
-OUT_SEQ = omp-fusion-sort-seq
6
+OUT_SEQ = omp-merge-sort-seq
7 7
 OBJS_SEQ = $(SOURCES_SEQ:.c=.o)
8 8
 SOURCES_PAR = $(SOURCES_COMMON) d2p.c
9
-OUT_PAR = omp-fusion-sort-par
9
+OUT_PAR = omp-merge-sort-par
10 10
 OBJS_PAR = $(SOURCES_PAR:.c=.o)
11 11
 
12 12
 all: release

+ 67
- 1
common.c View File

@@ -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
+}

+ 7
- 1
common.h View File

@@ -4,9 +4,15 @@
4 4
 #define _POSIX_C_SOURCE 199309L
5 5
 #include <time.h>
6 6
 
7
-struct timespec get_time();
7
+struct timespec get_time(void);
8 8
 struct timespec time_diff(struct timespec* ts1, struct timespec* ts2);
9 9
 struct timespec get_duration(struct timespec* ts);
10 10
 void print_time(struct timespec* ts);
11 11
 
12
+int* read_int_array(unsigned n);
13
+void read_input(int** array, unsigned* n);
14
+void print_array(int* array, unsigned n);
15
+
16
+void merge_sort_seq(int* array, unsigned s);
17
+
12 18
 #endif

+ 5
- 0
d2s.c View File

@@ -2,5 +2,10 @@
2 2
 
3 3
 int main(void)
4 4
 {
5
+  int* array;
6
+  unsigned n;
7
+  read_input(&array, &n);
8
+  merge_sort_seq(array, 2 * n);
9
+  print_array(array, 2 * n);
5 10
   return 0;
6 11
 }

Loading…
Cancel
Save