sorting-visualization/SortingVisualization/Algorithms/Bubblesort.cs

25 lines
854 B
C#
Raw Normal View History

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