Push project
This commit is contained in:
33
Sorting/SortingAlgorithms/BubbleSortAlgorithm.cs
Normal file
33
Sorting/SortingAlgorithms/BubbleSortAlgorithm.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SortingModel.SortingAlgorithms.Interfaces
|
||||
{
|
||||
public interface ISortingAlgorithm
|
||||
{
|
||||
string Sort(string input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace SortingModel.SortingAlgorithms.Interfaces
|
||||
{
|
||||
public interface ISortingAlgorithmFactory
|
||||
{
|
||||
ISortingAlgorithm Create(string strategy);
|
||||
IEnumerable<string> GetAvailableAlgorithmNames();
|
||||
}
|
||||
}
|
||||
63
Sorting/SortingAlgorithms/MergeSortAlgorithm.cs
Normal file
63
Sorting/SortingAlgorithms/MergeSortAlgorithm.cs
Normal 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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
51
Sorting/SortingAlgorithms/QuickSortAlgorithm.cs
Normal file
51
Sorting/SortingAlgorithms/QuickSortAlgorithm.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Sorting/SortingAlgorithms/SortingAlgorithmFactory.cs
Normal file
36
Sorting/SortingAlgorithms/SortingAlgorithmFactory.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user