Beispiel #1
0
        /// <summary>
        /// 归并排序
        /// </summary>
        /// <param name="list"></param>
        /// <param name="isAsc"></param>
        public static void Sort(IList <T> list, bool isAsc)
        {
            int       length     = 1;
            IList <T> sortedList = new List <T>(list.Count);

            foreach (T item in list)
            {
                sortedList.Add(item);
            }
            while (length < list.Count)
            {
                MergeSorter <T> .MergePass(list, sortedList, length, isAsc);

                length *= 2;
                MergeSorter <T> .MergePass(sortedList, list, length, isAsc);

                length *= 2;
            }
        }
Beispiel #2
0
        private static void MergePass(IList <T> list, IList <T> sortedList, int length, bool isASC)
        {
            int i, j;

            for (i = 0; i <= list.Count - 2 * length; i += 2 * length)
            {
                MergeSorter <T> .Merge(list, i, i + length - 1, i + 2 *length - 1, sortedList, isASC);
            }
            if (i + length < list.Count)
            {
                MergeSorter <T> .Merge(list, i, i + length - 1, list.Count - 1, sortedList, isASC);
            }
            else
            {
                for (j = i; j < list.Count; j++)
                {
                    sortedList[j] = list[j];
                }
            }
        }