sorting and PLF example 2 with modern form.

a lot of C# to be real!
This commit is contained in:
2025-10-23 11:46:36 +02:00
parent cb1b91ba0f
commit 85670b8a9e
16 changed files with 842 additions and 11 deletions

View File

@@ -0,0 +1,50 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SortVisualizer;
public class BogoSort : ISortAlgorithm
{
public string Name => "Bogo Sort";
private static readonly Random rand = new();
public static BogoSort Instance { get; } = new BogoSort();
public BogoSort() { }
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
try
{
while (!IsSorted(array))
{
token.ThrowIfCancellationRequested();
// Shuffle array
for (int i = 0; i < array.Length; i++)
{
int j = rand.Next(array.Length);
(array[i], array[j]) = (array[j], array[i]);
playSound?.Invoke(array[i]);
refresh?.Invoke();
// small delay for live updates
await Task.Delay(delay, token);
}
}
}
catch (TaskCanceledException)
{
// Sorting was stopped; exit gracefully
}
}
private bool IsSorted(int[] array)
{
for (int i = 1; i < array.Length; i++)
if (array[i - 1] > array[i])
return false;
return true;
}
}

View File

@@ -0,0 +1,29 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SortVisualizer;
public class BubbleSort : ISortAlgorithm
{
public string Name => "Bubble Sort";
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - i - 1; j++)
{
if (token.IsCancellationRequested) return;
playSound(array[j]);
if (array[j] > array[j + 1])
{
(array[j], array[j + 1]) = (array[j + 1], array[j]);
refresh();
await Task.Delay(delay);
}
}
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SortVisualizer;
public interface ISortAlgorithm
{
string Name { get; }
Task Sort(
int[] array,
Action refresh,
int delay,
Action<int> playSound,
CancellationToken token
);
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SortVisualizer;
public class InsertionSort : ISortAlgorithm
{
public string Name => "Insertion Sort";
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
for (int i = 1; i < array.Length; i++)
{
if (token.IsCancellationRequested) return;
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key)
{
if (token.IsCancellationRequested) return;
playSound(array[j]);
array[j + 1] = array[j];
j--;
refresh();
await Task.Delay(delay);
}
array[j + 1] = key;
}
}
}

View File

@@ -0,0 +1,83 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SortVisualizer;
public class MergeSort : ISortAlgorithm
{
public string Name => "Merge Sort";
public MergeSort() { }
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
await MergeSortRecursive(array, 0, array.Length - 1, refresh, delay, playSound, token);
}
private async Task MergeSortRecursive(int[] array, int left, int right, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
token.ThrowIfCancellationRequested();
if (left >= right) return;
int mid = (left + right) / 2;
await MergeSortRecursive(array, left, mid, refresh, delay, playSound, token);
await MergeSortRecursive(array, mid + 1, right, refresh, delay, playSound, token);
await Merge(array, left, mid, right, refresh, delay, playSound, token);
}
private async Task Merge(int[] array, int left, int mid, int right, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
int n1 = mid - left + 1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2];
Array.Copy(array, left, L, 0, n1);
Array.Copy(array, mid + 1, R, 0, n2);
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
token.ThrowIfCancellationRequested();
if (L[i] <= R[j])
{
array[k] = L[i];
playSound?.Invoke(L[i]);
i++;
}
else
{
array[k] = R[j];
playSound?.Invoke(R[j]);
j++;
}
refresh?.Invoke();
await Task.Delay(delay, token);
k++;
}
while (i < n1)
{
token.ThrowIfCancellationRequested();
array[k] = L[i];
playSound?.Invoke(L[i]);
i++; k++;
refresh?.Invoke();
await Task.Delay(delay, token);
}
while (j < n2)
{
token.ThrowIfCancellationRequested();
array[k] = R[j];
playSound?.Invoke(R[j]);
j++; k++;
refresh?.Invoke();
await Task.Delay(delay, token);
}
}
}

View File

@@ -0,0 +1,58 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SortVisualizer;
public class StoogeSort : ISortAlgorithm
{
public string Name => "Stooge Sort";
public StoogeSort() { }
public async Task Sort(int[] array, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
try
{
await StoogeSortRecursive(array, 0, array.Length - 1, refresh, delay, playSound, token);
}
catch (TaskCanceledException)
{
// Sorting was cancelled <20> handle gracefully
// Optionally, reset UI or log here
}
catch (Exception ex)
{
// Unexpected exception <20> optional logging
Console.WriteLine($"StoogeSort error: {ex}");
}
}
private async Task StoogeSortRecursive(int[] array, int i, int j, Action refresh, int delay, Action<int> playSound, CancellationToken token)
{
if (token.IsCancellationRequested) return; // graceful early exit
// Swap if first element is greater than last
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
playSound?.Invoke(array[i]);
playSound?.Invoke(array[j]);
refresh?.Invoke();
try { await Task.Delay(delay, token); }
catch (TaskCanceledException) { return; } // exit silently if cancelled
}
if (j - i + 1 > 2)
{
int t = (j - i + 1) / 3;
await StoogeSortRecursive(array, i, j - t, refresh, delay, playSound, token);
await StoogeSortRecursive(array, i + t, j, refresh, delay, playSound, token);
await StoogeSortRecursive(array, i, j - t, refresh, delay, playSound, token);
}
}
}