Skip to content

Commit 3bb3a2c

Browse files
committed
Added Pancake Sort
1 parent da8987f commit 3bb3a2c

File tree

43 files changed

+283
-62
lines changed

Some content is hidden

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

43 files changed

+283
-62
lines changed
2.5 KB
Binary file not shown.

README.md

Lines changed: 4 additions & 2 deletions
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.06-blue">
12-
<img src="https://img.shields.io/badge/version-v1.06-blue">
11+
<a href="https://img.shields.io/badge/version-v1.07-blue">
12+
<img src="https://img.shields.io/badge/version-v1.07-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.07 - Added Pancake Sort
38+
3739
- 1.06 - Added Tree Sort (Pre-Order Traversal)
3840

3941
- 1.05 - Added Cycle Sort

SortingAlgorithmVisualisation/Algorithms/AlgorithmBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public abstract class AlgorithmBase
2222
public abstract int elementCount { get; set; }
2323
public abstract void BeginAlgorithm(int[] elements);
2424

25-
public void ShowCompletedDisplay(int[] elements)
25+
public async void ShowCompletedDisplay(int[] elements)
2626
{
27-
ShowAllGreen(elements);
27+
await Task.Run(() => ShowAllElementsBlue(elements));
2828
}
2929

30-
private void ShowAllGreen(int[] elements)
30+
private void ShowAllElementsBlue(int[] elements)
3131
{
3232
if (threadDelay == 200)
3333
{
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Threading;
8+
using System.Windows.Forms;
9+
using System.Drawing;
10+
11+
namespace SortingAlgorithmVisualisation.Algorithms
12+
{
13+
class PancakeSort : AlgorithmBase
14+
{
15+
public override int elementCount { get; set; }
16+
private int sortedIndex;
17+
18+
public override void BeginAlgorithm(int[] elements)
19+
{
20+
StartPancakeSort(elements);
21+
}
22+
23+
private void StartPancakeSort(int[] elements)
24+
{
25+
for(int i = elementCount; i > 1; i--)
26+
{
27+
int maxIndex = FindMaxIndex(elements, i); //Returns the index of the highest value within a range
28+
sortedIndex = i;
29+
30+
if(maxIndex != i)
31+
{
32+
FlipArray(elements, maxIndex); //Flips to the highest index so that [0] is the highest value
33+
FlipArray(elements, i - 1); //Flips to make the highest value that was [0] to [i - 1]
34+
}
35+
}
36+
}
37+
38+
private int FindMaxIndex(int[] elements, int upperLimit)
39+
{
40+
int maxIndex = 0;
41+
int highestValue = 0;
42+
43+
for(int i = 0; i < upperLimit; i++)
44+
{
45+
if(elements[i] > highestValue)
46+
{
47+
maxIndex = i;
48+
highestValue = elements[i];
49+
}
50+
}
51+
52+
return maxIndex;
53+
}
54+
55+
private void FlipArray(int[] elements, int upperLimit)
56+
{
57+
int upperIndex = upperLimit;
58+
59+
for(int i = 0; i < upperLimit; i++)
60+
{
61+
int temp = elements[upperIndex];
62+
elements[upperIndex] = elements[i];
63+
elements[i] = temp;
64+
upperIndex--;
65+
}
66+
67+
ReDrawDisplay(elements);
68+
}
69+
70+
private void ClearDisplay()
71+
{
72+
for (int i = 0; i < sortedIndex; i++)
73+
{
74+
graphics.FillRectangle(new SolidBrush(SystemColors.ActiveBorder), i * maxWidth, 0, maxWidth, maxHeight);
75+
}
76+
}
77+
78+
private void ReDrawDisplay(int[] elements)
79+
{
80+
ClearDisplay();
81+
82+
if (threadDelay == 200)
83+
{
84+
threadDelay = 80;
85+
}
86+
else if (threadDelay == 0)
87+
{
88+
threadDelay = 1;
89+
}
90+
91+
for (int i = 0; i < sortedIndex; i++)
92+
{
93+
graphics.FillRectangle(new SolidBrush(Color.Black), i * maxWidth, maxHeight - elements[i], maxWidth, elements[i]);
94+
}
95+
96+
Thread.Sleep(threadDelay + 20);
97+
}
98+
}
99+
}

SortingAlgorithmVisualisation/Algorithms/RadixSort.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ private void StartRadixSort(int[] elements)
2626
{
2727
Thread.Sleep(700);
2828

29-
ClearDisplay();
3029
CountSort(elements, i);
3130

3231
//Redraw all elements
@@ -85,6 +84,8 @@ private void ClearDisplay()
8584

8685
private void ReDrawDisplay(int[] elements)
8786
{
87+
ClearDisplay();
88+
8889
if (threadDelay == 200)
8990
{
9091
threadDelay = 80;
@@ -98,6 +99,7 @@ private void ReDrawDisplay(int[] elements)
9899
{
99100
graphics.FillRectangle(new SolidBrush(Color.Black), i * maxWidth, maxHeight - elements[i], maxWidth, elements[i]);
100101
Thread.Sleep(threadDelay);
102+
// MessageBox.Show(elements[i].ToString());
101103
}
102104
}
103105
}

SortingAlgorithmVisualisation/Forms/DisplaySort.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ private void SetUpFormData(string _setModifier)
9797
{
9898
secondDelay.Text += threadDelay + 220 + "ms";
9999
}
100+
else if (algorithmName.Contains("Pancake"))
101+
{
102+
secondDelay.Text += threadDelay + 20 + "ms";
103+
}
100104
else
101105
{
102106
secondDelay.Text += threadDelay + "ms";

SortingAlgorithmVisualisation/Forms/MainMenuForm.Designer.cs

Lines changed: 32 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SortingAlgorithmVisualisation/Forms/MainMenuForm.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ private bool SetAlgorithmData()
140140
algorithm = new TreeSort();
141141
algorithm.SetComplexity(2);
142142
break;
143+
case "Pancake Sort":
144+
algorithm = new PancakeSort();
145+
algorithm.SetComplexity(4);
146+
break;
143147
case null:
144148
MessageBox.Show("Please select an algorithm","Error");
145149
return false;

SortingAlgorithmVisualisation/SortingAlgorithmVisualisation.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<TargetCulture>en</TargetCulture>
2626
<ProductName>Sorting Algorithm Visualiser</ProductName>
2727
<PublisherName>Nathan Jukes</PublisherName>
28-
<ApplicationRevision>7</ApplicationRevision>
28+
<ApplicationRevision>8</ApplicationRevision>
2929
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
3030
<UseApplicationTrust>false</UseApplicationTrust>
3131
<PublishWizardCompleted>true</PublishWizardCompleted>
@@ -87,6 +87,7 @@
8787
<Compile Include="Algorithms\InsertionSort.cs" />
8888
<Compile Include="Algorithms\MergeSort.cs" />
8989
<Compile Include="Algorithms\OddEvenSort.cs" />
90+
<Compile Include="Algorithms\PancakeSort.cs" />
9091
<Compile Include="Algorithms\QuickSort.cs" />
9192
<Compile Include="Algorithms\RadixSort.cs" />
9293
<Compile Include="Algorithms\SelectionSort.cs" />
Binary file not shown.

0 commit comments

Comments
 (0)