Initial commit from 29th of August 2018
This commit is contained in:
24
SortingVisualization/Algorithms/Bubblesort.cs
Normal file
24
SortingVisualization/Algorithms/Bubblesort.cs
Normal file
@ -0,0 +1,24 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
37
SortingVisualization/Algorithms/Cocktailshakersort.cs
Normal file
37
SortingVisualization/Algorithms/Cocktailshakersort.cs
Normal file
@ -0,0 +1,37 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
43
SortingVisualization/Algorithms/Gravitysort.cs
Normal file
43
SortingVisualization/Algorithms/Gravitysort.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
|
||||
namespace SortingVisualization.Algorithms {
|
||||
public class Gravitysort : SortingAlgorithm {
|
||||
public override string GetAlgorithmName() => "Gravitysort";
|
||||
|
||||
public override void Sort(ref DataSet set) {
|
||||
Simulate(ref set);
|
||||
System.Console.WriteLine("Doing {0}...", this.GetAlgorithmName());
|
||||
int i, j, max, sum;
|
||||
byte[] beads;
|
||||
|
||||
for (i = 1, max = set.ReadOperation(0); i < set.Size; ++i)
|
||||
if (set.ReadOperation(i) > max)
|
||||
max = set.ReadOperation(i);
|
||||
|
||||
beads = new byte[max * set.Size];
|
||||
|
||||
for (i = 0; i < set.Size; ++i)
|
||||
for (j = 0; j < set.ReadOperation(i); ++j)
|
||||
beads[i * max + j] = 1;
|
||||
|
||||
for (j = 0; j < max; ++j) {
|
||||
for (sum = i = 0; i < set.Size; ++i) {
|
||||
sum += beads[i * max + j];
|
||||
beads[i * max + j] = 0;
|
||||
}
|
||||
|
||||
for (i = set.Size - sum; i < set.Size; ++i)
|
||||
beads[i * max + j] = 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < set.Size; ++i) {
|
||||
for (j = 0; j < max && Convert.ToBoolean(beads[i * max + j]); ++j)
|
||||
;
|
||||
set.WriteOperation(i, j);
|
||||
}
|
||||
System.Console.WriteLine("{0} complete!", this.GetAlgorithmName());
|
||||
if (!set.SimulateMode)
|
||||
set.FinalizeVideo();
|
||||
}
|
||||
}
|
||||
}
|
20
SortingVisualization/Algorithms/Insertionsort.cs
Normal file
20
SortingVisualization/Algorithms/Insertionsort.cs
Normal file
@ -0,0 +1,20 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
57
SortingVisualization/Algorithms/Mergesort.cs
Normal file
57
SortingVisualization/Algorithms/Mergesort.cs
Normal file
@ -0,0 +1,57 @@
|
||||
namespace SortingVisualization.Algorithms {
|
||||
public class Mergesort : SortingAlgorithm {
|
||||
public override string GetAlgorithmName() => "Mergesort";
|
||||
|
||||
public override void Sort(ref DataSet set) {
|
||||
Simulate(ref set);
|
||||
System.Console.WriteLine("Doing {0}...", this.GetAlgorithmName());
|
||||
MergeSort(ref set, 0, set.Size - 1);
|
||||
System.Console.WriteLine("{0} complete!", this.GetAlgorithmName());
|
||||
if (!set.SimulateMode)
|
||||
set.FinalizeVideo();
|
||||
}
|
||||
public static void MergeSort(ref DataSet set, int l, int r) {
|
||||
if (l < r) {
|
||||
int mid = (l / 2) + (r / 2);
|
||||
MergeSort(ref set, l, mid);
|
||||
MergeSort(ref set, mid + 1, r);
|
||||
Merge(ref set, l, mid, r);
|
||||
}
|
||||
}
|
||||
|
||||
private static void Merge(ref DataSet set, int l, int m, int r) {
|
||||
int left = l;
|
||||
int right = m + 1;
|
||||
int[] tmp = new int[(r - l) + 1];
|
||||
int tmpIndex = 0;
|
||||
|
||||
while ((left <= m) && (right <= r)) {
|
||||
if (set.LessThan(left, right)) {
|
||||
tmp[tmpIndex] = set.ReadOperation(left);
|
||||
left++;
|
||||
} else {
|
||||
tmp[tmpIndex] = set.ReadOperation(right);
|
||||
right++;
|
||||
}
|
||||
tmpIndex++;
|
||||
}
|
||||
|
||||
while (left <= m) {
|
||||
tmp[tmpIndex] = set.ReadOperation(left);
|
||||
left++;
|
||||
tmpIndex++;
|
||||
}
|
||||
|
||||
while (right <= r) {
|
||||
tmp[tmpIndex] = set.ReadOperation(right);
|
||||
right++;
|
||||
tmpIndex++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < tmp.Length; i++) {
|
||||
set.WriteOperation(l + i, tmp[i]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
37
SortingVisualization/Algorithms/Quicksort.cs
Normal file
37
SortingVisualization/Algorithms/Quicksort.cs
Normal file
@ -0,0 +1,37 @@
|
||||
namespace SortingVisualization.Algorithms {
|
||||
public class Quicksort : SortingAlgorithm {
|
||||
public override string GetAlgorithmName() => "Quicksort";
|
||||
public override void Sort(ref DataSet set) {
|
||||
Simulate(ref set);
|
||||
System.Console.WriteLine("Doing {0}...", this.GetAlgorithmName());
|
||||
SortRecursion(ref set, 0, set.Size - 1);
|
||||
System.Console.WriteLine("{0} complete!", this.GetAlgorithmName());
|
||||
if (!set.SimulateMode)
|
||||
set.FinalizeVideo();
|
||||
}
|
||||
public static void SortRecursion(ref DataSet set, int l, int r) {
|
||||
int i = l, j = r;
|
||||
int pivot = (l + r) / 2;
|
||||
while (i <= j) {
|
||||
while (set.LessThan(i, pivot))
|
||||
i++;
|
||||
while (set.GreaterThan(j, pivot))
|
||||
j--;
|
||||
|
||||
if (i <= j) {
|
||||
set.Swap(i, j);
|
||||
if (pivot == i)
|
||||
pivot = j;
|
||||
else if (pivot == j)
|
||||
pivot = i;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
if (l < j)
|
||||
SortRecursion(ref set, l, j);
|
||||
if (i < r)
|
||||
SortRecursion(ref set, i, r);
|
||||
}
|
||||
}
|
||||
}
|
37
SortingVisualization/Algorithms/Radixsort.cs
Normal file
37
SortingVisualization/Algorithms/Radixsort.cs
Normal file
@ -0,0 +1,37 @@
|
||||
namespace SortingVisualization.Algorithms {
|
||||
public class Radixsort : SortingAlgorithm {
|
||||
public override string GetAlgorithmName() => "Radixsort";
|
||||
|
||||
public override void Sort(ref DataSet set) {
|
||||
Simulate(ref set);
|
||||
System.Console.WriteLine("Doing {0}...", this.GetAlgorithmName());
|
||||
int n; // Fachnummer
|
||||
int[] nPart = new int[2]; // Anzahl der Elemente in den beiden Faechern
|
||||
int[,] part = new int[2, set.Size]; // die beiden Faecher haben die Groesse des Arrays a
|
||||
|
||||
// Schleife ueber alle Bits der Schluessel (bei int: 32 Bit)
|
||||
for (int i = 0; i < 32; i++) {
|
||||
nPart[0] = 0;
|
||||
nPart[1] = 0;
|
||||
|
||||
// Partitionierungsphase: teilt "a" auf die Faecher auf
|
||||
for (int j = 0; j < set.Size; j++) {
|
||||
int tmp = set.ReadOperation(j);
|
||||
n = (tmp >> i) & 1; // ermittelt die Fachnummer: 0 oder 1
|
||||
part[n, nPart[n]++] = tmp; // kopiert j-tes Element ins richtige Fach
|
||||
}
|
||||
|
||||
// Sammelphase: kopiert die beiden Faecher wieder nach "a" zusammen
|
||||
for (int k = 0; k < nPart[0]; k++) {
|
||||
set.WriteOperation(k, part[0, k]);
|
||||
}
|
||||
for (int k = nPart[0]; k < set.Size; k++) {
|
||||
set.WriteOperation(k, part[1, k]);
|
||||
}
|
||||
}
|
||||
System.Console.WriteLine("{0} complete!", this.GetAlgorithmName());
|
||||
if (!set.SimulateMode)
|
||||
set.FinalizeVideo();
|
||||
}
|
||||
}
|
||||
}
|
20
SortingVisualization/Algorithms/Selectionsort.cs
Normal file
20
SortingVisualization/Algorithms/Selectionsort.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace SortingVisualization.Algorithms {
|
||||
public class Selectionsort : SortingAlgorithm {
|
||||
public override string GetAlgorithmName() => "Selectionsort";
|
||||
|
||||
public override void Sort(ref DataSet set) {
|
||||
Simulate(ref set);
|
||||
System.Console.WriteLine("Doing {0}...", this.GetAlgorithmName());
|
||||
for (int i = 0; i < set.Size; i++) {
|
||||
int min = i;
|
||||
for (int j = i + 1; j < set.Size; j++)
|
||||
if (set.LessThan(j, min))
|
||||
min = j;
|
||||
set.Swap(min, i);
|
||||
}
|
||||
System.Console.WriteLine("{0} complete!", this.GetAlgorithmName());
|
||||
if (!set.SimulateMode)
|
||||
set.FinalizeVideo();
|
||||
}
|
||||
}
|
||||
}
|
6
SortingVisualization/App.config
Normal file
6
SortingVisualization/App.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
|
||||
</startup>
|
||||
</configuration>
|
420
SortingVisualization/DataSet.cs
Normal file
420
SortingVisualization/DataSet.cs
Normal file
@ -0,0 +1,420 @@
|
||||
using Accord.Video.FFMPEG;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SortingVisualization {
|
||||
public enum SetType {
|
||||
Ordererd,
|
||||
Reversed,
|
||||
Random,
|
||||
SingleError
|
||||
}
|
||||
public class DataSet {
|
||||
#region Props
|
||||
/// <summary>
|
||||
/// Gibt die Größe (Anzahl der zu sortierenden Werte) an.
|
||||
/// </summary>
|
||||
public int Size { get; private set; }
|
||||
/// <summary>
|
||||
/// Speichert das initale Datenset um verschiedene Algorithmen auf dasselbe Set anzuwenden (direkter Vergleich).
|
||||
/// </summary>
|
||||
private readonly int[] _InitialData;
|
||||
public int Seed { get; private set; }
|
||||
private int[] _Data;
|
||||
public int ReadOperations { get; private set; } = 0;
|
||||
public int WriteOperations { get; private set; } = 0;
|
||||
public int CompareOperations { get; private set; } = 0;
|
||||
public int SwapOperations { get; private set; } = 0;
|
||||
public long Ticks { get; private set; } = 0;
|
||||
public string SortingAlgorithm {
|
||||
get => _sortingAlgorithm;
|
||||
set {
|
||||
_sortingAlgorithm = value;
|
||||
if (!Directory.Exists(this.SortingAlgorithm)) {
|
||||
Directory.CreateDirectory(this.SortingAlgorithm);
|
||||
}
|
||||
_Directory = Path.Combine(this.SortingAlgorithm, DataSet.SetTypeToString(this.SetType));
|
||||
if (!Directory.Exists(_Directory)) {
|
||||
Directory.CreateDirectory(_Directory);
|
||||
}
|
||||
_Directory = Path.Combine(_Directory, this.Size.ToString());
|
||||
if (!Directory.Exists(_Directory)) {
|
||||
Directory.CreateDirectory(_Directory);
|
||||
}
|
||||
if (this.SetType == SetType.Random || this.SetType == SetType.SingleError) {
|
||||
_Directory = Path.Combine(_Directory, this.Seed.ToString());
|
||||
if (!Directory.Exists(_Directory)) {
|
||||
Directory.CreateDirectory(_Directory);
|
||||
}
|
||||
}
|
||||
if (!VideoRenderer.IsOpen) {
|
||||
VideoRenderer.Open(Path.Combine(_Directory, this.SortingAlgorithm + ".mkv"), 3000, 2000, new Accord.Math.Rational(60), VideoCodec.Default, int.MaxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
private string _Directory = "uninitialized";
|
||||
private long _StartTime = 0;
|
||||
public SetType SetType { get; }
|
||||
private int _ExpectedFrameCount = 1;
|
||||
private bool _simulateMode;
|
||||
private int _frameSkipping = 0;
|
||||
private int _skippedFrames = 0;
|
||||
public bool SimulateMode {
|
||||
get => _simulateMode;
|
||||
set {
|
||||
if (value) {
|
||||
_ExpectedFrameCount = int.MaxValue;
|
||||
Console.WriteLine("Simulating {0} with {1} values (Set: {2} - Seed {3})...", this.SortingAlgorithm, this.Size, DataSet.SetTypeToString(this.SetType), this.Seed);
|
||||
} else {
|
||||
Console.WriteLine("Simulation ended! Results:\n\t{0} Reads\n\t{1} Writes\n\t{2} Comparisons\n\t{3} Swaps\n\t{4} Frames", this.ReadOperations, this.WriteOperations, this.CompareOperations, this.SwapOperations, this.FrameCount + this.Size);
|
||||
_ExpectedFrameCount = this.FrameCount + this.Size;
|
||||
this.ResetToInitial();
|
||||
_frameSkipping = _ExpectedFrameCount / 3600;
|
||||
Console.WriteLine("Drawing 1 in every {0} frames", _frameSkipping + 1);
|
||||
Console.SetOut(TextWriter.Null);
|
||||
}
|
||||
_simulateMode = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region Constructors
|
||||
public DataSet(int size, SetType type, int seed) {
|
||||
this.SetType = type;
|
||||
this.Seed = seed;
|
||||
this.Size = size;
|
||||
_InitialData = new int[size];
|
||||
var rnd = new Random(seed);
|
||||
switch (type) {
|
||||
case SetType.Ordererd:
|
||||
for (int i = 0; i < size; i++)
|
||||
_InitialData[i] = i + 1;
|
||||
break;
|
||||
case SetType.Reversed:
|
||||
for (int i = 0; i < size; i++)
|
||||
_InitialData[i] = size - i;
|
||||
break;
|
||||
case SetType.Random:
|
||||
for (int i = 0; i < size; i++) {
|
||||
int tmp;
|
||||
do {
|
||||
tmp = rnd.Next(1, size + 1); // TODO: Do this better!
|
||||
} while (_InitialData.Contains(tmp));
|
||||
_InitialData[i] = tmp;
|
||||
}
|
||||
break;
|
||||
case SetType.SingleError:
|
||||
for (int i = 1; i < size - 1; i++)
|
||||
_InitialData[i] = i + 1;
|
||||
_InitialData[0] = size;
|
||||
_InitialData[size - 1] = 1;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Unhandled Enumeration type when initializing the dataset values!", "type");
|
||||
}
|
||||
_Data = new int[size];
|
||||
_InitialData.CopyTo(_Data, 0);
|
||||
_yScale = (double)_FrameHeight / (double)(this.Size);
|
||||
_xScale = (double)_FrameWidth / (double)(this.Size);
|
||||
_BarWidth = (int)Math.Ceiling(_xScale);
|
||||
}
|
||||
public DataSet(int size, SetType type) : this(size, type, Guid.NewGuid().GetHashCode()) { }
|
||||
public override string ToString() => $"{this.SortingAlgorithm} - {this.Size} Values - {DataSet.SetTypeToString(this.SetType)} Set - Seed {this.Seed}";
|
||||
#endregion
|
||||
#region Operations
|
||||
public static string SetTypeToString(SetType type) {
|
||||
switch (type) {
|
||||
case SetType.Ordererd:
|
||||
return "Ordered";
|
||||
case SetType.Reversed:
|
||||
return "Reversed";
|
||||
case SetType.Random:
|
||||
return "Random";
|
||||
case SetType.SingleError:
|
||||
return "SingleError";
|
||||
default:
|
||||
return "Unkown";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gibt das Element des Datensets aus, das an (index + 1)-ter Stelle steht.
|
||||
/// </summary>
|
||||
/// <param name="index">Zugriffsindex, wirft einen Fehler, wenn der Index nicht gültig ist.</param>
|
||||
/// <returns>Kopie des Werts aus dem Datenset.</returns>
|
||||
private int this[int index] {
|
||||
get {
|
||||
this.ReadOperations++;
|
||||
return _Data[index];
|
||||
}
|
||||
set {
|
||||
this.WriteOperations++;
|
||||
_Data[index] = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Reads the value at an index (same as this[index]) but generates a frame.
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public int ReadOperation(int index) {
|
||||
this.DrawReadFrame(index);
|
||||
return this[index];
|
||||
}
|
||||
public void WriteOperation(int index, int value) {
|
||||
this[index] = value;
|
||||
this.DrawWriteFrame(index);
|
||||
}
|
||||
private void Comparation(int left, int right) {
|
||||
this.CompareOperations++;
|
||||
this.DrawCompareFrame(left, right);
|
||||
}
|
||||
public bool GreaterThan(int left, int right) {
|
||||
this.Comparation(left, right);
|
||||
return (this[left] > this[right]);
|
||||
}
|
||||
public bool GreaterThanOrEqual(int left, int right) {
|
||||
this.Comparation(left, right);
|
||||
return (this[left] >= this[right]);
|
||||
}
|
||||
public bool LessThan(int left, int right) {
|
||||
this.Comparation(left, right);
|
||||
return (this[left] < this[right]);
|
||||
}
|
||||
public bool LessThanOrEqual(int left, int right) {
|
||||
this.Comparation(left, right);
|
||||
return (this[left] <= this[right]);
|
||||
}
|
||||
public bool Equal(int left, int right) {
|
||||
this.Comparation(left, right);
|
||||
return (this[left] == this[right]);
|
||||
}
|
||||
public bool Unequal(int left, int right) {
|
||||
this.Comparation(left, right);
|
||||
return (this[left] != this[right]);
|
||||
}
|
||||
public void Swap(int left, int right) {
|
||||
int tmp = this[left];
|
||||
this[left] = this[right];
|
||||
this[right] = tmp;
|
||||
this.SwapOperations++;
|
||||
this.DrawSwapFrame(left, right);
|
||||
}
|
||||
#endregion
|
||||
#region Timing
|
||||
public void StartTimer() => _StartTime = DateTime.Now.Ticks;
|
||||
public void StopTimer() => this.Ticks = DateTime.Now.Ticks - _StartTime;
|
||||
#endregion
|
||||
#region Visualizing
|
||||
public Brush BackgroundBrush = new SolidBrush(Color.Black);
|
||||
public Brush ReadColor = new SolidBrush(Color.Blue);
|
||||
public Brush WriteColor = new SolidBrush(Color.Magenta);
|
||||
public Brush CheckColor = new SolidBrush(Color.Green);
|
||||
public Brush BarColor = new SolidBrush(Color.White);
|
||||
public Brush CompareColor = new SolidBrush(Color.Red);
|
||||
public Brush SwapColor = new SolidBrush(Color.Yellow);
|
||||
private Bitmap _Frame = new Bitmap(_FrameWidth, _FrameHeight);
|
||||
private VideoFileWriter VideoRenderer = new VideoFileWriter();
|
||||
int _progress = 0;
|
||||
private int FrameCount {
|
||||
get => _FrameCount;
|
||||
set {
|
||||
if (!this.SimulateMode)
|
||||
VideoRenderer.WriteVideoFrame(_Frame);
|
||||
// _Frame.Save(Path.Combine(this.SortingAlgorithm, DataSet.SetTypeToString(this.SetType), $"Image{ _FrameCount.ToString("d6")}.png"), ImageFormat.Png);
|
||||
_FrameCount = value;
|
||||
int newprogress = (_FrameCount * 100) / _ExpectedFrameCount;
|
||||
if (newprogress > _progress) {
|
||||
_progress = newprogress;
|
||||
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {
|
||||
AutoFlush = true
|
||||
});
|
||||
Console.Write("\rProgress: {0}% - {1} frames", newprogress, _FrameCount);
|
||||
Console.SetOut(TextWriter.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
private static readonly int _FrameHeight = 2000;
|
||||
private static readonly int _FrameWidth = 3000;
|
||||
private readonly double _yScale = 1;
|
||||
private readonly double _xScale = 1;
|
||||
private readonly int _BarWidth = 1;
|
||||
private int _FrameCount = 0;
|
||||
private string _sortingAlgorithm = "DefaultAlgorithm";
|
||||
|
||||
private void DrawFrame() {
|
||||
this.ClearFrame();
|
||||
}
|
||||
|
||||
private void ClearFrame() {
|
||||
DrawRect(0, 0, _FrameWidth, _FrameHeight, ref this.BackgroundBrush);
|
||||
}
|
||||
|
||||
private void DrawRect(int x1, int y1, int w, int h, ref Brush brush) {
|
||||
int wn = w - 1;
|
||||
if (wn <= 0)
|
||||
wn = 1;
|
||||
int hn = h - 1;
|
||||
if (hn <= 0)
|
||||
hn = 1;
|
||||
using (var graphics = Graphics.FromImage(_Frame)) {
|
||||
graphics.FillRectangle(brush, x1, y1, wn, hn);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCompareFrame(int left, int right) {
|
||||
if (this.SimulateMode) {
|
||||
this.FrameCount++;
|
||||
} else {
|
||||
if (this.SkipFrame())
|
||||
return;
|
||||
DrawFrame();
|
||||
for (int i = 0; i < this.Size; i++) {
|
||||
int xpos = (int)(_xScale * i);
|
||||
int ypos = (int)(_yScale * this._Data[i]);
|
||||
if (i == left || i == right)
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.CompareColor);
|
||||
else
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.BarColor);
|
||||
}
|
||||
++this.FrameCount;
|
||||
//Console.WriteLine("Compare frame {0} was drawn",this.FrameCount);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawReadFrame(int index) {
|
||||
DataSet dataSet = this;
|
||||
if (dataSet.SimulateMode) {
|
||||
this.FrameCount++;
|
||||
} else {
|
||||
if (this.SkipFrame())
|
||||
return;
|
||||
DrawFrame();
|
||||
for (int i = 0; i < this.Size; i++) {
|
||||
int xpos = (int)(_xScale * i);
|
||||
int ypos = (int)(_yScale * this._Data[i]);
|
||||
if (i == index)
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.ReadColor);
|
||||
else
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.BarColor);
|
||||
}
|
||||
++this.FrameCount;
|
||||
//Console.WriteLine("Read frame {0} was drawn", this.FrameCount);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawWriteFrame(int index) {
|
||||
if (this.SimulateMode) {
|
||||
this.FrameCount++;
|
||||
} else {
|
||||
if (this.SkipFrame())
|
||||
return;
|
||||
DrawFrame();
|
||||
for (int i = 0; i < this.Size; i++) {
|
||||
int xpos = (int)(_xScale * i);
|
||||
int ypos = (int)(_yScale * this._Data[i]);
|
||||
if (i == index)
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.WriteColor);
|
||||
else
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.BarColor);
|
||||
}
|
||||
++this.FrameCount;
|
||||
//Console.WriteLine("Write frame {0} was drawn", this.FrameCount);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawSwapFrame(int left, int right) {
|
||||
if (this.SimulateMode) {
|
||||
this.FrameCount++;
|
||||
} else {
|
||||
if (this.SkipFrame())
|
||||
return;
|
||||
DrawFrame();
|
||||
for (int i = 0; i < this.Size; i++) {
|
||||
int xpos = (int)(_xScale * i);
|
||||
int ypos = (int)(_yScale * this._Data[i]);
|
||||
if (i == left || i == right)
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.SwapColor);
|
||||
else
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.BarColor);
|
||||
}
|
||||
++this.FrameCount;
|
||||
//Console.WriteLine("Swap frame {0} was drawn", this.FrameCount);
|
||||
}
|
||||
}
|
||||
|
||||
private bool SkipFrame() {
|
||||
if (_skippedFrames++ < _frameSkipping) {
|
||||
++this._FrameCount; // Avoid automatic drawing trough property
|
||||
return true;
|
||||
} else {
|
||||
_skippedFrames = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void FinalizeVideo() {
|
||||
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {
|
||||
AutoFlush = true
|
||||
});
|
||||
Console.WriteLine("\n\nFinalizing...");
|
||||
for (int i = 0; i < this.Size; i++) {
|
||||
int xpos = (int)(_xScale * i);
|
||||
int ypos = (int)(_yScale * this._Data[i]);
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.BarColor);
|
||||
}
|
||||
//if (!this.SkipFrame())
|
||||
++this.FrameCount;
|
||||
//Console.WriteLine("Finalizing Frame {0} was drawn", this.FrameCount);
|
||||
for (int d = 0; d < this.Size; d++) { // Add 1 checked bar per frame
|
||||
int xpos = (int)(_xScale * d);
|
||||
int ypos = (int)(_yScale * this._Data[d]);
|
||||
DrawRect(xpos, _FrameHeight - ypos, _BarWidth, ypos, ref this.CheckColor);
|
||||
//if (this.SkipFrame())
|
||||
// continue;
|
||||
++this.FrameCount;
|
||||
//Console.WriteLine("Finalizing Frame {0} was drawn", this.FrameCount);
|
||||
}
|
||||
Console.WriteLine("Encoding video from frames...");
|
||||
//var startInfo = new System.Diagnostics.ProcessStartInfo {
|
||||
// WorkingDirectory = _Directory,
|
||||
// WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
|
||||
// FileName = "ffmpeg.exe",
|
||||
// Arguments = $"-r 60 -f image2 -s {_FrameWidth}x{_FrameHeight} -i Image%06d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p {this.SortingAlgorithm}.mp4",
|
||||
// RedirectStandardInput = true,
|
||||
// UseShellExecute = false,
|
||||
// RedirectStandardOutput = true,
|
||||
// CreateNoWindow = false
|
||||
//};
|
||||
//Process.Start(startInfo);
|
||||
VideoRenderer.Close();
|
||||
VideoRenderer.Dispose();
|
||||
VideoRenderer = null;
|
||||
VideoRenderer = new VideoFileWriter();
|
||||
Console.WriteLine("Video encoded!");
|
||||
}
|
||||
#endregion
|
||||
#region Resetting
|
||||
public void ResetToInitial() {
|
||||
_Data = null;
|
||||
_Data = new int[this.Size];
|
||||
_InitialData.CopyTo(_Data, 0);
|
||||
_Frame?.Dispose();
|
||||
_Frame = null;
|
||||
_Frame = new Bitmap(_FrameWidth, _FrameHeight);
|
||||
this.ReadOperations = 0;
|
||||
this.WriteOperations = 0;
|
||||
this.CompareOperations = 0;
|
||||
this.Ticks = 0;
|
||||
this.FrameCount = 0;
|
||||
this._progress = 0;
|
||||
this._skippedFrames = 0;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
96
SortingVisualization/Program.cs
Normal file
96
SortingVisualization/Program.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using SortingVisualization.Algorithms;
|
||||
using System;
|
||||
|
||||
namespace SortingVisualization {
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
SortingAlgorithm sort;
|
||||
Console.WriteLine("Select an algorithm:\n\t1. Bubblesort\n\t2. Insertion Sort\n\t3. Selection Sort\n\t4. Quicksort\n\t5. Radixsort\n\t6. Shell Sort\n\t7. Mergesort\n\t8. Heapsort\n\t9. Cocktailshakersort\n\t10. Gravitysort");
|
||||
string input;
|
||||
|
||||
int SetSize = 20;
|
||||
int selection;
|
||||
do { input = Console.ReadLine(); }
|
||||
while (!int.TryParse(input, out selection));
|
||||
switch (selection) {
|
||||
case 1:
|
||||
sort = new Bubblesort();
|
||||
break;
|
||||
case 2:
|
||||
sort = new Insertionsort();
|
||||
break;
|
||||
case 3:
|
||||
sort = new Selectionsort();
|
||||
break;
|
||||
case 4:
|
||||
sort = new Quicksort();
|
||||
break;
|
||||
case 5:
|
||||
sort = new Radixsort();
|
||||
break;
|
||||
case 7:
|
||||
sort = new Mergesort();
|
||||
break;
|
||||
case 9:
|
||||
sort = new Cocktailshakersort();
|
||||
break;
|
||||
case 10:
|
||||
sort = new Gravitysort();
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Not Implemented or invalid selection!");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine("Select the set Size:");
|
||||
do {
|
||||
input = Console.ReadLine();
|
||||
} while (!int.TryParse(input, out SetSize));
|
||||
Console.WriteLine("Select a set:\n\t1. Random\n\t2. Ordered\n\t3. Reversed\n\t4. Single Error");
|
||||
do { input = Console.ReadLine(); }
|
||||
while (!int.TryParse(input, out selection));
|
||||
SetType type;
|
||||
switch (selection) {
|
||||
case 1:
|
||||
type = SetType.Random;
|
||||
break;
|
||||
case 2:
|
||||
type = SetType.Ordererd;
|
||||
break;
|
||||
case 3:
|
||||
type = SetType.Reversed;
|
||||
break;
|
||||
case 4:
|
||||
type = SetType.SingleError;
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Not Implemented or invalid selection!");
|
||||
return;
|
||||
}
|
||||
DataSet set;
|
||||
if (type == SetType.Random || type == SetType.SingleError) {
|
||||
Console.WriteLine("Do you want to enter a seed? (y/n)");
|
||||
ConsoleKeyInfo key;
|
||||
do {
|
||||
int seed;
|
||||
key = Console.ReadKey(true);
|
||||
if (key.Key == ConsoleKey.Y) {
|
||||
Console.WriteLine("Enter a seed: ");
|
||||
do {
|
||||
input = Console.ReadLine();
|
||||
} while (!int.TryParse(input, out seed));
|
||||
set = new DataSet(SetSize, type, seed);
|
||||
break;
|
||||
}
|
||||
if (key.Key == ConsoleKey.N) {
|
||||
Console.WriteLine("Generating with random seed...");
|
||||
set = new DataSet(SetSize, type);
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
} else {
|
||||
set = new DataSet(SetSize, type);
|
||||
}
|
||||
sort.Sort(ref set);
|
||||
}
|
||||
}
|
||||
}
|
36
SortingVisualization/Properties/AssemblyInfo.cs
Normal file
36
SortingVisualization/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||
// die einer Assembly zugeordnet sind.
|
||||
[assembly: AssemblyTitle("SortingVisualization")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("SortingVisualization")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
||||
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||
[assembly: Guid("3ced6adb-adb8-499e-8a0e-6db4b064f514")]
|
||||
|
||||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||
//
|
||||
// Hauptversion
|
||||
// Nebenversion
|
||||
// Buildnummer
|
||||
// Revision
|
||||
//
|
||||
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
||||
// übernehmen, indem Sie "*" eingeben:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
14
SortingVisualization/SortingAlgorithm.cs
Normal file
14
SortingVisualization/SortingAlgorithm.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace SortingVisualization {
|
||||
public abstract class SortingAlgorithm {
|
||||
public abstract void Sort(ref DataSet set);
|
||||
public abstract string GetAlgorithmName();
|
||||
public void Simulate(ref DataSet set) {
|
||||
if (!set.SimulateMode) {
|
||||
set.SimulateMode = true;
|
||||
Sort(ref set);
|
||||
set.SimulateMode = false;
|
||||
set.SortingAlgorithm = this.GetAlgorithmName();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
84
SortingVisualization/SortingVisualization.csproj
Normal file
84
SortingVisualization/SortingVisualization.csproj
Normal file
@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{3CED6ADB-ADB8-499E-8A0E-6DB4B064F514}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>SortingVisualization</RootNamespace>
|
||||
<AssemblyName>SortingVisualization</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Accord, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Accord.3.8.0\lib\net46\Accord.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Accord.Video, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Accord.Video.3.8.0\lib\net46\Accord.Video.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Accord.Video.FFMPEG, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=x86">
|
||||
<HintPath>..\packages\Accord.Video.FFMPEG.3.8.0\lib\net46\Accord.Video.FFMPEG.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Algorithms\Gravitysort.cs" />
|
||||
<Compile Include="Algorithms\Cocktailshakersort.cs" />
|
||||
<Compile Include="Algorithms\Radixsort.cs" />
|
||||
<Compile Include="Algorithms\Selectionsort.cs" />
|
||||
<Compile Include="Algorithms\Insertionsort.cs" />
|
||||
<Compile Include="Algorithms\Mergesort.cs" />
|
||||
<Compile Include="Algorithms\Quicksort.cs" />
|
||||
<Compile Include="Algorithms\Bubblesort.cs" />
|
||||
<Compile Include="DataSet.cs" />
|
||||
<Compile Include="SortingAlgorithm.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="..\packages\Accord.3.8.0\build\Accord.targets" Condition="Exists('..\packages\Accord.3.8.0\build\Accord.targets')" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}".</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\Accord.3.8.0\build\Accord.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.3.8.0\build\Accord.targets'))" />
|
||||
<Error Condition="!Exists('..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets'))" />
|
||||
</Target>
|
||||
<Import Project="..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets" Condition="Exists('..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets')" />
|
||||
</Project>
|
6
SortingVisualization/packages.config
Normal file
6
SortingVisualization/packages.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Accord" version="3.8.0" targetFramework="net461" />
|
||||
<package id="Accord.Video" version="3.8.0" targetFramework="net461" />
|
||||
<package id="Accord.Video.FFMPEG" version="3.8.0" targetFramework="net461" />
|
||||
</packages>
|
Reference in New Issue
Block a user