-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
552 lines (456 loc) · 14.8 KB
/
Program.cs
File metadata and controls
552 lines (456 loc) · 14.8 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Configuration;
namespace SortingBasics
{
public static class AppSettings
{
public static string ReadSetting(string key)
{
string result = "Not Found";
try
{
var appSettings = ConfigurationManager.AppSettings;
result = appSettings[key];
}
catch (ConfigurationErrorsException)
{
Console.WriteLine("Error reading app settings");
}
return result;
}
}
interface ISortable<T> where T : IComparable
{
void SortWithRecursion(IList<T> items,int offset);
void SortWithoutRecursionv1(IList<T> items);
void SortWithoutRecursionv2(IList<T> items);
}
public sealed class MOBsSuperSort<T> : ISortable<T> where T : IComparable
{
private bool Print { get; }
private bool PrintToFile = true;
public static string Filename = "";
private void ReadSettings()
{
PrintToFile = (AppSettings.ReadSetting("PrintToFile") == "true");
Filename = AppSettings.ReadSetting("Filename");
}
public static void DisplayResults()
{
Process notePad = new Process();
notePad.StartInfo.FileName = @"c:\windows\vim.bat";
notePad.StartInfo.Arguments = MOBsSuperSort<int>.Filename;
notePad.Start();
}
public static void DeleteOldTextFile()
{
if (File.Exists(Filename))
File.Delete(Filename);
}
public MOBsSuperSort(bool print)
{
Print = print;
ReadSettings();
}
public void PrintItems(string Message, IList<T> SortedItems)
{
System.Console.WriteLine(Message);
System.Console.WriteLine("The list of items.");
for (int index = 0; index < SortedItems.Count; index++)
System.Console.Write("[{0}] - {1} ", index, SortedItems[index]);
System.Console.WriteLine("End of the list of items.");
}
public void SortWithRecursion(IList<T> unsortedItems, int offset)
{
if (offset == unsortedItems.Count)
return;
}
private void UpdateProgress(int count)
{
if (count % 10000 == 0)
System.Console.WriteLine("");
if (count % 100 == 0)
System.Console.Write("o");
}
public void SortWithoutRecursionv3(List<T> unsortedItems)
{
int count = unsortedItems.Count;
if (Print)
{
PrintItems("v3 Pre Sorted items", unsortedItems);
PrintListToFile(String.Format("v3 Pre Sorted items : {0}", count), unsortedItems);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
unsortedItems.Sort();
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
PrintTime("v3 ", elapsedMs);
WriteToFile(String.Format("v3 Post Sorted items : {0} Took : {1} seconds {2} minutes ", count, elapsedMs * 0.001, (elapsedMs * 0.001) / 60));
if (Print)
{
PrintItems("V3 Post Sorted items", unsortedItems);
PrintListToFile("V3 Post Sorted List", unsortedItems);
}
System.Console.WriteLine("v3 Post Sorted items");
}
public void SortWithoutRecursionv1(IList<T> unsortedItems)
{
int NUM_ITERATIONS = 0;
int NUM_COMPARES = 0;
int count = unsortedItems.Count;
if (Print)
{
PrintItems("v1 Pre Sorted items", unsortedItems);
PrintListToFile(String.Format("v1 Pre Sorted items : {0}", count), unsortedItems);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
//Can not use another list, need to use the same list so it gets smaller !
if (1 > count)
return;
int startPos = 0;
//Goes through the whole list from the start position n-1 times
//If the current item is less than the start pos,swap it
//the other sorter also checks the item at the end of the list aswell
do
{
UpdateProgress(startPos);
for (int index = startPos + 1; index < count; index++)
{
if (unsortedItems[index].CompareTo(unsortedItems[startPos]) < 0)
{
T temp = unsortedItems[index];
unsortedItems[index] = unsortedItems[startPos];
unsortedItems[startPos] = temp;
}
++NUM_COMPARES;
}
++startPos;
++NUM_ITERATIONS;
} while (startPos < count - 1);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
PrintTime("v1 ", elapsedMs);
WriteToFile(String.Format("v1 Post Sorted items : {0} Took : {1} seconds {2} minutes - Did {3} Iterations Did {4} Compares ", count, elapsedMs * 0.001, (elapsedMs * 0.001) / 60, NUM_ITERATIONS, NUM_COMPARES));
if (Print)
{
PrintItems("Post Sorted items", unsortedItems);
PrintListToFile("V1 Post Sorted List", unsortedItems);
}
System.Console.WriteLine(String.Format("v1 Post Sorted items Did {0} iterations in search and {1} Comparisons", NUM_ITERATIONS, NUM_COMPARES));
}
private void PrintTime(string mes, long elapsedMs)
{
System.Console.WriteLine(mes + " Time took seconds " + (elapsedMs * 0.001));
System.Console.WriteLine(mes + " Time took minutes " + ((elapsedMs * 0.001) / 60));
}
public void WriteSessionIdToFile(Guid id, string message)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Filename, true))
{
file.WriteLine("\n\n\nThis session id is : " + id.ToString());
file.WriteLine("This is test type " + message);
}
}
public static void WriteToFile(string message)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Filename, true))
{
file.WriteLine(message);
}
System.Console.WriteLine(message);
}
private void PrintListToFile(string message, IList<T> unsortedItems)
{
if (PrintToFile == false)
return;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Filename, true))
{
int count = 0;
file.WriteLine(message);
foreach (var line in unsortedItems)
{
file.Write(String.Format(" {0} ", line));
count++;
}
file.WriteLine(" \n");
}
}
public void SortWithoutRecursionv2(IList<T> unsortedItems)
{
int count = unsortedItems.Count;
System.Console.WriteLine("v2 Pre Sorted items : " + count);
if (Print)
{
PrintListToFile("v2 Pre Sorted items : " + count, unsortedItems);
PrintItems("V2 Pre Sorted items", unsortedItems);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here
//This one uses a backward pointer as well so should be twice as quick
//Can not use another list, need to use the same list so it gets smaller !
if (1 > count)
return;
//We are choosing pairs, the biggest and smallest. We put then at th left startpos
//and at the right endpos
//So we are going to start with the left startPos, and the right endPos
//We go through the list and if the current item is < item at startPos swap it
//otherwise if its greater than the item at end pos swap it, at the ed of the run
//start pos and end pos must move up, as the startpos will have the smallest in the list
//endpos will have the biggest
int startPos = 0;
int endPos = count-1;
int NUM_ITERATIONS = 0;
int NUM_COMPARES = 0;
do
{
//Must make sure the number in the index on the left is smaller than the index on the right
//ie 5,3,5,2,2 index is 0, so left element is 5 and right element is 2, looking at index+1 so value 3
//so 3 is smaller than 1 but also bigger than 2 - so it will go both places ! Make sure before we start
//that the smallest in index left and index right is on the left, and the other one is in index right
//so before we start we swap 5 and 2, now we have 2,3,5,2,5, now the number we are looking at is
//still 3, so it goes in neither as it doesnt move
if (unsortedItems[startPos].CompareTo(unsortedItems[endPos]) > 0)
{
T temp = unsortedItems[startPos];
unsortedItems[startPos] = unsortedItems[endPos];
unsortedItems[endPos] = temp;
}
if (endPos - startPos > 1)
{
//we are NOT the last pair, so need to iterate
UpdateProgress(startPos);
for (int index = startPos + 1; index < endPos; index++)
{
if (unsortedItems[index].CompareTo(unsortedItems[startPos]) < 0)
{
T temp = unsortedItems[index];
unsortedItems[index] = unsortedItems[startPos];
unsortedItems[startPos] = temp;
--NUM_COMPARES;
}
else if (unsortedItems[index].CompareTo(unsortedItems[endPos]) > 0)
{
//Careful if the number is bigger than the one on the right, will the one at the right
//we swap it with be smaller than the one on the left ie 3,5,6,7,1, so
//compare 5, its not smaller than 3 so dont swap, its bigger than 1 so swap with 1
//now the 1 should have been swapped with the 3 ! but we have already swapped 3 and 1
//so we are ok !
T temp = unsortedItems[index];
unsortedItems[index] = unsortedItems[endPos];
unsortedItems[endPos] = temp;
}
NUM_COMPARES += 2;
}
}
startPos++;
endPos--;
//You should only have to sort half the list, as each number in the first half will contain the
//sorted
NUM_ITERATIONS++;
} while (startPos < endPos);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
PrintTime("v2 ", elapsedMs);
WriteToFile(String.Format("v2 Post Sorted items : {0} Took : {1} seconds {2} minutes We did {3} ITERATIONS and {4} Compares in V2", count, elapsedMs * 0.001, (elapsedMs * 0.001) / 60, NUM_ITERATIONS, NUM_COMPARES));
if (Print)
{
PrintListToFile("V2 Post sorted items", unsortedItems);
PrintItems(String.Format("V2 Post sorted items did {0} iterations and {1} compares ", NUM_ITERATIONS, NUM_COMPARES), unsortedItems);
}
}
}
class Program
{
private static int NUMS_NUM_NUMS = 10;
private static bool PrintList = false;
private static int NUMBER_ITERATIONS = 3;
private static void ReadSettings()
{
NUMS_NUM_NUMS = Convert.ToInt32(AppSettings.ReadSetting("NUMS_NUM_NUMS"));
PrintList = (AppSettings.ReadSetting("PrintList") == "true");
NUMBER_ITERATIONS = Convert.ToInt32(AppSettings.ReadSetting("NUMBER_ITERATIONS"));
}
private static void FillRandomInts(List<int> MyItemsToSort)
{
Random r = new Random();
for (int index = 0; index < NUMS_NUM_NUMS; index++)
MyItemsToSort.Add(r.Next(0, 1000));
}
private static void FillInts(List<int> MyItemsToSort, bool constant, bool ascending)
{
if (constant)
{
for (int index = 0; index < NUMS_NUM_NUMS; index++)
{
MyItemsToSort.Add(42);
}
}
else if (ascending)
{
for (int index = 0; index < NUMS_NUM_NUMS; index++)
{
MyItemsToSort.Add(index);
}
}
else
{
for (int index = NUMS_NUM_NUMS; index > 0; index--)
{
MyItemsToSort.Add(index);
}
}
}
private static void TestAlgorithmFill(ref List<int> MyItemsToSort)
{
MyItemsToSort.Clear();
MyItemsToSort = new List<int>() { 6, 7, 8, 9, 5 };
return;
}
private static void Fill(List<int> MyItemsToSort, int testT)
{
MyItemsToSort.Clear();
if ((TestTypes)testT == TestTypes.Random)
FillRandomInts(MyItemsToSort);
else
{
bool Ascending = true;
bool constantNums = false;
TestTypes typeOfTest = (TestTypes)testT;
switch (typeOfTest)
{
case TestTypes.Ordered:
//Nums go up from 1
break;
case TestTypes.Reversed:
//nums go down to 1
Ascending = false;
break;
case TestTypes.Constant:
//All nums the same
constantNums = true;
break;
}
FillInts(MyItemsToSort, constantNums, Ascending);
}
}
class g
{
int gg;
public g(int hgh)
{
gg = hgh;
}
public void print()
{
System.Console.Write("The g class:" + gg);
}
public g()
{
System.Console.Write("Hello");
}
}
static private void arrayStuff()
{
g ggg = new g(444444);
//Shows that constructors for items in the array, dont get called when the array is created,
//but for value types you can directly set a value after the array is created
int[] myints2 = new int[10];
myints2[5] = 99;
//For reference types the const is not called either, but you CAN
//use the [] indexor to set a value as the memory has been set up
g[] myaf = new g[1000];
myaf[10] = ggg;
myaf[10].print();
//But the constructor has not been called on items in the array so the references still refer to null
//so calling anything on them will null exception
myaf[11].print();
const int mysize = 50;
int[] myints = new int[mysize];
myints[20] = 99;
List<g> mylagaa = new List<g>(mysize) { new g(3), new g(88) };
List<g> mylag = new List<g>(mysize);
//mylag[44] = ggg;
List<int> myla = new List<int>(mysize);
myla[44] = 99;
List<int> myl = new List<int>();
myl[44] = 99;
}
static void Main(string[] args)
{
ReadSettings();
//JustTestThisSoftware();
MOBsSuperSort<int>.DeleteOldTextFile();
for (int count = 0; count < NUMBER_ITERATIONS; count++)
{
System.Console.WriteLine(String.Format("About to test. Iteration : {0}" , (count+1)));
DoIt();
}
MOBsSuperSort<int>.DisplayResults();
System.Console.WriteLine("Press any key to quit");
System.Console.ReadKey();
}
private enum TestTypes
{
Random,
Ordered,
Reversed,
Constant
}
static void TestList(List<int> MyItemsToSort, String searchType)
{
//Now test to see if it worked
List<int> PerfectList = new List<int>(MyItemsToSort);
PerfectList.Sort();
IEnumerable<Tuple<int, int>> bothList = PerfectList.Zip(MyItemsToSort, Tuple.Create<int, int>);
foreach (var f in bothList)
{
if (f.Item1 != f.Item2)
{
MOBsSuperSort<int>.WriteToFile(String.Format("The list {2} was not sorted properly ! Found the value {0} and it should have been a {1}", f.Item1, f.Item2, searchType));
return;
}
}
MOBsSuperSort<int>.WriteToFile("The " + searchType + " list was sorted properly ! ");
}
static TestTypes SetTestType()
{
return TestTypes.Reversed;
}
static void JustTestThisSoftware()
{
List<int> MyItemsToSort = new List<int>(NUMS_NUM_NUMS);
TestAlgorithmFill(ref MyItemsToSort);
MOBsSuperSort<int> sorting = new MOBsSuperSort<int>(false);
sorting.SortWithoutRecursionv2(MyItemsToSort);
TestList(MyItemsToSort, "Search V1");
}
static void DoIt()
{
Guid id = Guid.NewGuid();
List<int> MyItemsToSort = new List<int>(NUMS_NUM_NUMS);
foreach (var suit in Enum.GetValues(typeof(TestTypes)))
{
//var suit = SetTestType();
System.Console.WriteLine(String.Format("Testing with sort type : {0}", suit));
MOBsSuperSort<int> sorting = new MOBsSuperSort<int>(PrintList);
sorting.WriteSessionIdToFile(id, suit.ToString());
//v1 sort test
Fill(MyItemsToSort, (int)suit);
sorting.SortWithoutRecursionv1(MyItemsToSort);
TestList(MyItemsToSort, "Search V1");
//Now test v2
Fill(MyItemsToSort, (int)suit);
sorting.SortWithoutRecursionv2(MyItemsToSort);
TestList(MyItemsToSort, "Search V2");
//Now test the built in one
Fill(MyItemsToSort, (int)suit);
sorting.SortWithoutRecursionv3(MyItemsToSort);
TestList(MyItemsToSort, "Search V3");
}
}
}
}