-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortProj.cs
More file actions
469 lines (422 loc) · 15 KB
/
MergeSortProj.cs
File metadata and controls
469 lines (422 loc) · 15 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
namespace SBD_P1
{
static class Globals
{
public static int DiskWriteCounter = 0;
public static int DiskReadCounter = 0;
public static int PhaseCounter = 0;
public static int PageSize = 256;
public static char Delimiter = ',';
}
class DigitSet : IComparable<DigitSet>
{
private string _digitsString;
public DigitSet(string digitsString)
{
_digitsString = digitsString.Replace(Globals.Delimiter.ToString(), "");
}
public void SortSelf()
{
_digitsString = string.Concat(_digitsString.OrderByDescending(x => x));
}
public override string ToString()
{
return string.Join(Globals.Delimiter, _digitsString.ToCharArray());
}
public int CompareTo([AllowNull] DigitSet other)
{
if (other == null) return 1;
if (_digitsString.Length < other._digitsString.Length) return -1;
if (_digitsString.Length > other._digitsString.Length) return 1;
for (int i = 0; i < _digitsString.Length; i++)
{
if (_digitsString[i] < other._digitsString[i]) return -1;
if (_digitsString[i] > other._digitsString[i]) return 1;
}
return 0;
}
}
struct SourceTape
{
private readonly DigitSet[] _page;
private int _pageIndex;
private int _recordIndex;
private readonly string _fileName;
private long _recordsInFile;
public SourceTape(string fileName)
{
_page = new DigitSet[Globals.PageSize];
_pageIndex = -1;
_recordIndex = 0;
_recordsInFile = 0;
_fileName = fileName;
LoadNextPage();
}
public void LoadNextPage()
{
_pageIndex++;
_recordIndex = 0;
using StreamReader reader = new StreamReader(_fileName);
for (int i = 0; i < _pageIndex * Globals.PageSize; i++)
reader.ReadLine();
for (int i = 0; i < Globals.PageSize && reader.Peek() >= 0; i++)
{
string line = reader.ReadLine();
_page[i] = new DigitSet(line);
_recordsInFile++;
}
Globals.DiskReadCounter++;
}
public DigitSet LoadNextRecord()
{
if (_recordIndex == Globals.PageSize) LoadNextPage();
if (_pageIndex * Globals.PageSize + _recordIndex + 1 > _recordsInFile) return null;
_recordIndex++;
return _page[_recordIndex - 1];
}
}
struct DestinationTape : IDisposable
{
private readonly DigitSet[] _page;
private int _recordIndex;
private readonly string _fileName;
public DestinationTape(string fileName)
{
_page = new DigitSet[Globals.PageSize];
_recordIndex = 0;
_fileName = fileName;
File.Delete(fileName);
}
void SaveNextPage()
{
using StreamWriter writer = new StreamWriter(_fileName, true);
for (int i = 0; i < _recordIndex; i++)
{
writer.WriteLine(_page[i]);
}
_recordIndex = 0;
Globals.DiskWriteCounter++;
}
public void SaveNextRecord(DigitSet digitSet)
{
_page[_recordIndex] = digitSet;
_recordIndex++;
if (_recordIndex >= Globals.PageSize)
{
SaveNextPage();
}
}
public void Dispose()
{
SaveNextPage();
}
}
class Program
{
static void MockFile()
{
Console.WriteLine("Enter file name:");
string fileName = Console.ReadLine();
using var mockTape = new DestinationTape(fileName);
Console.WriteLine("Enter the amount of records to be generated:");
if (int.TryParse(Console.ReadLine(), out int counter))
{
var ranGen = new Random();
for (int i = 0; i < counter; i++)
{
int[] digitsSet = new int[ranGen.Next(1, 30)];
for (int j = 0; j < digitsSet.Length; j++)
{
digitsSet[j] = ranGen.Next(0, 10);
}
digitsSet = digitsSet.OrderByDescending(x => x).ToArray();
mockTape.SaveNextRecord(new DigitSet(string.Join(Globals.Delimiter, digitsSet.ToArray())));
}
}
}
static void ReadRecords()
{
Console.WriteLine("Enter file name:");
string fileName = Console.ReadLine();
using var mockTape = new DestinationTape(fileName);
Console.WriteLine("Enter the amount of records:");
if (int.TryParse(Console.ReadLine(), out int counter))
{
for (int i = 0; i < counter; i++)
{
DigitSet toSave = new DigitSet(Console.ReadLine());
toSave.SortSelf();
mockTape.SaveNextRecord(toSave);
}
}
}
static TimeSpan SortFile()
{
bool sorted = false;
Console.WriteLine("Enter file name: ");
var filename = Console.ReadLine();
File.Copy(filename, "s1.txt");
Console.WriteLine("Display file after every phase? - Y/n");
var displayOption = Console.ReadLine()?.ToLower() == "y" ? true : false;
Console.Clear();
Console.WriteLine("File before sorting:");
DisplayFile(filename);
Stopwatch sw = new Stopwatch();
sw.Start();
while (!sorted)
{
Globals.PhaseCounter++;
Distribute();
sorted = !Merge();
if (displayOption) {
Console.WriteLine("Phase #" + Globals.PhaseCounter);
DisplayFile("s1.txt");
//Console.WriteLine("Press any key to continue to next phase.");
//Console.ReadKey();
}
}
sw.Stop();
Console.WriteLine("File after sorting:");
DisplayFile("d1.txt");
File.Delete("sorted_" + filename);
File.Copy("d1.txt", "sorted_" + filename);
File.Delete("s1.txt");
File.Delete("d1.txt");
File.Delete("d2.txt");
return sw.Elapsed;
}
static void Distribute()
{
SourceTape s1 = new SourceTape("s1.txt");
using DestinationTape d1 = new DestinationTape("d1.txt");
using DestinationTape d2 = new DestinationTape("d2.txt");
int counter = 0;
var record = s1.LoadNextRecord();
DigitSet lastRecord = null;
while (record != null)
{
if (record.CompareTo(lastRecord) == -1)
{
counter++;
counter %= 2;
}
if (counter == 0) d1.SaveNextRecord(record);
else d2.SaveNextRecord(record);
lastRecord = record;
record = s1.LoadNextRecord();
}
}
static bool Merge()
{
SourceTape s1 = new SourceTape("d1.txt");
SourceTape s2 = new SourceTape("d2.txt");
using DestinationTape d1 = new DestinationTape("s1.txt");
DigitSet r1, r2, l1 = null, l2 = null;
r1 = s1.LoadNextRecord();
r2 = s2.LoadNextRecord();
if (r2 == null) return false;
while (!(r1 == null && r2 == null))
{
if (r1 != null && r2 != null)
{
if (r1.CompareTo(l1) == -1)
{
while (r2 != null && r2.CompareTo(l2) == 1)
{
d1.SaveNextRecord(r2);
l2 = r2;
r2 = s2.LoadNextRecord();
}
l1 = null;
l2 = null;
}
else if (r2.CompareTo(l2) == -1)
{
while (r1 != null && r1.CompareTo(l1) == 1)
{
d1.SaveNextRecord(r1);
l1 = r1;
r1 = s1.LoadNextRecord();
}
l1 = null;
l2 = null;
}
else
{
if (r1.CompareTo(r2) == -1)
{
d1.SaveNextRecord(r1);
l1 = r1;
r1 = s1.LoadNextRecord();
}
else
{
d1.SaveNextRecord(r2);
l2 = r2;
r2 = s2.LoadNextRecord();
}
}
}
else if (r1 == null)
{
while (r2 != null)
{
d1.SaveNextRecord(r2);
l2 = r2;
r2 = s2.LoadNextRecord();
}
}
else if (r2 == null)
{
while (r1 != null)
{
d1.SaveNextRecord(r1);
l1 = r1;
r1 = s1.LoadNextRecord();
}
}
}
return true;
}
//static bool Merge2()
//{
// SourceTape s1 = new SourceTape("d1.txt");
// SourceTape s2 = new SourceTape("d2.txt");
// using DestinationTape d1 = new DestinationTape("s1.txt");
// DigitSet r1, r2, l1 = null, l2 = null;
// r1 = s1.LoadNextRecord();
// r2 = s2.LoadNextRecord();
// if (r2 == null) return false;
// while (!(r1 == null && r2 == null))
// {
// if (r1 != null && r1.CompareTo(r2) == 1)
// {
// //r1 > r2
// if (r2 != null && r2.CompareTo(l1) == 1)
// {
// d1.SaveNextRecord(r2);
// l2 = r2;
// r2 = s2.LoadNextRecord();
// }
// else
// {
// d1.SaveNextRecord(r1);
// l1 = r1;
// r1 = s1.LoadNextRecord();
// }
// }
// else
// {
// if (r1 != null && r1.CompareTo(l2) == 1)
// {
// d1.SaveNextRecord(r1);
// l1 = r1;
// r1 = s1.LoadNextRecord();
// }
// else
// {
// d1.SaveNextRecord(r2);
// l2 = r2;
// r2 = s2.LoadNextRecord();
// }
// }
// }
// return true;
//}
static void ChangePageSize()
{
Console.Clear();
Console.WriteLine("Current page size: " + Globals.PageSize);
Console.Write("Enter new page size: ");
if (int.TryParse(Console.ReadLine(), out int NewPageSize))
{
if (NewPageSize > 0)
{
Globals.PageSize = NewPageSize;
Console.WriteLine("Page size changed to: " + Globals.PageSize);
}
else
{
Console.WriteLine("Page size can not be negative!");
}
}
else
{
Console.WriteLine("Invalid input!");
}
}
static string DisplayMainMenu()
{
Console.Clear();
Console.WriteLine("1 - Sort records from file");
Console.WriteLine("2 - Generate file with random records");
Console.WriteLine("3 - Sort entered records");
Console.WriteLine("4 - Change page size");
Console.WriteLine("5 - Exit");
return Console.ReadLine();
}
static bool OptionController(string option)
{
Console.Clear();
switch (option)
{
case "1":
TimeSpan t = SortFile();
DisplayDetails(t);
ResetGlobals();
break;
case "2":
MockFile();
ResetGlobals();
Console.WriteLine("Records generated!");
break;
case "3":
ReadRecords();
Console.WriteLine("Records saved!");
break;
case "4":
ChangePageSize();
break;
case "5":
return false;
default:
Console.WriteLine("Invalid option");
break;
}
Console.ReadLine();
return true;
}
static void DisplayDetails(TimeSpan t)
{
Console.WriteLine("Records sorted!");
Console.WriteLine("Phases: " + --Globals.PhaseCounter);
Console.WriteLine("Write operations: " + Globals.DiskWriteCounter);
Console.WriteLine("Read operations: " + Globals.DiskReadCounter);
Console.WriteLine("Time elapsed: " + t);
}
static void ResetGlobals()
{
Globals.DiskReadCounter = 0;
Globals.DiskWriteCounter = 0;
Globals.PhaseCounter = 0;
}
static void DisplayFile(string filename)
{
Console.WriteLine(File.ReadAllText(filename));
}
static void Main()
{
bool running = true;
while (running)
{
var option = DisplayMainMenu();
running = OptionController(option);
}
}
}
}