You are nearly there. However, let's improve it a little:
Interface
public interface ISort
{
T[] Sort<T>(T[] data) where T : IComparable;
}
BubbleSort
public class BubbleSort : ISort
{
public T[] Sort<T>(T[] data) where T : IComparable
{
var result = data.ToArray();
for (var x = 1; x < result.Length; x++)
{
for (var y = 0; y < result.Length - 1; y++)
{
if (result[y].CompareTo(result[y + 1]) <= 0) continue;
var temp = result[y];
result[y] = result[y + 1];
result[y + 1] = temp;
}
}
return result;
}
}
QuickSort
public class QuickSort : ISort
{
public T[] Sort<T>(T[] data) where T : IComparable
{
var result = data.ToArray();
InternalQuickSort(result, 0, result.Length);
return result;
}
private static void InternalQuickSort<T>(T[] data, int start, int end) where T : IComparable
{
if (start >= end) return;
var pivotIndex = Partition(data, start, end);
InternalQuickSort(data, start, pivotIndex);
InternalQuickSort(data, pivotIndex + 1, end);
}
private static void Swap<T>(T[] data, int i, int j) where T : IComparable
{
T temp = data[i];
data[i] = data[j];
data[j] = temp;
}
private static int Partition<T>(T[] data, int start, int end) where T : IComparable
{
var pivotIndex = data[start];
var swapIndex = start;
for (var i = start + 1; i < end; i++)
{
if (data[i].CompareTo(pivotIndex) >= 0) continue;
swapIndex++;
Swap(data, i, swapIndex);
}
Swap(data, start, swapIndex);
return swapIndex;
}
}
Usage
private static ISort GetSorter(char option)
=> char.ToLower(option) switch
{
'a' => new BubbleSort(),
'b' => new QuickSort(),
_ => null
};
static void Main(string[] args)
{
char response;
do
{
Console.WriteLine();
Console.WriteLine("Choose between the two sorting strategies:");
Console.WriteLine(" a) - Bubble Sort");
Console.WriteLine(" b) - Quick Sort");
Console.WriteLine(" x) - To Quit");
Console.WriteLine();
Console.Write("Your option: ");
if (!char.TryParse(Console.ReadLine(), out response))
continue;
if (response == 'x')
break;
var sorter = GetSorter(response);
if (sorter == null)
continue;
Console.Write("Type something to sort: ");
var data = Console.ReadLine();
Console.WriteLine();
var result = sorter.Sort(data.ToCharArray());
Console.WriteLine(string.Join(", ", result));
} while (response != 'x');
}
Output
Choose between the two sorting strategies:
a) - Bubble Sort
b) - Quick Sort
x) - To Quit
Your option: a
Type something to sort: qwertyuiop
e, i, o, p, q, r, t, u, w, y
Choose between the two sorting strategies:
a) - Bubble Sort
b) - Quick Sort
x) - To Quit
Your option: b
Type something to sort: qwertyuiop
e, i, o, p, q, r, t, u, w, y
Choose between the two sorting strategies:
a) - Bubble Sort
b) - Quick Sort
x) - To Quit
Your option: x
Additional Resources
Generics (C# Programming Guide)
Generics introduce the concept of type parameters to .NET, which make
it possible to design classes and methods that defer the specification
of one or more types until the class or method is declared and
instantiated by client code. For example, by using a generic type
parameter T, you can write a single class that other client code can
use without incurring the cost or risk of runtime casts or boxing
operations
IComparable Interface
Defines a generalized type-specific comparison method that a value
type or class implements to order or sort its instances.