using System; using System.Collections.Generic; namespace NAudio.Utils { class MergeSort { /// /// In-place and stable implementation of MergeSort /// static void Sort(IList list, int lowIndex, int highIndex, IComparer comparer) { if (lowIndex >= highIndex) { return; } int midIndex = (lowIndex + highIndex) / 2; // Partition the list into two lists and Sort them recursively Sort(list, lowIndex, midIndex, comparer); Sort(list, midIndex + 1, highIndex, comparer); // Merge the two sorted lists int endLow = midIndex; int startHigh = midIndex + 1; while ((lowIndex <= endLow) && (startHigh <= highIndex)) { // MRH, if use < 0 sort is not stable if (comparer.Compare(list[lowIndex], list[startHigh]) <= 0) { lowIndex++; } else { // list[lowIndex] > list[startHigh] // The next element comes from the second list, // move the list[start_hi] element into the next // position and shuffle all the other elements up. T t = list[startHigh]; for (int k = startHigh - 1; k >= lowIndex; k--) { list[k + 1] = list[k]; } list[lowIndex] = t; lowIndex++; endLow++; startHigh++; } } } /// /// MergeSort a list of comparable items /// public static void Sort(IList list) where T : IComparable { Sort(list, 0, list.Count - 1, Comparer.Default); } /// /// MergeSort a list /// public static void Sort(IList list, IComparer comparer) { Sort(list, 0, list.Count - 1, comparer); } } }