Push project

This commit is contained in:
2026-01-07 22:41:11 +08:00
commit 794fb22492
103 changed files with 6658 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
using SortingModel.SortingAlgorithms.Interfaces;
namespace SortingModel.SortingAlgorithms
{
public class BubbleSortAlgorithm : ISortingAlgorithm
{
public string Sort(string input)
{
if (string.IsNullOrEmpty(input))
return input;
char[] chars = input.ToCharArray();
int inputLength = chars.Length;
// Bubble sort algorithm
for (int i = 0; i < inputLength - 1; i++)
{
for (int j = 0; j < inputLength - i - 1; j++)
{
if (chars[j] > chars[j + 1])
{
// Swap
char temp = chars[j];
chars[j] = chars[j + 1];
chars[j + 1] = temp;
}
}
}
return new string(chars);
}
}
}

View File

@@ -0,0 +1,7 @@
namespace SortingModel.SortingAlgorithms.Interfaces
{
public interface ISortingAlgorithm
{
string Sort(string input);
}
}

View File

@@ -0,0 +1,8 @@
namespace SortingModel.SortingAlgorithms.Interfaces
{
public interface ISortingAlgorithmFactory
{
ISortingAlgorithm Create(string strategy);
IEnumerable<string> GetAvailableAlgorithmNames();
}
}

View File

@@ -0,0 +1,63 @@
using SortingModel.SortingAlgorithms.Interfaces;
namespace SortingModel.SortingAlgorithms
{
public class MergeSortAlgorithm : ISortingAlgorithm
{
public string Sort(string input)
{
if (string.IsNullOrEmpty(input))
return input;
char[] chars = input.ToCharArray();
MergeSort(chars, 0, chars.Length - 1);
return new string(chars);
}
private static void MergeSort(char[] chars, int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
MergeSort(chars, left, mid);
MergeSort(chars, mid + 1, right);
Merge(chars, left, mid, right);
}
}
private static void Merge(char[] chars, int left, int mid, int right)
{
char[] temp = new char[right - left + 1];
int i = left;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= right)
{
if (chars[i] <= chars[j])
{
temp[k++] = chars[i++];
}
else
{
temp[k++] = chars[j++];
}
}
while (i <= mid)
{
temp[k++] = chars[i++];
}
while (j <= right)
{
temp[k++] = chars[j++];
}
for (int idx = 0; idx < temp.Length; idx++)
{
chars[left + idx] = temp[idx];
}
}
}
}

View File

@@ -0,0 +1,51 @@
using SortingModel.SortingAlgorithms.Interfaces;
namespace SortingModel.SortingAlgorithms
{
public class QuickSortAlgorithm : ISortingAlgorithm
{
public string Sort(string input)
{
if (string.IsNullOrEmpty(input))
return input;
char[] chars = input.ToCharArray();
QuickSort(chars, 0, chars.Length - 1);
return new string(chars);
}
private void QuickSort(char[] chars, int left, int right)
{
if (left < right)
{
int pivotIndex = Partition(chars, left, right);
QuickSort(chars, left, pivotIndex - 1);
QuickSort(chars, pivotIndex + 1, right);
}
}
private int Partition(char[] chars, int left, int right)
{
char pivot = chars[right];
int i = left - 1;
for (int j = left; j < right; j++)
{
if (chars[j] < pivot)
{
i++;
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
// Swap pivot
char pivotTemp = chars[i + 1];
chars[i + 1] = chars[right];
chars[right] = pivotTemp;
return i + 1;
}
}
}

View File

@@ -0,0 +1,36 @@
using SortingModel.SortingAlgorithms.Interfaces;
namespace SortingModel.SortingAlgorithms
{
public class SortingAlgorithmFactory : ISortingAlgorithmFactory
{
private static readonly IReadOnlyDictionary<string, SortingAlgorithmType> NameToTypeMap
= new Dictionary<string, SortingAlgorithmType>
{
{ "Bubble Sort", SortingAlgorithmType.BubbleSort },
{ "Merge Sort", SortingAlgorithmType.MergeSort },
{ "Quick Sort", SortingAlgorithmType.QuickSort }
};
private static readonly Dictionary<SortingAlgorithmType, Func<ISortingAlgorithm>> TypeToAlgorithmMap
= new Dictionary<SortingAlgorithmType, Func<ISortingAlgorithm>>
{
{ SortingAlgorithmType.BubbleSort, () => new BubbleSortAlgorithm() },
{ SortingAlgorithmType.MergeSort, () => new MergeSortAlgorithm() },
{ SortingAlgorithmType.QuickSort, () => new QuickSortAlgorithm() }
};
public ISortingAlgorithm Create(string strategy)
{
if(NameToTypeMap.TryGetValue(strategy, out SortingAlgorithmType algorithmType)) {
if(TypeToAlgorithmMap.TryGetValue(algorithmType, out var algorithm)) {
return algorithm();
}
}
return TypeToAlgorithmMap[SortingAlgorithmType.BubbleSort]();
}
public IEnumerable<string> GetAvailableAlgorithmNames() => NameToTypeMap.Keys;
}
}