This project allows you to implement custom sorting algorithms in C# using a simple interface. Each algorithm is automatically detected and run by the program.
The program searches the algorithms folder for any class that implements the IAlgorithm interface. Each algorithm is run on a randomized integer array, and the program will measure the time taken to sort it.
All sorting algorithms must implement the IAlgorithm interface:
public interface IAlgorithm
{
public string Name => System.Text.RegularExpressions.Regex.Replace(GetType().Name, "(\\B[A-Z])", " $1");
public int[] Sort(int[] nums);
}Details:
Name: Automatically generates a readable name from the class name.
Example:DotnetSort→ "Dotnet Sort".Sort(int[] nums): The method where your sorting logic goes.- Input:
nums— an integer array to sort. - Output: The sorted integer array.
- Input:
- Open your project in your IDE.
- Navigate to the
algorithmsfolder. - Create a new C# file for your algorithm, for example:
MySortingAlgorithm.cs. - Implement the
IAlgorithminterface:
using System;
public class MySortingAlgorithm : IAlgorithm
{
public int[] Sort(int[] nums)
{
int[] array = nums;
//Logic goes here
return array;
}
}Notes:
- Always clone the input array if your algorithm modifies it in-place.
- Make sure the
Sortmethod returns the sorted array. - The
Program.csfile will run the algorithm until the array is sorted
- Save your new
.csfile inside thealgorithmsfolder. - The main program will automatically detect any class implementing
IAlgorithminside this folder.
Suppose you created DotnetSort.cs:
public class DotnetSort : IAlgorithm
{
public int[] Sort(int[] nums)
{
int[] numbers = nums;
Array.Sort(numbers);
return numbers;
}
}When you run the program:
Found: Dotnet Sort
Algorithms are sorting this array: {8, 0, 7, 5, 7, 9, 5, 2, 1, 8}
Running: Dotnet Sort
Dotnet Sort took 3 ms to sort the array
- Make sure your algorithm
.csfiles are inside thealgorithmsfolder. - Compile and run the program.
- The program will:
- Detect all algorithms implementing
IAlgorithm. - Generate a random array.
- Run each algorithm and display timing and progress.
- Detect all algorithms implementing
This setup makes it easy to add, test, and benchmark new sorting algorithms without touching the main program.