chore(CSharp): added merge sort (#484)

pull/488/head
Waqar Hassan Khan 2021-09-26 20:45:46 +06:00 committed by GitHub
parent b561c2e0dc
commit c25f746db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 0 deletions

View File

@ -7,6 +7,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
2. [Insertion Sort](src/Sorts/insertion-sort.cs) 2. [Insertion Sort](src/Sorts/insertion-sort.cs)
3. [Selection Sort](src/Sorts/selection-sort.cs) 3. [Selection Sort](src/Sorts/selection-sort.cs)
4. [Counting Sort](src/Sorts/counting-sort.cs) 4. [Counting Sort](src/Sorts/counting-sort.cs)
5. [Merge Sort](src/Sorts/merge-sort.cs)
## Strings ## Strings
- [Palindrome](src/Strings/palindrome.cs) - [Palindrome](src/Strings/palindrome.cs)

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Algorithms.Sorts
{
public class MergeSort
{
public static List<int> Sort(List<int> numbers)
{
Divide(numbers, 0, numbers.Count - 1);
return numbers;
}
public static void Merge(List<int> numbers, int left, int mid, int right)
{
List<int> leftPartition = new List<int>();
List<int> rightPartition = new List<int>();
for(int i = left; i <= mid; i++)
{
leftPartition.Add(numbers[i]);
}
for(int i = mid + 1; i <= right; i++)
{
rightPartition.Add(numbers[i]);
}
int l = 0, r = 0;
int size1 = leftPartition.Count, size2 = rightPartition.Count;
for(int i = left; i <= right; i++)
{
if((l < size1 && r >= size2) || (l < size1 && r < size2 && leftPartition[l] < rightPartition[r]))
{
numbers[i] = leftPartition[l++];
}
else
{
numbers[i] = rightPartition[r++];
}
}
}
public static void Divide(List<int> numbers, int left, int right)
{
if(left < right)
{
int mid = (left + right) / 2;
Divide(numbers, left, mid);
Divide(numbers, mid + 1, right);
Merge(numbers, left, mid, right);
}
}
public static void Main()
{
List<int> numbers = new List<int> { 9, 1, -1, 10, 12, 2, 0, 0, -2, -9 , 107};
List<int> answer = Sort(numbers);
Console.WriteLine(string.Join(", ", answer));
}
}
}

View File

@ -0,0 +1,39 @@
using NUnit.Framework;
using System.Collections.Generic;
namespace Algorithms.Tests.Sorts
{
[TestFixture]
class MergeSort
{
static object[] TestCasesForMergeSort =
{
new object[] {
new List<int>{ 0, 19, 12, 22, 107, 118, 0, 1, 2},
"0, 0, 1, 2, 12, 19, 22, 107, 118"
},
new object[] {
new List<int>{ 10, 11, 19, 0, -1, -19, -12, 1, 2, 1, 16, -100},
"-100, -19, -12, -1, 0, 1, 1, 2, 10, 11, 16, 19"
},
new object[] {
new List<int>{ -1, -2, -3, -4, -5, -10},
"-10, -5, -4, -3, -2, -1"
},
new object[] {
new List<int>{ -1 },
"-1"
}
};
[TestCaseSource(nameof(TestCasesForMergeSort))]
public void TestMergeSort_ShouldGetExpected(List<int> numbers, string expected)
{
List<int> results = Algorithms.Sorts.MergeSort.Sort(numbers);
Assert.AreEqual(string.Join(", ", results), expected);
}
}
}

View File

@ -24,6 +24,7 @@ Sorted array is
- [C++](../../../algorithms/CPlusPlus/Sorting/merge-sort.cpp) - [C++](../../../algorithms/CPlusPlus/Sorting/merge-sort.cpp)
- [JavaScript](../../../algorithms/JavaScript/src/sorting/merge-sort.js) - [JavaScript](../../../algorithms/JavaScript/src/sorting/merge-sort.js)
- [Python](../../../algorithms/Python/sorting/merge_sort.py) - [Python](../../../algorithms/Python/sorting/merge_sort.py)
- [C#](../../../algorithms/CSharp/src/Sorts/merge-sort.cs)
## Video URL ## Video URL