sorting-visualization/SortingVisualization/Algorithms/Cocktailshakersort.cs

38 lines
1.3 KiB
C#

namespace SortingVisualization.Algorithms {
public class Cocktailshakersort : SortingAlgorithm {
public override string GetAlgorithmName() => "Cocktailshakersort";
public override void Sort(ref DataSet set) {
Simulate(ref set);
System.Console.WriteLine("Doing {0}...", this.GetAlgorithmName());
int beginn = -1;
int ende = set.Size - 2;
bool vertauscht;
do {
vertauscht = false;
beginn++;
for (int i = beginn; i < ende; i++) {
if (set.GreaterThan(i, i + 1)) {
set.Swap(i, i + 1);
vertauscht = true;
}
}
if (!vertauscht) {
break;
}
vertauscht = false;
ende--;
for (int i = ende; i >= beginn - 1; i--) {
if (set.GreaterThan(i, i + 1)) {
set.Swap(i, i + 1);
vertauscht = true;
}
}
} while (vertauscht);
System.Console.WriteLine("{0} complete!", this.GetAlgorithmName());
if (!set.SimulateMode)
set.FinalizeVideo();
}
}
}