예제 #1
0
        public static SubList <T> SortedMerge(SubList <T> first, SubList <T> second, IComparer <T> comp)
        {
            if ((first.Index + first.Length != second.Index &&
                 second.Index + second.Length != first.Index) ||
                first.MainList != second.MainList)
            {
                throw new InvalidOperationException();
            }

            SubList <T> result = new SubList <T>(first.MainList,
                                                 Math.Min(first.Index, second.Index),
                                                 first.Length + second.Length);

            var mainList = first.MainList;

            for (int i = Math.Max(first.Index, second.Index); i < result.Length; i++)
            {
                for (int j = i - 1; j >= 0; j--)
                {
                    if (comp.Compare(mainList[j], mainList[i]) < 0)
                    {
                        if (i != j + 1)
                        {
                            T temp = mainList[i];
                            mainList.RemoveAt(i);
                            mainList.Insert(j + 1, temp);
                        }
                        break;
                    }
                }
            }

            return(result);
        }
예제 #2
0
 public static void MergeSort <T>(this IList <T> list, IComparer <T> comp)
 {
     if (list.Count > 1)
     {
         SubList <T> first  = new SubList <T>(list, 0, list.Count / 2);
         SubList <T> second = new SubList <T>(list, first.Length, list.Count - first.Length);
         first.MergeSort(comp);
         second.MergeSort(comp);
         SubList <T> .SortedMerge(first, second, comp);
     }
 }