public static void Main(string[] args) { MergeSort ms = new MergeSort(); int[] arr = Console.ReadLine().Split().Select(i => int.Parse(i)).ToArray(); ms.MergeSortMethod(arr, 0, arr.Length - 1); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine(); }
public void Sort(IComparable[] items) { var length = items.Length; var helperArray = new IComparable[length]; for (int subarraySize = 1; subarraySize < length; subarraySize += subarraySize)//1,2,4,8,16,32 { for (int startIndex = 0; startIndex < length - subarraySize; startIndex += subarraySize + subarraySize) { int middleIndex = startIndex + subarraySize - 1, endIndex = Math.Min(middleIndex + subarraySize, length - 1); MergeSort.Merge(items, helperArray, startIndex, middleIndex, endIndex); } } }
private static void Sort(int[] data, int[] copy, int start, int end) { if (start >= end) { return; } int middle = (end - start) / 2 + start; MergeSort.Sort(data, copy, start, middle); MergeSort.Sort(data, copy, middle + 1, end); Array.Copy(data, start, copy, start, end - start + 1); int left = start; int right = middle + 1; int i = start; while (left <= middle && right <= end) { if (copy[right] < copy[left]) { data[i] = copy[right]; right++; } else { data[i] = copy[left]; left++; } i++; } while (left <= middle) { data[i] = copy[left]; left++; i++; } }
public static void Sort(int[] data) { int[] copy = new int[data.Length]; MergeSort.Sort(data, copy, 0, data.Length - 1); }