-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort.c
126 lines (102 loc) · 1.92 KB
/
sort.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* assert */
#include <assert.h>
/* FILE, fopen, fclose, fscanf, rewind */
#include <stdio.h>
/* EXIT_SUCCESS, malloc, free, qsort */
#include <stdlib.h>
/* time, CLOCKS_PER_SEC */
#include <time.h>
static void
load(
char const * const filename,
int * const np,
unsigned int ** const valsp
)
{
int ret;
int j, n;
unsigned int dummy;
FILE * fp;
unsigned int * vals;
/* open the file */
fp = fopen(filename, "r");
assert(fp);
/* count the number of values in the file */
for (n=0; fscanf(fp, "%u", &dummy)>0; ++n);
assert(feof(fp));
/* allocate memory for values */
vals = malloc(n*sizeof(*vals));
assert(vals);
/* reset file pointer */
rewind(fp);
/* read in the values */
for (j=0; j<n; ++j) {
ret = fscanf(fp, "%u", &vals[j]);
assert(1 == ret);
}
/* close file */
ret = fclose(fp);
assert(!ret);
/* record output values */
*np = n;
*valsp = vals;
}
static int
asc(
void const * const a,
void const * const b
)
{
return (*(unsigned int*)a) < (*(unsigned int *)b) ? -1 : 1;
}
static void
sort_local(
int const n,
unsigned int * const vals
)
{
qsort(vals, n, sizeof(*vals), asc);
}
static void
print_numbers(
char const * const filename,
int const n,
unsigned int const * const vals
)
{
int ret, i;
FILE * fp;
/* open file */
fp = fopen(filename, "w");
assert(fp);
/* write list to fout */
for (i=0; i<n; ++i) {
fprintf(fp, "%u\n", vals[i]);
}
/* close file */
ret = fclose(fp);
assert(!ret);
}
static void
print_time(
double const seconds
)
{
printf("Sort Time: %0.04fs\n", seconds);
}
int
main(int argc, char ** argv)
{
int n;
clock_t s, e;
unsigned int * vals;
assert(argc > 2);
load(argv[1], &n, &vals);
s = clock(); /* use MPI_Wtime() in an MPI context */
sort_local(n, vals);
e = clock();
print_numbers(argv[2], n, vals);
print_time((double)(e-s)/CLOCKS_PER_SEC);
free(vals);
return EXIT_SUCCESS;
}