public static void Main()
int[] arr = {17, 7, 2, 45, 72};
MergeSort(arr, 0, arr.Length-1);
public static void MergeSort(int[] arr, int p, int r)
public static void Merge(int[] arr, int p, int q, int r)
int[] left = new int[q-p+1];
Array.Copy(arr, p, left, 0, q-p+1);
int[] right = new int[r-q];
Array.Copy(arr, q+1, right, 0, r-q);
while (leftIndex < left.Length && rightIndex < right.Length) {
if (WillProduceSmallerNum(left[leftIndex], right[rightIndex])) {
arr[index++] = left[leftIndex++];
arr[index++] = right[rightIndex++];
while (leftIndex < left.Length) {
arr[index++] = left[leftIndex++];
while (rightIndex < right.Length) {
arr[index++] = right[rightIndex++];
public static bool WillProduceSmallerNum(int a, int b)
string normal = a.ToString() + b.ToString();
string reverse = b.ToString() + a.ToString();
return String.Compare(normal, reverse) < 1;
public static void Print(int[] arr)
StringBuilder sb = new StringBuilder();
for (int i = arr.Length-1; i >= 0; i--) {
Console.WriteLine(sb.ToString());