forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_5-7.c
148 lines (121 loc) · 2.71 KB
/
exercise_5-7.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <string.h>
#define MAX_NUM_LINES 5000
#define MAX_LINE_LENGTH 1000
#define ALLOC_BUFFER_SIZE 10000
char* lines[MAX_NUM_LINES];
static char alloc_buffer[ALLOC_BUFFER_SIZE];
static char* alloc_next_free = alloc_buffer;
char* alloc(int n)
{
if (alloc_buffer + ALLOC_BUFFER_SIZE - alloc_next_free >= n)
{
alloc_next_free += n;
return alloc_next_free - n;
}
else
{
return 0;
}
}
void afree(char* p)
{
if (p >= alloc_buffer && p < alloc_buffer + ALLOC_BUFFER_SIZE)
{
alloc_next_free = p;
}
}
void swap(char* v[], int i, int j)
{
char* tmp = v[i];
v[i] = v[j];
v[j] = tmp;
}
void quick_sort(char* v[], int left, int right)
{
int last;
if (left >= right)
{
return;
}
swap(v, left, (left + right) / 2);
last = left;
for (int i = left + 1; i <= right; ++i)
{
if (strcmp(v[i], v[left]) < 0)
{
swap(v, ++last, i);
}
}
swap(v, left, last);
quick_sort(v, left, last - 1);
quick_sort(v, last + 1, right);
}
int get_line(char* line, int max_line_length)
{
int current_char;
int length = 0;
for (; length < max_line_length - 1 && (current_char = getchar()) != EOF && current_char != '\n'; ++length)
{
line[length] = current_char;
}
if (current_char == '\n')
{
line[length++] = current_char;
}
line[length] = '\0';
return length;
}
int read_lines(char* plines[], int max_lines, char* buffer)
{
int length;
int num_lines = 0;
char line[MAX_LINE_LENGTH];
char* ptr = buffer + strlen(buffer);
while ((length = get_line(line, MAX_LINE_LENGTH)) > 0)
{
if (num_lines >= max_lines || (strlen(buffer) + length) > ALLOC_BUFFER_SIZE)
{
return -1;
}
else
{
/* Delete newline. */
line[length - 1] = '\0';
strcpy(ptr, line);
lines[num_lines++] = ptr;
ptr += length;
}
}
return num_lines;
}
void write_lines(char* plines[], int num_lines)
{
for (int i = 0; i < num_lines; ++i)
{
printf("%s\n", plines[i]);
}
}
/*
* Using alloc : 0.002s
* Using array supplied by main: 0.002s
*
* Conclusion: No measurable difference in performance (without using a more precise method).
*/
int main()
{
int num_lines;
char store[ALLOC_BUFFER_SIZE] = { 0 };
if ((num_lines = read_lines(lines, MAX_NUM_LINES, store)) >= 0)
{
quick_sort(lines, 0, num_lines - 1);
write_lines(lines, num_lines);
return 0;
}
else
{
puts("error: input too big to sort");
return 1;
}
return 0;
}