Skip to content

Commit ca707db

Browse files
committed
complete refactoring/cleanup into separate classes
1 parent 9c348a2 commit ca707db

13 files changed

+932
-0
lines changed

Diff for: App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
5+
</startup>
6+
</configuration>

Diff for: ArrayOperations.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace CompareSorts
5+
{
6+
public static class ArrayOperations
7+
{
8+
/* Returns an array of specified length.
9+
* Array's elements are ints dependent on 'orderOfArray':
10+
* "ascending" from 0 to length - 1
11+
* "descending" from length - 1 to 0
12+
* "random" from 0 - 99 inclusive (no check for uniqueness)
13+
*/
14+
public static int[] Make(int length, string orderOfInitialArray)
15+
{
16+
Random rnd = new Random();
17+
18+
int[] array = new int[length];
19+
for (int i = 0; i < length; i++)
20+
{
21+
switch (orderOfInitialArray)
22+
{
23+
// Ascending:
24+
case "ascending":
25+
array[i] = i;
26+
break;
27+
// Descending:
28+
case "descending":
29+
array[i] = length - i - 1;
30+
break;
31+
// Random:
32+
case "random":
33+
array[i] = rnd.Next(100);
34+
break;
35+
// Default to ascending if incorrect parameter
36+
default:
37+
array[i] = i;
38+
break;
39+
}
40+
}
41+
return array;
42+
}
43+
// Prints the current array with a message to describe the situation
44+
public static void Print(int[] array, string text)
45+
{
46+
int n = array.Length;
47+
for (int i = 0; i < n; i++)
48+
Console.Write(array[i].ToString().PadLeft(3));
49+
Console.WriteLine(" " + text);
50+
}
51+
52+
//Swaps two ints in an array
53+
public static void Swap(int[] array, int p1, int p2)
54+
{
55+
int temp = array[p1];
56+
array[p1] = array[p2];
57+
array[p2] = temp;
58+
}
59+
}
60+
}

Diff for: Bubble.cs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace CompareSorts
5+
{
6+
public static class Bubble
7+
{
8+
/* Sorts an array using Bubble sort algorithm.
9+
* Prints updates if printUpdates is true.
10+
* Optimised: will only loop once through an array once after it's sorted,
11+
* and distinguishes between 'sorted' and 'unsorted' parts.
12+
* Time complexity: Worst = n^2, Best = n, Average = n^2.
13+
*/
14+
public static int[] Sort(int[] array, bool printUpdates)
15+
{
16+
ulong comparisons = 0;
17+
ulong swaps = 0;
18+
// Get (length - 1) because we compare with (j + 1)
19+
int n = array.Length - 1;
20+
bool swapped = false;
21+
22+
/* Loop through the array, checking that each element is smaller than the next.
23+
* If not, swap those two elements and mark 'swapped' to true.
24+
* Stop looping when the whole array has been checked without making a swap.
25+
*/
26+
do
27+
{
28+
swapped = false;
29+
for (int j = 0; j < n; j++)
30+
{
31+
// Since the next line will compare elements, add 1 to comparisons
32+
comparisons++;
33+
if (printUpdates)
34+
Console.Write("^ Compare " + array[j].ToString().PadLeft(2) +
35+
" with " + array[j + 1].ToString().PadLeft(2));
36+
if ((array[j] > array[j + 1]))
37+
{
38+
// Swap them, set 'swapped' to true, count 1 more swap
39+
ArrayOperations.Swap(array, j, j + 1);
40+
swapped = true;
41+
swaps++;
42+
43+
// Since a swap was made, print array in current state and say what was swapped
44+
if (printUpdates)
45+
{
46+
Console.WriteLine(", it's bigger so swap:");
47+
string message = "swapped " + array[j + 1].ToString().PadLeft(2) +
48+
" with " + array[j].ToString().PadLeft(2);
49+
ArrayOperations.Print(array, message);
50+
}
51+
}
52+
else if (printUpdates)
53+
{
54+
Console.WriteLine(", it's not bigger so don't swap");
55+
}
56+
}
57+
// After each loop, we know the biggest element must be sorted
58+
// So, it can be ignored on the next pass
59+
n--;
60+
} while (swapped);
61+
62+
// Print number of comparisons and swaps that were performed
63+
if (printUpdates)
64+
{
65+
Console.WriteLine("Since no swaps were made this pass, we're done!");
66+
Console.WriteLine("\nNumber of comparisons: " + comparisons +
67+
"\nNumber of swaps: " + swaps);
68+
}
69+
70+
CountAndDisplay.totalComparisonsBubble += comparisons;
71+
CountAndDisplay.totalSwapsBubble += swaps;
72+
return array;
73+
}
74+
}
75+
}

Diff for: CallSpecifiedSort.cs

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace CompareSorts
5+
{
6+
public static class CallSpecifiedSort
7+
{
8+
public const string a = "ascending",
9+
d = "descending",
10+
r = "random";
11+
12+
public const string B = "Bubble",
13+
S = "Selection",
14+
I = "Insertion",
15+
Q = "Quick",
16+
M = "Merge",
17+
A = "All";
18+
19+
public static void Init(string sortingAlgorithm, string orderOfInitialArray, ulong numberOfSorts, int lengthOfArray, bool printUpdates)
20+
{
21+
/* Initialise specified number of arrays of ints
22+
* Pass each one to Bubble||Selection||Insertion||Quick sort
23+
* Count total number of comparisons/swaps/shuffles/insertions as required
24+
*/
25+
for (int i = 0; i < (int)numberOfSorts; i++)
26+
{
27+
if (numberOfSorts > 1 && printUpdates == true)
28+
Console.WriteLine("\n\nRound {0}:\n", (i + 1));
29+
30+
// Create array
31+
int[] array = ArrayOperations.Make(lengthOfArray, orderOfInitialArray);
32+
33+
if (printUpdates && sortingAlgorithm != A)
34+
ArrayOperations.Print(array, "initial array");
35+
36+
switch (sortingAlgorithm)
37+
{
38+
case B:
39+
Bubble.Sort(array, printUpdates);
40+
break;
41+
case S:
42+
Selection.Sort(array, printUpdates);
43+
break;
44+
case I:
45+
Insertion.Sort(array, printUpdates);
46+
break;
47+
case Q:
48+
Quick.Sort(array, 0, array.Length - 1, printUpdates);
49+
break;
50+
case M:
51+
Merge.Sort(array, printUpdates);
52+
break;
53+
default:
54+
CallAllSorts(array, printUpdates);
55+
break;
56+
}
57+
}
58+
59+
}
60+
61+
/* Calls all sorting algorithms and prints additional information to specify which sort is which */
62+
private static void CallAllSorts(int[] array, bool printUpdates)
63+
{
64+
// Copy each array (since they're arrays of ints, shallow copy is enough)
65+
int[] array1 = (int[])array.Clone();
66+
int[] array2 = (int[])array.Clone();
67+
int[] array3 = (int[])array.Clone();
68+
int[] array4 = (int[])array.Clone();
69+
70+
if (printUpdates)
71+
{
72+
Console.WriteLine("\nBubble sort:" +
73+
"\n************");
74+
ArrayOperations.Print(array, "initial array");
75+
}
76+
Bubble.Sort(array, printUpdates);
77+
78+
if (printUpdates)
79+
{
80+
Console.WriteLine("\nSelection sort:" +
81+
"\n***************");
82+
ArrayOperations.Print(array1, "initial array");
83+
}
84+
Selection.Sort(array1, printUpdates);
85+
86+
if (printUpdates)
87+
{
88+
Console.WriteLine("\nInsertion sort:" +
89+
"\n***************");
90+
ArrayOperations.Print(array2, "initial array");
91+
}
92+
Insertion.Sort(array2, printUpdates);
93+
94+
if (printUpdates)
95+
{
96+
Console.WriteLine("\nQuick sort:" +
97+
"\n***********");
98+
ArrayOperations.Print(array3, "initial array");
99+
}
100+
Quick.Sort(array3, 0, array3.Length - 1, printUpdates);
101+
102+
if (printUpdates)
103+
{
104+
Console.WriteLine("\nMerge sort:" +
105+
"\n***********");
106+
ArrayOperations.Print(array4, "initial array");
107+
}
108+
Merge.Sort(array4, printUpdates);
109+
110+
if (printUpdates)
111+
Console.WriteLine();
112+
}
113+
}
114+
}

Diff for: CompareSorts.csproj

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E6097C8C-A718-4CC7-B6A7-148D065A1273}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>compare_sorting_algorithms</RootNamespace>
10+
<AssemblyName>compare-sorting-algorithms</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Net.Http" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="ArrayOperations.cs" />
46+
<Compile Include="Bubble.cs" />
47+
<Compile Include="CountAndDisplay.cs" />
48+
<Compile Include="CallSpecifiedSort.cs" />
49+
<Compile Include="Insertion.cs" />
50+
<Compile Include="Merge.cs" />
51+
<Compile Include="Program.cs" />
52+
<Compile Include="Properties\AssemblyInfo.cs" />
53+
<Compile Include="Quick.cs" />
54+
<Compile Include="Selection.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="App.config" />
58+
</ItemGroup>
59+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
60+
</Project>

0 commit comments

Comments
 (0)