Skip to content

Commit

Permalink
Cleaned up complexity attributes and removed redundant status' (Algor…
Browse files Browse the repository at this point in the history
…ithm complete in each sort)
  • Loading branch information
nathanjukes committed Jun 16, 2020
1 parent fb48d9b commit 3812e0f
Show file tree
Hide file tree
Showing 53 changed files with 311 additions and 197 deletions.
Binary file modified .vs/SortingAlgorithmVisualisation/v16/.suo
Binary file not shown.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<a href="https://scrutinizer-ci.com/g/pH7Software/pH7-Social-Dating-CMS/build-status/master">
<img src="https://scrutinizer-ci.com/g/pH7Software/pH7-Social-Dating-CMS/badges/build.png?b=master">
</a>
<a href="https://img.shields.io/badge/version-v1.04-blue">
<img src="https://img.shields.io/badge/version-v1.04-blue">
<a href="https://img.shields.io/badge/version-v1.05-blue">
<img src="https://img.shields.io/badge/version-v1.05-blue">
</a>
<a href="https://github.com/nathanjukes/Sorting-Algorithm-Visualisation/blob/master/LICENSE.md">
<img src="https://img.shields.io/github/license/Naereen/StrapDown.js.svg">
Expand All @@ -34,6 +34,8 @@ Or download here:

## Change Log

- 1.05 - Added Cycle Sort

- 1.04 - Added Odd-Even Sort

- 1.03 - Added Gnome Sort, Changed GUI Sizing
Expand Down
57 changes: 50 additions & 7 deletions SortingAlgorithmVisualisation/Algorithms/AlgorithmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,59 @@ public abstract class AlgorithmBase
public abstract int elementCount { get; set; }
public abstract void BeginAlgorithm(int[] elements);

protected void ShowCompletedDisplay(Graphics graphics, int maxWidth, int maxHeight, int[] elements, int threadDelay)
public void ShowCompletedDisplay(int[] elements)
{
FormattingDisplay formatDisplay = new FormattingDisplay();
ShowAllGreen(elements);
}

private void ShowAllGreen(int[] elements)
{
if (threadDelay == 200)
{
threadDelay = 80;
}
else if (threadDelay == 0)
{
threadDelay = 1;
}

for (int i = 0; i < elements.Length; i++)
{
graphics.FillRectangle(new SolidBrush(Color.FromArgb(83, 153, 182)), i * maxWidth, maxHeight - elements[i], maxWidth, elements[i]);
Thread.Sleep(threadDelay);
}
}

formatDisplay.ShowAllGreen(graphics, maxWidth, maxHeight, elements, threadDelay);
public void SetComplexity(int complexityRangeValue)
{
switch (complexityRangeValue)
{
case 0:
timeComplexity = "O(nLog(n))";
spaceComplexity = "O(1)";
break;
case 1:
timeComplexity = "O(nLog(n))";
spaceComplexity = "O(Log(n))";
break;
case 2:
timeComplexity = "O(nLog(n))";
spaceComplexity = "O(n)";
break;
case 3:
timeComplexity = "O(nk)";
spaceComplexity = "O(n+k)";
break;
case 4:
timeComplexity = "O(n²)";
spaceComplexity = "O(1)";
break;
}
}

protected void SwapElements(int index1, int index2, int[] elements, int sortType)
{
switch(sortType)
switch (sortType)
{
case 0:
graphics.FillRectangle(new SolidBrush(Color.DarkRed), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
Expand All @@ -58,7 +101,7 @@ protected void SwapElements(int index1, int index2, int[] elements, int sortType
elements[index1] = elements[index2];
elements[index2] = tempValue;

switch(sortType)
switch (sortType)
{
default:
graphics.FillRectangle(new SolidBrush(Color.Black), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
Expand All @@ -68,11 +111,11 @@ protected void SwapElements(int index1, int index2, int[] elements, int sortType
graphics.FillRectangle(new SolidBrush(Color.DarkRed), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
graphics.FillRectangle(new SolidBrush(Color.Black), index2 * maxWidth, maxHeight - elements[index2], maxWidth, elements[index2]);

if(GetType().Name.Contains("Selection"))
if (GetType().Name.Contains("Selection"))
{
Thread.Sleep(threadDelay);
}

graphics.FillRectangle(new SolidBrush(Color.Black), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
break;
case 2:
Expand Down
4 changes: 1 addition & 3 deletions SortingAlgorithmVisualisation/Algorithms/BogoSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class BogoSort : AlgorithmBase
private int[] elementsCopy;
public override void BeginAlgorithm(int[] elements)
{
elementCount = elements.Length;

elementsCopy = (int[])elements.Clone();

StartBogoSort(elements);
Expand Down Expand Up @@ -54,7 +52,7 @@ private void StartBogoSort(int[] elements)
if (CheckIfSorted(elements))
{
DisplaySort.SortComplete = true;
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
ShowCompletedDisplay(elements);
break;
}
else
Expand Down
10 changes: 2 additions & 8 deletions SortingAlgorithmVisualisation/Algorithms/BubbleSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ class BubbleSort : AlgorithmBase

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

Sort(elements, graphics, maxWidth, maxHeight);

DisplaySort.SortComplete = true;
StartBubbleSort(elements);
}

private void Sort(int[] elements, Graphics graphics, int maxWidth, int maxHeight)
private void StartBubbleSort(int[] elements)
{
for (int i = 0; i < elementCount; i++)
{
Expand All @@ -34,8 +30,6 @@ private void Sort(int[] elements, Graphics graphics, int maxWidth, int maxHeight
}
}
}

ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
}
}
}
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/CocktailSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ class CocktailSort : AlgorithmBase

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

StartCocktailSort(elements);

DisplaySort.SortComplete = true;

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

private void StartCocktailSort(int[] elements)
Expand Down
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/CombSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ class CombSort : AlgorithmBase

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

StartCombSort(elements);

DisplaySort.SortComplete = true;

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

private void StartCombSort(int[] elements)
Expand Down
8 changes: 1 addition & 7 deletions SortingAlgorithmVisualisation/Algorithms/CycleSort.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
 using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
Expand All @@ -15,13 +15,7 @@ class CycleSort : AlgorithmBase

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)
Expand Down
7 changes: 0 additions & 7 deletions SortingAlgorithmVisualisation/Algorithms/GnomeSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@ class GnomeSort : AlgorithmBase

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

StartGnomeSort(elements);

DisplaySort.SortComplete = true;

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

private void StartGnomeSort(int[] elements)
{
for(int i = 0; i < elementCount; i++)
{

if(i + 1 < elementCount && i >= 0 && elements[i] > elements[i + 1])
{
SwapElements(i, i + 1, elements, 1);
Expand Down
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/HeapSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class HeapSort : AlgorithmBase

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

sortedLength = elementCount;

if (elementCount % 2 == 0)
Expand All @@ -33,10 +31,6 @@ public override void BeginAlgorithm(int[] elements)
Heapify(elements);

DeleteElements(elements);

DisplaySort.SortComplete = true;

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

private void Heapify(int[] elements)
Expand Down
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/InsertionSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ class InsertionSort : AlgorithmBase

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

StartInsertionSort(elements);

DisplaySort.SortComplete = true;

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

private void StartInsertionSort(int[] elements)
Expand Down
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/MergeSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ class MergeSort : AlgorithmBase
private int qCount = 0;
public override void BeginAlgorithm(int[] elements)
{
elementCount = elements.Length;

SplitArray(elements);

Thread.Sleep(200);

DisplaySort.SortComplete = true;

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

private int[] SplitArray(int[] unsortedElements)
Expand Down
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/OddEvenSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ class OddEvenSort : AlgorithmBase

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

StartOddEvenSort(elements);

DisplaySort.SortComplete = true;

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

private void StartOddEvenSort(int[] elements)
Expand Down
17 changes: 6 additions & 11 deletions SortingAlgorithmVisualisation/Algorithms/QuickSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@ class QuickSort : AlgorithmBase

public override void BeginAlgorithm(int[] elements)
{
elementCount = elements.Length;
QuickSortStart(elements, 0, elementCount - 1);

DisplaySort.SortComplete = true;

ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
StartQuickSort(elements, 0, elementCount - 1);
}

private void QuickSortStart(int[] elements, int startIndex, int endIndex)
private void StartQuickSort(int[] elements, int startIndex, int endIndex)
{
if(startIndex >= endIndex)
{
return;
}
else
{
int midIndex = findNextIndex(elements, startIndex, endIndex);
int midIndex = FindNextIndex(elements, startIndex, endIndex);

QuickSortStart(elements, startIndex, midIndex - 1);
QuickSortStart(elements, midIndex + 1, endIndex);
StartQuickSort(elements, startIndex, midIndex - 1);
StartQuickSort(elements, midIndex + 1, endIndex);
}
}

private int findNextIndex(int[] elements, int startIndex, int endIndex)
private int FindNextIndex(int[] elements, int startIndex, int endIndex)
{
int pivotValue = elements[endIndex];
int pivotIndex = startIndex;
Expand Down
6 changes: 1 addition & 5 deletions SortingAlgorithmVisualisation/Algorithms/RadixSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ class RadixSort : AlgorithmBase
public override int elementCount { get; set; }

private string[] elementDuplicates;

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

DisplaySort.SortComplete = true;

StartRadixSort(elements);
}

Expand All @@ -37,7 +34,6 @@ private void StartRadixSort(int[] elements)
}

Thread.Sleep(220);
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
}

private void CountSort(int[] elements, int LengthToMinus)
Expand Down
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/SelectionSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ class SelectionSort : AlgorithmBase

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

StartSelectionSort(elements);

DisplaySort.SortComplete = true;

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

private void StartSelectionSort(int[] elements)
Expand Down
6 changes: 0 additions & 6 deletions SortingAlgorithmVisualisation/Algorithms/ShellSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ class ShellSort : AlgorithmBase
public override int elementCount { get; set; }
public override void BeginAlgorithm(int[] elements)
{
elementCount = elements.Length;

StartShellSort(elements);

DisplaySort.SortComplete = true;

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

private void StartShellSort(int[] elements)
Expand Down
Loading

0 comments on commit 3812e0f

Please sign in to comment.