Esempio n. 1
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tiny.txt");   // Prompt
            Console.WriteLine("2 - words3.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tiny.txt";
                break;

            case "2":
                fieName = "words3.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Sorting\\{fieName}");
            var words = @in.ReadAllStrings();

            var list = words.Select(word => new StringComparable(word)).ToList();

            var listComparable  = list.Cast <IComparable>().ToList();
            var arrayComparable = list.Cast <IComparable>().ToArray();
            var listStrings     = words.ToList();

            // sort list
            MergeBU.Sort(listComparable);
            // print results.
            AbstractSort.Show(listComparable);

            Console.ReadLine();
        }
Esempio n. 2
0
        private static void Sort(string name, int length)
        {
            IComparable[] array;
            Stopwatch     watch = new Stopwatch();
            Example       sorter;

            switch (name)
            {
            case "Selection":
                array  = new Program().GetRandomIComparableArray(length);   //20170705
                sorter = new Selection();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("选择排序法:" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;

            case "Insertion":
                array  = new Program().GetRandomIComparableArray(length);
                sorter = new Selection();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("插入排序法:" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;

            case "InsertionPro":
                //array = new Program().GetRandomIComparableArray(length);
                //watch.Start();
                //new Insertion().SortPro(array);
                //watch.Stop();
                //Console.WriteLine("插入排序改进版法:" + watch.Elapsed);
                break;

            case "Shell":
                array  = new Program().GetRandomIComparableArray(length);
                sorter = new Shell();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("希尔排序法:" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;

            case "Merge":
                array  = new Program().GetRandomIComparableArray(length);
                sorter = new Merge();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("归并(自顶向下)排序法:" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;

            case "MergeBU":
                array  = new Program().GetRandomIComparableArray(length);
                sorter = new MergeBU();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("归并(自底向上)排序法:" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;

            case "Quick":
                array  = new Program().GetRandomIComparableArray(length);
                sorter = new Quick();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("快速排序法:" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;

            case "Quick3Way":
                array  = new Program().GetRandomIComparableArray(length);
                sorter = new Quick3Way();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("快速排序法(三向切分):" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;

            case "Heap":
                array  = new Program().GetRandomIComparableArray(length);
                sorter = new Heap();
                watch.Start();
                sorter.Sort(array);
                watch.Stop();
                Console.WriteLine("堆排序法:" + watch.Elapsed + "  结果是否有序 " + sorter.IsSorted(array));
                break;
            }
        }
Esempio n. 3
0
 protected override Action GetSortByComparer <TSource>(IList <TSource> arr, IComparer <TSource> comparer)
 {
     return(() => MergeBU.Sort(arr, comparer));
 }
Esempio n. 4
0
 protected override Action GetSimpleSort <TSource>(IList <TSource> arr)
 {
     return(() => MergeBU.Sort(arr));
 }
Esempio n. 5
0
 protected override Action GetSortBySelector <TSource, TKey>(IList <TSource> arr, Func <TSource, TKey> keySelector)
 {
     return(() => MergeBU.Sort(arr, keySelector));
 }