Skip to content

Commit

Permalink
Fix iiitv#473: Add Merge Sort [C#] (iiitv#492)
Browse files Browse the repository at this point in the history
* Added Merge Sort [C#]

* Added method comments and time and space complexity
  • Loading branch information
DimaMukhin authored and singhpratyush committed Oct 5, 2017
1 parent 444d6ca commit d7d0256
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | |
Expand Down
78 changes: 78 additions & 0 deletions merge_sort/MergeSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Time Complexity: O(n log(n))
Space Complexity: O(n)
*/

using System;

public class MergeSort
{
/// <summary>
/// Merges two unsorted halves of an array into one sorted array
/// first half: from 'first' to 'mid'
/// second half: from 'mid' to 'last'
/// </summary>
/// <param name="a">The original unsorted array</param>
/// <param name="first">The start index of the first half of the array a</param>
/// <param name="mid">The middle index of the two halves of the array</param>
/// <param name="last">The end index of the second half of the array a</param>
/// <param name="temp">A temporary array for storing the sorted halves</param>
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];
}

/// <summary>
/// Recursive method for sorting an array 'a' between two indexes
/// first and last by using merge sort
/// </summary>
/// <param name="a">The original unsorted array</param>
/// <param name="first">The start index. The array will be sorted starting from this index</param>
/// <param name="last">The end index. The array will be sorted untill this index</param>
/// <param name="temp">A temporary array for storing merge results</param>
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
}
}

/// <summary>
/// Merge sort helper method. Calls the recursive Merge sort method
/// </summary>
/// <param name="a">The original unsorted array</param>
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));
}
}

0 comments on commit d7d0256

Please sign in to comment.