sorting-visualization/SortingVisualization/Algorithms/Insertionsort.cs

21 lines
757 B
C#

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