From c25f746db67c545e7059576425831f3e37494f00 Mon Sep 17 00:00:00 2001 From: Waqar Hassan Khan Date: Sun, 26 Sep 2021 20:45:46 +0600 Subject: [PATCH] chore(CSharp): added merge sort (#484) --- algorithms/CSharp/README.md | 1 + algorithms/CSharp/src/Sorts/merge-sort.cs | 67 ++++++++++++++++++++++ algorithms/CSharp/test/Sorts/merge-sort.cs | 39 +++++++++++++ docs/en/Sorting/Merge-Sort.md | 1 + 4 files changed, 108 insertions(+) create mode 100644 algorithms/CSharp/src/Sorts/merge-sort.cs create mode 100644 algorithms/CSharp/test/Sorts/merge-sort.cs diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 109bb20e..3c818b97 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -7,6 +7,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ 2. [Insertion Sort](src/Sorts/insertion-sort.cs) 3. [Selection Sort](src/Sorts/selection-sort.cs) 4. [Counting Sort](src/Sorts/counting-sort.cs) +5. [Merge Sort](src/Sorts/merge-sort.cs) ## Strings - [Palindrome](src/Strings/palindrome.cs) diff --git a/algorithms/CSharp/src/Sorts/merge-sort.cs b/algorithms/CSharp/src/Sorts/merge-sort.cs new file mode 100644 index 00000000..05c34987 --- /dev/null +++ b/algorithms/CSharp/src/Sorts/merge-sort.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Algorithms.Sorts +{ + public class MergeSort + { + public static List Sort(List numbers) + { + Divide(numbers, 0, numbers.Count - 1); + return numbers; + } + + public static void Merge(List numbers, int left, int mid, int right) + { + List leftPartition = new List(); + List rightPartition = new List(); + + 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 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 numbers = new List { 9, 1, -1, 10, 12, 2, 0, 0, -2, -9 , 107}; + List answer = Sort(numbers); + + Console.WriteLine(string.Join(", ", answer)); + } + } +} diff --git a/algorithms/CSharp/test/Sorts/merge-sort.cs b/algorithms/CSharp/test/Sorts/merge-sort.cs new file mode 100644 index 00000000..2a08872e --- /dev/null +++ b/algorithms/CSharp/test/Sorts/merge-sort.cs @@ -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{ 0, 19, 12, 22, 107, 118, 0, 1, 2}, + "0, 0, 1, 2, 12, 19, 22, 107, 118" + }, + + new object[] { + new List{ 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{ -1, -2, -3, -4, -5, -10}, + "-10, -5, -4, -3, -2, -1" + }, + + new object[] { + new List{ -1 }, + "-1" + } + }; + + [TestCaseSource(nameof(TestCasesForMergeSort))] + public void TestMergeSort_ShouldGetExpected(List numbers, string expected) + { + List results = Algorithms.Sorts.MergeSort.Sort(numbers); + Assert.AreEqual(string.Join(", ", results), expected); + } + } +} diff --git a/docs/en/Sorting/Merge-Sort.md b/docs/en/Sorting/Merge-Sort.md index 02426d89..fce83fb5 100644 --- a/docs/en/Sorting/Merge-Sort.md +++ b/docs/en/Sorting/Merge-Sort.md @@ -24,6 +24,7 @@ Sorted array is - [C++](../../../algorithms/CPlusPlus/Sorting/merge-sort.cpp) - [JavaScript](../../../algorithms/JavaScript/src/sorting/merge-sort.js) - [Python](../../../algorithms/Python/sorting/merge_sort.py) +- [C#](../../../algorithms/CSharp/src/Sorts/merge-sort.cs) ## Video URL