Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Merge Sort [C#] #492

Merged
merged 3 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | |
| [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
51 changes: 51 additions & 0 deletions merge_sort/MergeSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

public class MergeSort
{
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];
}

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
}
}

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));
}
}