-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_outer_merge_sort.cpp
More file actions
290 lines (240 loc) · 4.52 KB
/
10_outer_merge_sort.cpp
File metadata and controls
290 lines (240 loc) · 4.52 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// 归并排序分三步走
// 用 stringstream 内存字符串模拟文件
void merge1(ostream & fp,istream & fp1,istream & fp2)
{
char p1 = 0;
char p2 = 0;
// 三步走 1. 耗尽其一
fp1.get(p1);
fp2.get(p2);
for (; !fp1.fail() && !fp2.fail();)
{
if (p1<=p2)
{
fp.put(p1);
fp1.get(p1);
}
else
{
fp.put(p2);
fp2.get(p2); // get 有没有成功,看 for 判断
}
}
// 三步走 2. 捡起第一步中漏掉的
if(!fp1.fail())
{
fp.put(p1);
}
if (!fp2.fail())
{
fp.put(p2);
}
// 三步走 3. 把剩下的都耗尽
for (;fp1.get(p1);)
{
fp.put(p1);
}
for (;fp2.get(p2);)
{
fp.put(p2);
}
}
void merge2_error(ostream & fp,istream & fp1,istream & fp2)
{
char p1 = 0;
char p2 = 0;
fp1>>p1;
fp2>>p2;
// 流输入有没有成功 >> , 要看接下来的 eof()
for (;!fp1.eof() && !fp2.eof();)
{
if (p1<=p2)
{
fp.put(p1);
fp1>>p1;
}
else
{
fp.put(p2);
fp2>>p2;
}
}
for (;!fp1.eof();)
{
fp1>>p1;
fp.put(p1);
}
for (;!fp2.eof();)
{
fp2>>p2;
fp.put(p2);
}
}
void merge2_fix(ostream & fp,istream & fp1,istream & fp2)
{
// 分 3 步走
char p1 = 0;
char p2 = 0;
fp1>>p1;
fp2>>p2;
//1. 先把两个中的一个耗尽
// 流输入有没有成功 >> , 要看接下来的 eof()
for (;!fp1.eof() && !fp2.eof();)
{
if (p1<=p2)
{
fp.put(p1);
fp1>>p1;
}
else
{
fp.put(p2);
fp2>>p2;
}
}
// 2. 耗尽其中一个后,把上面已经获取到的捡漏
if (!fp1.eof())
{
fp.put(p1);
}
if(!fp2.eof())
{
fp.put(p2);
}
// 3. 把还没耗尽的都耗尽
for (;fp1>>p1;)
{
fp.put(p1);
}
for (;;)
{
// 两种判断结束的方法
fp2>>p2;
if (fp2.eof())
{
break;
}
fp.put(p2);
}
}
// 用 eof() 不理想
void test_eof()
{
cout<<"test_eof:"<<endl;
std::stringstream s("123");
char c = 0;
for (;!s.eof();)
{
s>>c;
cout<<c<<" ";
}
cout<<endl;
// output: 1 2 3 3
}
void test_good()
{
cout<<"test_good:"<<endl;
std::stringstream s("123");
char c = 0;
for (;s.good();)
{
s>>c;
cout<<c<<" ";
}
cout<<endl;
// output: 1 2 3 3
}
void test_get()
{
cout<<"test_get:"<<endl;
std::stringstream s("123");
char c = 0;
// 这句会有强制转换,调试发现会调用方法
// operator void *() const
// { // test if any stream operation has failed
// return (fail() ? 0 : (void *)this);
// }
for (;s.get(c);)
{
cout<<c<<" ";
}
cout<<endl;
// output : 1 2 3
}
void test_fail()
{
cout<<"test_fail:"<<endl;
std::stringstream s("123");
char c = 0;
for (;!s.fail();)
{
s>>c;
cout<<c<<" ";
}
cout<<endl;
// output : 1 2 3 3
}
// 不能使用,死循环
void test_bad()
{
cout<<"test_bad:"<<endl;
std::stringstream s("123");
char c = 0;
for (;!s.bad();)
{
s>>c;
cout<<c<<" ";
}
cout<<endl;
// output : infinit loop 1 2 3 3 3 3 ....
}
void test_merge(const char * s1, const char * s2, void (*func_merge)(ostream & fp,istream & fp1,istream & fp2))
{
std::stringstream _s1(s1);
std::stringstream _s2(s2);
std::stringstream _s;
func_merge(_s,_s1,_s2);
cout<<"\t"<<_s1.str() << " + "<<_s2.str() << " = "<< _s.str()<<endl;
}
void test_merge(void (*func_merge)(ostream & fp,istream & fp1,istream & fp2))
{
test_merge("135","",func_merge);
test_merge("135","2",func_merge);
test_merge("135","24",func_merge);
test_merge("13578","2469",func_merge);
}
int main()
{
test_eof();
test_good();
test_get();
test_fail();
cout<<"merge1:"<<endl;
test_merge(merge1);
cout<<"merge2_error:"<<endl;
test_merge(merge2_error);
cout<<"merge2_fix:"<<endl;
test_merge(merge2_fix);
return 0;
}
/*
merge1:
135 + = 135
135 + 2 = 1235
135 + 24 = 12345
13578 + 2469 = 123456789
merge2_error:
135 + = 355
135 + 2 = 1255
135 + 24 = 12345
13578 + 2469 = 123456789
merge2_fix:
135 + = 135
135 + 2 = 1235
135 + 24 = 12345
13578 + 2469 = 123456789
*/