Skip to content
This repository was archived by the owner on Nov 3, 2024. It is now read-only.

Commit 186318a

Browse files
committed
Finish basic functionality
1 parent d3cba14 commit 186318a

File tree

9 files changed

+227
-76
lines changed

9 files changed

+227
-76
lines changed

GNUmakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ objs := $(srcs:%=out/%.o)
99
.PHONY: all
1010
all: test
1111

12-
test: $(objs) GNUmakefile
12+
test: $(objs)
1313
$(CC) $(objs) -o $@ $(LDFLAGS)
1414

1515
out/%.c.o: %.c test.h config.h

config.h

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
static int *init_asc(int len) {
2+
int *a = malloc(len * sizeof(int));
3+
for (int i = 0; i < len; ++i)
4+
a[i] = i;
5+
return a;
6+
}
7+
8+
static int *init_des(int len) {
9+
int *a = malloc(len * sizeof(int));
10+
for (int i = 0; i < len; ++i)
11+
a[i] = len-i-1;
12+
return a;
13+
}
14+
15+
#if RAND_MAX <= 32767
16+
#warning "RAND_MAX is less than or equal to 32767."
17+
#endif
18+
static int *init_ran(int len) {
19+
int *a = malloc(len * sizeof(int)), c = RAND_MAX/len + 1;
20+
for (int i = 0; i < len; ++i)
21+
a[i] = rand() / c;
22+
return a;
23+
}
24+
125
Metric sort_bubble(Sequence seq);
226
Metric sort_insert(Sequence seq);
327
Metric sort_quick(Sequence seq);
@@ -7,15 +31,15 @@ Metric sort_heap(Sequence seq);
731
int repeattest = 3; /* repeat x times and get the average */
832

933
Sequence seqs[] = {
10-
{"ascending", init_asc, 100},
11-
{"ascending", init_asc, 1000},
12-
{"ascending", init_asc, 10000},
13-
{"descending", init_des, 100},
14-
{"descending", init_des, 1000},
15-
{"descending", init_des, 10000},
16-
{"random", init_ran, 100},
17-
{"random", init_ran, 1000},
18-
{"random", init_ran, 10000}
34+
{"ascending", init_asc, 2000},
35+
{"ascending", init_asc, 16000},
36+
{"ascending", init_asc, 128000},
37+
{"descending", init_des, 2000},
38+
{"descending", init_des, 16000},
39+
{"descending", init_des, 128000},
40+
{"random", init_ran, 2000},
41+
{"random", init_ran, 16000},
42+
{"random", init_ran, 128000}
1943
};
2044

2145
struct TestCase {

sorts/bubble.c

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
#include "test.h"
22

3-
static void sort_bubble_(int *a, int len) {
4-
int i, t, s = 1;
5-
while (s) {
6-
s = 0;
7-
for (i = 1; i < len; i++) {
8-
if (a[i] < a[i - 1]) {
9-
t = a[i];
10-
a[i] = a[i - 1];
11-
a[i - 1] = t;
12-
s = 1;
13-
}
14-
}
15-
--len;
16-
}
3+
static Metric M;
4+
5+
static void bubblesort(int *a, int len) {
6+
int i, t, s = 1;
7+
while (s) {
8+
s = 0;
9+
for (i = 1; i < len; i++) {
10+
if (a[i] < a[i-1]) {
11+
t = a[i];
12+
a[i] = a[i-1];
13+
a[i-1] = t;
14+
s = 1;
15+
++M.swap;
16+
}
17+
++M.comp;
18+
}
19+
--len;
20+
}
1721
}
1822

1923
Metric sort_bubble(Sequence seq) {
20-
return (Metric){0};
24+
M.comp = M.swap = M.ptime = 0;
25+
bubblesort(seq.data, seq.len);
26+
return M;
2127
}

sorts/heap.c

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
11
#include "test.h"
22

3-
static void sort_heap_(int *a, int len) {
3+
static Metric M;
4+
5+
static int max(int *a, int len, int i, int j, int k) {
6+
int m = i;
7+
if (j < len && a[j] > a[m]) {
8+
m = j;
9+
++M.comp;
10+
}
11+
if (k < len && a[k] > a[m]) {
12+
m = k;
13+
++M.comp;
14+
}
15+
return m;
16+
}
17+
18+
static void downheap(int *a, int len, int i) {
19+
while (1) {
20+
int j = max(a, len, i, 2*i + 1, 2*i + 2);
21+
if (j == i) {
22+
break;
23+
}
24+
int t = a[i];
25+
a[i] = a[j];
26+
a[j] = t;
27+
i = j;
28+
++M.swap;
29+
}
30+
}
31+
32+
static void heapsort(int *a, int len) {
33+
int i;
34+
for (i = (len - 2) / 2; i >= 0; i--) {
35+
downheap(a, len, i);
36+
}
37+
for (i = 0; i < len; i++) {
38+
int t = a[len - i - 1];
39+
a[len - i - 1] = a[0];
40+
a[0] = t;
41+
++M.swap;
42+
downheap(a, len - i - 1, 0);
43+
}
444
}
545

646
Metric sort_heap(Sequence seq) {
7-
return (Metric){0};
47+
M.comp = M.swap = M.ptime = 0;
48+
heapsort(seq.data, seq.len);
49+
return M;
850
}

sorts/insert.c

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
#include "test.h"
22

3-
static void sort_insert_(int *a, int len) {
3+
static Metric M;
4+
5+
static void insertsort(int *a, int len) {
6+
int i, j, key;
7+
for (i = 1; i < len; ++i) {
8+
key = a[i];
9+
j = i;
10+
if (j > 0 && key < a[j-1]) {
11+
do {
12+
a[j] = a[j-1];
13+
--j;
14+
++M.swap;
15+
++M.comp;
16+
} while (j > 0 && key < a[j-1]);
17+
}
18+
a[j] = key;
19+
++M.comp;
20+
++M.swap;
21+
}
422
}
523

624
Metric sort_insert(Sequence seq) {
7-
return (Metric){0};
25+
M.comp = M.swap = M.ptime = 0;
26+
insertsort(seq.data, seq.len);
27+
return M;
828
}

sorts/merge.c

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
1+
#include <stdlib.h>
2+
13
#include "test.h"
24

3-
static void sort_merge_(int *a, int len) {
5+
static Metric M;
6+
7+
static void merge(int *a, int len, int m, int *x) {
8+
int i, j, k;
9+
for (i = 0; i < m; i++) {
10+
x[i] = a[i];
11+
}
12+
M.swap += m;
13+
for (i = 0, j = m, k = 0;;) {
14+
++M.comp;
15+
++M.swap;
16+
if (a[j] < x[i]) {
17+
a[k++] = a[j++];
18+
if (j >= len)
19+
break;
20+
} else {
21+
a[k++] = x[i++];
22+
if (i >= m)
23+
return;
24+
}
25+
}
26+
while (i < m)
27+
a[k++] = x[i++];
28+
M.swap += m;
29+
}
30+
31+
static void mergesort(int *a, int len, int *x) {
32+
if (len < 2)
33+
return;
34+
int m = len / 2;
35+
if (len > 2) {
36+
mergesort(a, m, x);
37+
mergesort(a + m, len - m, x);
38+
}
39+
merge(a, len, m, x);
440
}
541

642
Metric sort_merge(Sequence seq) {
7-
return (Metric){0};
43+
M.comp = M.swap = M.ptime = 0;
44+
if (seq.len < 2)
45+
return M;
46+
int *x = malloc(seq.len/2 * sizeof(int));
47+
mergesort(seq.data, seq.len, x);
48+
free(x);
49+
return M;
850
}

sorts/quick.c

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
#include "test.h"
22

3-
static void sort_quick_(int *a, int len) {
3+
static Metric M;
4+
5+
static void quicksort(int *a, int len) {
6+
if (len < 2)
7+
return;
8+
int pivot = a[len / 2];
9+
int i, j, t;
10+
for (i = 0, j = len - 1; ; ++i, --j) {
11+
if (a[i] < pivot) {
12+
do {
13+
++i;
14+
++M.comp;
15+
} while (a[i] < pivot);
16+
}
17+
++M.comp;
18+
if (a[j] > pivot) {
19+
do {
20+
--j;
21+
++M.comp;
22+
} while (a[j] > pivot);
23+
}
24+
++M.comp;
25+
if (i >= j)
26+
break;
27+
t = a[i];
28+
a[i] = a[j];
29+
a[j] = t;
30+
++M.swap;
31+
}
32+
quicksort(a, i);
33+
quicksort(a + i, len - i);
434
}
535

636
Metric sort_quick(Sequence seq) {
7-
return (Metric){0};
37+
M.comp = M.swap = M.ptime = 0;
38+
quicksort(seq.data, seq.len);
39+
return M;
840
}

test.c

+26-40
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,7 @@
66

77
#include "test.h"
88

9-
static int *init_asc(int len) {
10-
int *a = malloc(len * sizeof(int));
11-
for (int i = 0; i < len; ++i)
12-
a[i] = i;
13-
return a;
14-
}
15-
16-
static int *init_des(int len) {
17-
int *a = malloc(len * sizeof(int));
18-
for (int i = 0; i < len; ++i)
19-
a[i] = len-i-1;
20-
return a;
21-
}
22-
23-
#if RAND_MAX <= 32767
24-
#warning "RAND_MAX is less than or equal to 32767."
25-
#endif
26-
static int *init_ran(int len) {
27-
int *a = malloc(len * sizeof(int)), c = RAND_MAX/len + 1;
28-
for (int i = 0; i < len; ++i)
29-
a[i] = rand() / c;
30-
return a;
31-
}
9+
typedef struct TestCase TestCase;
3210

3311
#include "config.h"
3412

@@ -41,34 +19,41 @@ static bool is_array_sorted(int *a, int len) {
4119

4220
static void test(TestCase t) {
4321
for (int i = 0; i < LENGTH(seqs); ++i) {
44-
Metric aux = {0};
45-
bool unsorted = false;
22+
Sequence seq = seqs[i];
4623
int repeat = repeattest;
47-
48-
printf("%s,%d,%s,", t.name, seqs[i].len, seqs[i].name);
24+
seq.data = NULL;
4925

5026
while (repeat--) {
51-
Sequence seq = {0};
52-
seq.name = seqs[i].name;
53-
seq.len = seqs[i].len;
27+
free(seq.data);
5428
seq.data = malloc(seq.len * sizeof(int));
5529
memcpy(seq.data, seqs[i].data, seq.len * sizeof(int));
5630

31+
Metric aux = {0};
32+
clock_t t_beg, t_end;
33+
t_beg = clock();
5734
aux = t.sort(seq);
58-
t.mets[i].comp += aux.comp;
59-
t.mets[i].chan += aux.chan;
60-
t.mets[i].wtime += aux.wtime;
35+
t_end = clock();
36+
aux.ptime = (double)(t_end - t_beg) / (CLOCKS_PER_SEC/1000);
6137

62-
if (!is_array_sorted(seq.data, seq.len))
63-
unsorted = true;
64-
free(seq.data);
38+
t.mets[i].comp += aux.comp;
39+
t.mets[i].swap += aux.swap;
40+
t.mets[i].ptime += aux.ptime;
6541
}
6642

6743
t.mets[i].comp /= repeattest;
68-
t.mets[i].chan /= repeattest;
69-
t.mets[i].wtime /= repeattest;
70-
printf(unsorted ? "unsorted" : "sorted");
71-
putchar('\n');
44+
t.mets[i].swap /= repeattest;
45+
t.mets[i].ptime /= repeattest;
46+
47+
printf("%s,%d,%s,%lld,%lld,%.3lf,%s\n",
48+
t.name,
49+
seqs[i].len,
50+
seqs[i].name,
51+
t.mets[i].comp,
52+
t.mets[i].swap,
53+
t.mets[i].ptime,
54+
is_array_sorted(seq.data, seq.len) ? "sorted" : "UNSORTED");
55+
56+
free(seq.data);
7257
}
7358
}
7459

@@ -77,6 +62,7 @@ int main(void) {
7762

7863
for (int i = 0; i < LENGTH(seqs); ++i)
7964
seqs[i].data = seqs[i].initpop(seqs[i].len);
65+
printf("Algorithm,Load number,Init sort,Comparisons,Swaps,CPU time (ms),Is sorted?\n");
8066
for (int i = 0; i < LENGTH(tests); ++i)
8167
test(tests[i]);
8268
for (int i = 0; i < LENGTH(seqs); ++i)

test.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
typedef struct Sequence Sequence;
44

5-
typedef struct TestCase TestCase;
6-
75
typedef struct {
8-
int comp, chan, wtime; /* comparisons, changes, walltime (elapsed time) */
6+
unsigned long long comp, swap; /* comparisons, swaps, */
7+
double ptime; /* CPU time (in ms) */
98
} Metric;
109

1110
struct Sequence {

0 commit comments

Comments
 (0)