Skip to content

Commit

Permalink
Added Cycle Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanjukes committed Jun 16, 2020
1 parent 8e39cb8 commit fb48d9b
Show file tree
Hide file tree
Showing 35 changed files with 146 additions and 120 deletions.
Binary file modified .vs/SortingAlgorithmVisualisation/v16/.suo
Binary file not shown.
93 changes: 93 additions & 0 deletions SortingAlgorithmVisualisation/Algorithms/CycleSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SortingAlgorithmVisualisation.Algorithms
{
class CycleSort : AlgorithmBase
{
public override int elementCount { get; set; }

public override void BeginAlgorithm(int[] elements)
{
elementCount = elements.Length;

StartCycleSort(elements);

DisplaySort.SortComplete = true;

ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
}

private void StartCycleSort(int[] elements)
{
for(int i = 0; i < elementCount - 1; i++)
{
int currentValue = elements[i];
int index = i + GetLargerThanCount(elements, currentValue, i);

if(index == i)
{
continue;
}

if(currentValue == elements[index])
{
index++;
}

int temp = elements[index];
elements[index] = currentValue;
currentValue = temp; //Moves to next element
ReDraw(elements, index);

while (index != i) //Keeps looping until the next line is i + 0, meaning that the cycle is over
{
index = i + GetLargerThanCount(elements, currentValue, i);

while(currentValue == elements[index])
{
index++;
}

temp = elements[index];
elements[index] = currentValue;
currentValue = temp;
ReDraw(elements, index);
}
}
}

private int GetLargerThanCount(int[] elements, int currentValue, int lowerLimit)
{
int count = 0;

for (int j = lowerLimit + 1; j < elementCount; j++)
{
if (elements[j] < currentValue)
{
count++;
}
}

return count;
}

private void ReDraw(int[] elements, int index)
{
for (int i = 0; i < elementCount; i++)
{
graphics.FillRectangle(new SolidBrush(SystemColors.ActiveBorder), i * maxWidth, 0, maxWidth, maxHeight);
graphics.FillRectangle(new SolidBrush(Color.Black), i * maxWidth, maxHeight - elements[i], maxWidth, elements[i]);
}

graphics.FillRectangle(new SolidBrush(Color.DarkRed), index * maxWidth, maxHeight - elements[index], maxWidth, elements[index]);
Thread.Sleep(threadDelay);
}
}
}
1 change: 1 addition & 0 deletions SortingAlgorithmVisualisation/Algorithms/OddEvenSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SortingAlgorithmVisualisation.Algorithms
{
Expand Down
18 changes: 18 additions & 0 deletions SortingAlgorithmVisualisation/Forms/MainMenuForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion SortingAlgorithmVisualisation/Forms/MainMenuForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ private bool SetAlgorithmData()
algorithm = new OddEvenSort();
algorithm.timeComplexity = ("O(n²)");
algorithm.spaceComplexity = ("O(1)");
break;
break;
case "Cycle Sort":
algorithm = new CycleSort();
algorithm.timeComplexity = ("O(n²)");
algorithm.spaceComplexity = ("O(1)");
break;
case null:
MessageBox.Show("Please select an algorithm","Error");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<TargetCulture>en</TargetCulture>
<ProductName>Sorting Algorithm Visualiser</ProductName>
<PublisherName>Nathan Jukes</PublisherName>
<ApplicationRevision>5</ApplicationRevision>
<ApplicationRevision>6</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
Expand Down Expand Up @@ -81,6 +81,7 @@
<Compile Include="Algorithms\BubbleSort.cs" />
<Compile Include="Algorithms\CocktailSort.cs" />
<Compile Include="Algorithms\CombSort.cs" />
<Compile Include="Algorithms\CycleSort.cs" />
<Compile Include="Algorithms\GnomeSort.cs" />
<Compile Include="Algorithms\HeapSort.cs" />
<Compile Include="Algorithms\InsertionSort.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
<assemblyIdentity name="SortingAlgorithmVisualisation.application" version="1.0.0.5" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
<assemblyIdentity name="SortingAlgorithmVisualisation.application" version="1.0.0.6" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
<description asmv2:publisher="Nathan Jukes" asmv2:product="Sorting Algorithm Visualiser" xmlns="urn:schemas-microsoft-com:asm.v1" />
<deployment install="true" mapFileExtensions="true" />
<compatibleFrameworks xmlns="urn:schemas-microsoft-com:clickonce.v2">
<framework targetVersion="4.6.1" profile="Full" supportedRuntime="4.0.30319" />
</compatibleFrameworks>
<dependency>
<dependentAssembly dependencyType="install" codebase="SortingAlgorithmVisualisation.exe.manifest" size="3578">
<assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.5" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.6" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>qGsPZ30xKFPlDgjcVVyWGSKf2aHGrzpf5IY1JLKUbyI=</dsig:DigestValue>
<dsig:DigestValue>s+ZIU2P6mlGzoRNVe7imAEOMP6B/NNK8I5jPamee5JQ=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
<asmv1:assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.5" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<asmv1:assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.6" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<application />
<entryPoint>
<assemblyIdentity name="SortingAlgorithmVisualisation" version="1.0.0.0" language="neutral" processorArchitecture="msil" />
Expand Down Expand Up @@ -42,14 +42,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="SortingAlgorithmVisualisation.exe" size="626696">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="SortingAlgorithmVisualisation.exe" size="627720">
<assemblyIdentity name="SortingAlgorithmVisualisation" version="1.0.0.0" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>JqWDp9YGUyQCnckAgzvcpzoePQJ8tYByCR0irr/uffk=</dsig:DigestValue>
<dsig:DigestValue>5Ns1YxxulkL82DbuMQvuz4/8ezrRdVSAiuSU0Ixq/gM=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
<assemblyIdentity name="SortingAlgorithmVisualisation.application" version="1.0.0.5" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
<assemblyIdentity name="SortingAlgorithmVisualisation.application" version="1.0.0.6" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
<description asmv2:publisher="Nathan Jukes" asmv2:product="Sorting Algorithm Visualiser" xmlns="urn:schemas-microsoft-com:asm.v1" />
<deployment install="true" mapFileExtensions="true" />
<compatibleFrameworks xmlns="urn:schemas-microsoft-com:clickonce.v2">
<framework targetVersion="4.6.1" profile="Full" supportedRuntime="4.0.30319" />
</compatibleFrameworks>
<dependency>
<dependentAssembly dependencyType="install" codebase="SortingAlgorithmVisualisation.exe.manifest" size="3578">
<assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.5" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.6" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>gEV41A2KgdEocAvUZjAcNXznpuCh2XUqlcI9UKb47gI=</dsig:DigestValue>
<dsig:DigestValue>hCQdAwQ46af4s4F9ZcH57Clrp4xWtaI2cwN6dJ6zxBo=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
<asmv1:assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.5" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<asmv1:assemblyIdentity name="SortingAlgorithmVisualisation.exe" version="1.0.0.6" publicKeyToken="0000000000000000" language="en" processorArchitecture="msil" type="win32" />
<application />
<entryPoint>
<assemblyIdentity name="SortingAlgorithmVisualisation" version="1.0.0.0" language="neutral" processorArchitecture="msil" />
Expand Down Expand Up @@ -42,14 +42,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="SortingAlgorithmVisualisation.exe" size="624136">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="SortingAlgorithmVisualisation.exe" size="624648">
<assemblyIdentity name="SortingAlgorithmVisualisation" version="1.0.0.0" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>kUD1seuNfwlAhutBak/QwMUnrnQlPVuHikizm7R2GAA=</dsig:DigestValue>
<dsig:DigestValue>krjawEi/FwRECaGIXyzeRXRS+p+II5u3tQZbmI4Bxcs=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file not shown.

This file was deleted.

Binary file not shown.
Loading

0 comments on commit fb48d9b

Please sign in to comment.