Skip to content

Commit 3812e0f

Browse files
committed
Cleaned up complexity attributes and removed redundant status' (Algorithm complete in each sort)
1 parent fb48d9b commit 3812e0f

File tree

53 files changed

+311
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+311
-197
lines changed
-35 KB
Binary file not shown.

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<a href="https://scrutinizer-ci.com/g/pH7Software/pH7-Social-Dating-CMS/build-status/master">
99
<img src="https://scrutinizer-ci.com/g/pH7Software/pH7-Social-Dating-CMS/badges/build.png?b=master">
1010
</a>
11-
<a href="https://img.shields.io/badge/version-v1.04-blue">
12-
<img src="https://img.shields.io/badge/version-v1.04-blue">
11+
<a href="https://img.shields.io/badge/version-v1.05-blue">
12+
<img src="https://img.shields.io/badge/version-v1.05-blue">
1313
</a>
1414
<a href="https://github.com/nathanjukes/Sorting-Algorithm-Visualisation/blob/master/LICENSE.md">
1515
<img src="https://img.shields.io/github/license/Naereen/StrapDown.js.svg">
@@ -34,6 +34,8 @@ Or download here:
3434

3535
## Change Log
3636

37+
- 1.05 - Added Cycle Sort
38+
3739
- 1.04 - Added Odd-Even Sort
3840

3941
- 1.03 - Added Gnome Sort, Changed GUI Sizing

SortingAlgorithmVisualisation/Algorithms/AlgorithmBase.cs

+50-7
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,59 @@ public abstract class AlgorithmBase
2222
public abstract int elementCount { get; set; }
2323
public abstract void BeginAlgorithm(int[] elements);
2424

25-
protected void ShowCompletedDisplay(Graphics graphics, int maxWidth, int maxHeight, int[] elements, int threadDelay)
25+
public void ShowCompletedDisplay(int[] elements)
2626
{
27-
FormattingDisplay formatDisplay = new FormattingDisplay();
27+
ShowAllGreen(elements);
28+
}
29+
30+
private void ShowAllGreen(int[] elements)
31+
{
32+
if (threadDelay == 200)
33+
{
34+
threadDelay = 80;
35+
}
36+
else if (threadDelay == 0)
37+
{
38+
threadDelay = 1;
39+
}
40+
41+
for (int i = 0; i < elements.Length; i++)
42+
{
43+
graphics.FillRectangle(new SolidBrush(Color.FromArgb(83, 153, 182)), i * maxWidth, maxHeight - elements[i], maxWidth, elements[i]);
44+
Thread.Sleep(threadDelay);
45+
}
46+
}
2847

29-
formatDisplay.ShowAllGreen(graphics, maxWidth, maxHeight, elements, threadDelay);
48+
public void SetComplexity(int complexityRangeValue)
49+
{
50+
switch (complexityRangeValue)
51+
{
52+
case 0:
53+
timeComplexity = "O(nLog(n))";
54+
spaceComplexity = "O(1)";
55+
break;
56+
case 1:
57+
timeComplexity = "O(nLog(n))";
58+
spaceComplexity = "O(Log(n))";
59+
break;
60+
case 2:
61+
timeComplexity = "O(nLog(n))";
62+
spaceComplexity = "O(n)";
63+
break;
64+
case 3:
65+
timeComplexity = "O(nk)";
66+
spaceComplexity = "O(n+k)";
67+
break;
68+
case 4:
69+
timeComplexity = "O(n²)";
70+
spaceComplexity = "O(1)";
71+
break;
72+
}
3073
}
3174

3275
protected void SwapElements(int index1, int index2, int[] elements, int sortType)
3376
{
34-
switch(sortType)
77+
switch (sortType)
3578
{
3679
case 0:
3780
graphics.FillRectangle(new SolidBrush(Color.DarkRed), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
@@ -58,7 +101,7 @@ protected void SwapElements(int index1, int index2, int[] elements, int sortType
58101
elements[index1] = elements[index2];
59102
elements[index2] = tempValue;
60103

61-
switch(sortType)
104+
switch (sortType)
62105
{
63106
default:
64107
graphics.FillRectangle(new SolidBrush(Color.Black), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
@@ -68,11 +111,11 @@ protected void SwapElements(int index1, int index2, int[] elements, int sortType
68111
graphics.FillRectangle(new SolidBrush(Color.DarkRed), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
69112
graphics.FillRectangle(new SolidBrush(Color.Black), index2 * maxWidth, maxHeight - elements[index2], maxWidth, elements[index2]);
70113

71-
if(GetType().Name.Contains("Selection"))
114+
if (GetType().Name.Contains("Selection"))
72115
{
73116
Thread.Sleep(threadDelay);
74117
}
75-
118+
76119
graphics.FillRectangle(new SolidBrush(Color.Black), index1 * maxWidth, maxHeight - elements[index1], maxWidth, elements[index1]);
77120
break;
78121
case 2:

SortingAlgorithmVisualisation/Algorithms/BogoSort.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class BogoSort : AlgorithmBase
1515
private int[] elementsCopy;
1616
public override void BeginAlgorithm(int[] elements)
1717
{
18-
elementCount = elements.Length;
19-
2018
elementsCopy = (int[])elements.Clone();
2119

2220
StartBogoSort(elements);
@@ -54,7 +52,7 @@ private void StartBogoSort(int[] elements)
5452
if (CheckIfSorted(elements))
5553
{
5654
DisplaySort.SortComplete = true;
57-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
55+
ShowCompletedDisplay(elements);
5856
break;
5957
}
6058
else

SortingAlgorithmVisualisation/Algorithms/BubbleSort.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ class BubbleSort : AlgorithmBase
1515

1616
public override void BeginAlgorithm(int[] elements)
1717
{
18-
elementCount = elements.Length;
19-
20-
Sort(elements, graphics, maxWidth, maxHeight);
21-
22-
DisplaySort.SortComplete = true;
18+
StartBubbleSort(elements);
2319
}
2420

25-
private void Sort(int[] elements, Graphics graphics, int maxWidth, int maxHeight)
21+
private void StartBubbleSort(int[] elements)
2622
{
2723
for (int i = 0; i < elementCount; i++)
2824
{
@@ -34,8 +30,6 @@ private void Sort(int[] elements, Graphics graphics, int maxWidth, int maxHeight
3430
}
3531
}
3632
}
37-
38-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
3933
}
4034
}
4135
}

SortingAlgorithmVisualisation/Algorithms/CocktailSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ class CocktailSort : AlgorithmBase
1515

1616
public override void BeginAlgorithm(int[] elements)
1717
{
18-
elementCount = elements.Length;
19-
2018
StartCocktailSort(elements);
21-
22-
DisplaySort.SortComplete = true;
23-
24-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2519
}
2620

2721
private void StartCocktailSort(int[] elements)

SortingAlgorithmVisualisation/Algorithms/CombSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ class CombSort : AlgorithmBase
1313

1414
public override void BeginAlgorithm(int[] elements)
1515
{
16-
elementCount = elements.Length;
17-
1816
StartCombSort(elements);
19-
20-
DisplaySort.SortComplete = true;
21-
22-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2317
}
2418

2519
private void StartCombSort(int[] elements)

SortingAlgorithmVisualisation/Algorithms/CycleSort.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
 using System;
22
using System.Collections.Generic;
33
using System.Drawing;
44
using System.Linq;
@@ -15,13 +15,7 @@ class CycleSort : AlgorithmBase
1515

1616
public override void BeginAlgorithm(int[] elements)
1717
{
18-
elementCount = elements.Length;
19-
2018
StartCycleSort(elements);
21-
22-
DisplaySort.SortComplete = true;
23-
24-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2519
}
2620

2721
private void StartCycleSort(int[] elements)

SortingAlgorithmVisualisation/Algorithms/GnomeSort.cs

-7
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,13 @@ class GnomeSort : AlgorithmBase
1313

1414
public override void BeginAlgorithm(int[] elements)
1515
{
16-
elementCount = elements.Length;
17-
1816
StartGnomeSort(elements);
19-
20-
DisplaySort.SortComplete = true;
21-
22-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2317
}
2418

2519
private void StartGnomeSort(int[] elements)
2620
{
2721
for(int i = 0; i < elementCount; i++)
2822
{
29-
3023
if(i + 1 < elementCount && i >= 0 && elements[i] > elements[i + 1])
3124
{
3225
SwapElements(i, i + 1, elements, 1);

SortingAlgorithmVisualisation/Algorithms/HeapSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class HeapSort : AlgorithmBase
2020

2121
public override void BeginAlgorithm(int[] elements)
2222
{
23-
elementCount = elements.Length;
24-
2523
sortedLength = elementCount;
2624

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

3533
DeleteElements(elements);
36-
37-
DisplaySort.SortComplete = true;
38-
39-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
4034
}
4135

4236
private void Heapify(int[] elements)

SortingAlgorithmVisualisation/Algorithms/InsertionSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ class InsertionSort : AlgorithmBase
1414

1515
public override void BeginAlgorithm(int[] elements)
1616
{
17-
elementCount = elements.Length;
18-
1917
StartInsertionSort(elements);
20-
21-
DisplaySort.SortComplete = true;
22-
23-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2418
}
2519

2620
private void StartInsertionSort(int[] elements)

SortingAlgorithmVisualisation/Algorithms/MergeSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@ class MergeSort : AlgorithmBase
1717
private int qCount = 0;
1818
public override void BeginAlgorithm(int[] elements)
1919
{
20-
elementCount = elements.Length;
21-
2220
SplitArray(elements);
2321

2422
Thread.Sleep(200);
25-
26-
DisplaySort.SortComplete = true;
27-
28-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2923
}
3024

3125
private int[] SplitArray(int[] unsortedElements)

SortingAlgorithmVisualisation/Algorithms/OddEvenSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ class OddEvenSort : AlgorithmBase
1313

1414
public override void BeginAlgorithm(int[] elements)
1515
{
16-
elementCount = elements.Length;
17-
1816
StartOddEvenSort(elements);
19-
20-
DisplaySort.SortComplete = true;
21-
22-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2317
}
2418

2519
private void StartOddEvenSort(int[] elements)

SortingAlgorithmVisualisation/Algorithms/QuickSort.cs

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,25 @@ class QuickSort : AlgorithmBase
1414

1515
public override void BeginAlgorithm(int[] elements)
1616
{
17-
elementCount = elements.Length;
18-
QuickSortStart(elements, 0, elementCount - 1);
19-
20-
DisplaySort.SortComplete = true;
21-
22-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
17+
StartQuickSort(elements, 0, elementCount - 1);
2318
}
2419

25-
private void QuickSortStart(int[] elements, int startIndex, int endIndex)
20+
private void StartQuickSort(int[] elements, int startIndex, int endIndex)
2621
{
2722
if(startIndex >= endIndex)
2823
{
2924
return;
3025
}
3126
else
3227
{
33-
int midIndex = findNextIndex(elements, startIndex, endIndex);
28+
int midIndex = FindNextIndex(elements, startIndex, endIndex);
3429

35-
QuickSortStart(elements, startIndex, midIndex - 1);
36-
QuickSortStart(elements, midIndex + 1, endIndex);
30+
StartQuickSort(elements, startIndex, midIndex - 1);
31+
StartQuickSort(elements, midIndex + 1, endIndex);
3732
}
3833
}
3934

40-
private int findNextIndex(int[] elements, int startIndex, int endIndex)
35+
private int FindNextIndex(int[] elements, int startIndex, int endIndex)
4136
{
4237
int pivotValue = elements[endIndex];
4338
int pivotIndex = startIndex;

SortingAlgorithmVisualisation/Algorithms/RadixSort.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ class RadixSort : AlgorithmBase
1414
public override int elementCount { get; set; }
1515

1616
private string[] elementDuplicates;
17+
1718
public override void BeginAlgorithm(int[] elements)
1819
{
19-
elementCount = elements.Length;
20-
21-
DisplaySort.SortComplete = true;
22-
2320
StartRadixSort(elements);
2421
}
2522

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

3936
Thread.Sleep(220);
40-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
4137
}
4238

4339
private void CountSort(int[] elements, int LengthToMinus)

SortingAlgorithmVisualisation/Algorithms/SelectionSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ class SelectionSort : AlgorithmBase
1414

1515
public override void BeginAlgorithm(int[] elements)
1616
{
17-
elementCount = elements.Length;
18-
1917
StartSelectionSort(elements);
20-
21-
DisplaySort.SortComplete = true;
22-
23-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2418
}
2519

2620
private void StartSelectionSort(int[] elements)

SortingAlgorithmVisualisation/Algorithms/ShellSort.cs

-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ class ShellSort : AlgorithmBase
1212
public override int elementCount { get; set; }
1313
public override void BeginAlgorithm(int[] elements)
1414
{
15-
elementCount = elements.Length;
16-
1715
StartShellSort(elements);
18-
19-
DisplaySort.SortComplete = true;
20-
21-
ShowCompletedDisplay(graphics, maxWidth, maxHeight, elements, threadDelay);
2216
}
2317

2418
private void StartShellSort(int[] elements)

0 commit comments

Comments
 (0)