From d7d02569872332d140ba5d23431504e209ce651d Mon Sep 17 00:00:00 2001 From: Dima Mukhin Date: Thu, 5 Oct 2017 01:52:31 -0400 Subject: [PATCH] Fix #473: Add Merge Sort [C#] (#492) * Added Merge Sort [C#] * Added method comments and time and space complexity --- README.md | 2 +- merge_sort/MergeSort.cs | 78 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 merge_sort/MergeSort.cs diff --git a/README.md b/README.md index fa724bad..fac4ff01 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa | [Linear Search](https://en.wikipedia.org/wiki/Linear_search) | [:white_check_mark:](linear_search/linear_search.c) | | [:white_check_mark:](linear_search/LinearSearch.java) | [:white_check_mark:](linear_search/linear_search.py) | [:white_check_mark:](linear_search/linear-search.go) | [:white_check_mark:](linear_search/linearSearch.js) | [:white_check_mark:](linear_search/LinearSearch.cs) | | [Longest Common Subsequence](http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.c) | | [:white_check_mark:](longest_common_subsequence/LongestCommonSubsequence.java) | [:white_check_mark:](longest_common_subsequence/longest_common_subsequence.py) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.go) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.js) | | | [Longest Palindromic Substring](http://www.geeksforgeeks.org/longest-palindrome-substring-set-1/) | | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.cpp) | [:white_check_mark:](longest_palindromic_substring/LongestPalindromicSubstring.java) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.py) | | [:white_check_mark:](longest_palindromic_substring/longestPalindromicSubstring.js) | | -| [Merge Sort](https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/overview-of-merge-sort) | [:white_check_mark:](merge_sort/merge_sort.c) | | [:white_check_mark:](merge_sort/MergeSort.java) | [:white_check_mark:](merge_sort/merge_sort.py) | [:white_check_mark:](merge_sort/merge_sort.go) | [:white_check_mark:](merge_sort/mergeSort.js) | | +| [Merge Sort](https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/overview-of-merge-sort) | [:white_check_mark:](merge_sort/merge_sort.c) | | [:white_check_mark:](merge_sort/MergeSort.java) | [:white_check_mark:](merge_sort/merge_sort.py) | [:white_check_mark:](merge_sort/merge_sort.go) | [:white_check_mark:](merge_sort/mergeSort.js) | [:white_check_mark:](merge_sort/MergeSort.cs) | | [Modular Exponential](http://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) | [:white_check_mark:](modular_exponential/modular_exponential.c) | | [:white_check_mark:](modular_exponential/ModularExponential.java) | [:white_check_mark:](modular_exponential/modular_exponential.py) | [:white_check_mark:](modular_exponential/modular_exponential.go) | [:white_check_mark:](modular_exponential/modularExponential.js) | | | [N-Queen Problem](https://en.wikipedia.org/wiki/Eight_queens_puzzle) | | [:white_check_mark:](n_queen_problem/NQueenProblem.cpp) | [:white_check_mark:](n_queen_problem/NQueenProblem.java) | [:white_check_mark:](n_queen_problem/n_queen_problem.py) | | | | | [Prime Factor](https://en.wikipedia.org/wiki/Prime_factor) | [:white_check_mark:](prime_factor/prime_factor.c) | | [:white_check_mark:](prime_factor/PrimeFactor.java) | [:white_check_mark:](prime_factor/prime_factor.py) | [:white_check_mark:](prime_factor/prime_factor.go) | [:white_check_mark:](prime_factor/primeFactor.js) | | diff --git a/merge_sort/MergeSort.cs b/merge_sort/MergeSort.cs new file mode 100644 index 00000000..36cb97c8 --- /dev/null +++ b/merge_sort/MergeSort.cs @@ -0,0 +1,78 @@ +/* +Time Complexity: O(n log(n)) +Space Complexity: O(n) +*/ + +using System; + +public class MergeSort +{ + /// + /// Merges two unsorted halves of an array into one sorted array + /// first half: from 'first' to 'mid' + /// second half: from 'mid' to 'last' + /// + /// The original unsorted array + /// The start index of the first half of the array a + /// The middle index of the two halves of the array + /// The end index of the second half of the array a + /// A temporary array for storing the sorted halves + public static void Merge(int[] a, int first, int mid, int last, int[] temp) + { + int currL = first; //left half pointer + int currR = mid; //right half pointer + int currT; //current sorted array pointer + + for (currT = first; currT < last; currT++) + { + //if left half is element is smaller or right pointer is out of bounds + if (currL < mid && (currR >= last || a[currL] < a[currR])) + temp[currT] = a[currL++]; + else + temp[currT] = a[currR++]; + } + + for (currT = first; currT < last; currT++) + a[currT] = temp[currT]; + } + + /// + /// Recursive method for sorting an array 'a' between two indexes + /// first and last by using merge sort + /// + /// The original unsorted array + /// The start index. The array will be sorted starting from this index + /// The end index. The array will be sorted untill this index + /// A temporary array for storing merge results + public static void DoMergeSort(int[] a, int first, int last, int[] temp) + { + if (last - first > 1) + { + int mid = first + (last - first) / 2; //find the middle + DoMergeSort(a, first, mid, temp); //sort left half + DoMergeSort(a, mid, last, temp); //sort right half + Merge(a, first, mid, last, temp); //merge two sorted halves + } + } + + /// + /// Merge sort helper method. Calls the recursive Merge sort method + /// + /// The original unsorted array + public static void DoMergeSort(int[] a) + { + if (a == null || a.Length <= 1) + return; + DoMergeSort(a, 0, a.Length, new int[a.Length]); + } + + public static void Main() + { + int[] a = new int[] {2, 4, 9, 6, 7, 8}; + Console.WriteLine("Before Merge Sort:"); + Console.WriteLine("[{0}]", string.Join(", ", a)); + DoMergeSort(a); + Console.WriteLine("After Merge Sort:"); + Console.WriteLine("[{0}]", string.Join(", ", a)); + } +}