-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (80 loc) · 3.46 KB
/
Program.cs
File metadata and controls
89 lines (80 loc) · 3.46 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
using System.Text;
using static System.Console;
namespace Sort
{
internal class Program
{
static void Main(string[] args)
{
string? input;
ConsoleKey keyEntered;
SortStatistic sortingStats;
List<IComparable> originalList = new List<IComparable>(), sortedList = new List<IComparable>();
StringBuilder results = new StringBuilder();
bool validInput;
// Get type of data to sort from user
WriteLine("What kind of data do you want to sort? Press T for text or N for numbers: ");
keyEntered = ReadKey().Key;
while (keyEntered != ConsoleKey.T && keyEntered != ConsoleKey.N)
{
WriteLine("\nYour input was invalid. Try again: Press T for text or N for numbers: ");
keyEntered = ReadKey().Key;
}
// Get strings from user
if (keyEntered == ConsoleKey.T)
{
WriteLine("\nEnter a list of words seperated by a comma and a space (ex: Apple, Orange, Green): ");
validInput = false;
while (!validInput)
{
try
{
input = ReadLine() ?? throw new ArgumentNullException();
originalList = input.Split(", ").Cast<IComparable>().ToList();
if (originalList.Count > 1)
validInput = true;
}
catch (System.Exception) { }
if (!validInput)
WriteLine("Your words were an invalid input. Try again: ");
}
}
// Get numbers from user
else
{
WriteLine("\nEnter a list of numbers seperated by a comma and a space (ex: 1, -23, 5, 102): ");
validInput = false;
while (!validInput)
{
try
{
input = ReadLine() ?? throw new ArgumentNullException();
originalList = input.Split(", ").Select(x => float.Parse(x)).Cast<IComparable>().ToList();
if (originalList.Count > 1)
validInput = true;
}
catch (System.Exception) { }
if (!validInput)
WriteLine("Your numbers were an invalid input. Try again: ");
}
}
results.AppendLine("\nResults:");
// Bubble Sort
sortedList = new List<IComparable>(originalList);
sortingStats = sortedList.BubbleSort();
results.AppendLine($"\nBubble Sort returned: {string.Join(", ", sortedList)}");
results.AppendLine(sortingStats.Results);
// Quick Sort
sortedList = new List<IComparable>(originalList);
sortingStats = sortedList.QuickSort();
results.AppendLine($"\nQuick Sort returned: {string.Join(", ", sortedList)}");
results.AppendLine(sortingStats.Results);
// Cocktail Sort
sortedList = new List<IComparable>(originalList);
sortingStats = sortedList.CocktailSort();
results.AppendLine($"\nCocktail Sort returned: {string.Join(", ", sortedList)}");
results.AppendLine(sortingStats.Results);
WriteLine(results.ToString());
}
}
}