예제 #1
0
        static void Main(string[] args)
        {
            int n = 1000000;

            int[] a = SortTestHelper.generateRandomArray(n, 0, n);
            int[] d = SortTestHelper.generateNearlyOrderedArray(n, 100);

            //SelectionSort.sort(a);
            //SortTestHelper.printArray(a);
            int[] b = new int[a.Length];
            int[] c = new int[a.Length];
            int[] e = new int[a.Length];
            int[] f = new int[a.Length];
            int[] g = new int[a.Length];
            int[] h = new int[a.Length];
            int[] i = new int[a.Length];
            int[] j = new int[a.Length];
            int[] k = new int[a.Length];
            int[] l = new int[a.Length];



            Array.Copy(a, b, a.Length);
            Array.Copy(a, c, a.Length);
            Array.Copy(a, e, a.Length);
            Array.Copy(a, f, a.Length);
            Array.Copy(a, h, a.Length);
            Array.Copy(a, i, a.Length);
            Array.Copy(a, j, a.Length);
            Array.Copy(a, k, a.Length);
            Array.Copy(a, l, a.Length);



            //   SortTestHelper.testSort("Selection Sort", SelectionSort.sort, a);
            //   SortTestHelper.testSort("Insertion Sort Basic", InsertionSort.sortBasic, b);
            SortTestHelper.testSort("Insertion Sort Advanced", InsertionSort.sortAdvanced, c);
            SortTestHelper.testSort("Insertion Sort Advanced Nearly Ordered", InsertionSort.sortAdvanced, d);
            // SortTestHelper.testSort("Bubble Sort", BubbleSort.sort, e);
            //SortTestHelper.testSort("Shell Sort", ShellSort.sort, f);
            SortTestHelper.testSort("Merge Sort", MergeSort <int> .sort, g);
            SortTestHelper.testSort("Merge Sort Advanced", MergeSortAdvanced <int> .sort, h);
            SortTestHelper.testSort("Merge Sort Buttom UP", MergeSortBU <int> .sort, i);
            SortTestHelper.testSort("Quick Sort", QuickSort.sort, j);
            SortTestHelper.testSort("Heap Sort", HeapSort.sort, k);
            SortTestHelper.testSort("Heap Sort - no aux array", HeapSort2.sort, l);



            //foreach (int o in l)
            //{
            //    Console.WriteLine(o);
            //}

            Console.WriteLine("------------------------");
            float[] z = { 3.1F, 2.2F, 3.2F, 1.1F };
            InsertionSort.sortBasic(z);

            foreach (float y in z)
            {
                Console.WriteLine(y);
            }

            Student[] stu = { new Student("A", 10), new Student("C", 30), new Student("B", 20), new Student("D", 20) };

            SelectionSort.sort <Student>(stu);
            foreach (Student y in stu)
            {
                Console.WriteLine(y);
            }
        }
예제 #2
0
        public static void Run()
        {
            var sample1 = Utility.RandomIntegers(20000, 100000);
            var sample2 = sample1.Select(i => i).ToList();  //make a deep copy
            var sample3 = sample1.Select(i => i).ToList();  //make a deep copy
            var sample4 = sample1.Select(i => i).ToList();  //make a deep copy
            var sample5 = sample1.Select(i => i).ToList();  //make a deep copy
            var sample6 = sample1.Select(i => i).ToList();  //make a deep copy
            var sample7 = sample1.Select(i => i).ToList();  //make a deep copy

            Console.WriteLine("----------------Verify Sort Functionality-------------------");
            List <int> demo = new List <int> {
                5, 4, 7, 3, 3, 9
            };
            var result = InsertionSort.Sort(demo);

            Console.WriteLine("Insertion Sort: {0}", string.Join(",", result.ConvertAll <string>(x => x.ToString())));

            demo = new List <int> {
                5, 4, 7, 3, 3, 9
            };
            result = BubbleSort.Sort(demo);
            Console.WriteLine("Bubble Sort: {0}", string.Join(",", result.ConvertAll <string>(x => x.ToString())));

            demo = new List <int> {
                5, 4, 7, 3, 3, 9
            };
            result = MergeSort.Sort(demo);
            Console.WriteLine("Merge Sort: {0}", string.Join(",", result.ConvertAll <string>(x => x.ToString())));

            demo = new List <int> {
                5, 4, 7, 3, 3, 9
            };
            result = HeapSort.Sort(demo);
            Console.WriteLine("Heap Sort: {0}", string.Join(",", result.ConvertAll <string>(x => x.ToString())));

            demo = new List <int> {
                5, 4, 7, 3, 3, 9
            };
            result = QuickSort.Sort(demo);
            Console.WriteLine("Quick Sort: {0}", string.Join(",", result.ConvertAll <string>(x => x.ToString())));

            demo = new List <int> {
                5, 4, 7, 3, 3, 9
            };
            result = CountSort.Sort(demo, 9);
            Console.WriteLine("CountSort Sort: {0}", string.Join(",", result.ConvertAll <string>(x => x.ToString())));

            int selectIndex = 5;

            Console.WriteLine("Select {0}: {1}", selectIndex.ToString(), QuickSort.Randomized_Select(demo, 0, 5, selectIndex));

            Console.WriteLine("----------------Average Time-------------------");
            Sort(sample1, InsertionSort.Sort);
            Sort(sample2, BubbleSort.Sort);
            Sort(sample3, MergeSort.Sort);
            Sort(sample4, HeapSort.Sort);
            Sort(sample5, QuickSort.Sort);
            Sort(sample5, QuickSort.RandomizedSort);
            CountSortSample(sample7, 100000);

            Stopwatch sw = new Stopwatch();

            Console.WriteLine("List.Sort() starts..");
            sw.Start();
            sample6.Sort();
            sw.Stop();
            Console.WriteLine("List.Sort() ends with time {0}", sw.ElapsedMilliseconds.ToString()); //Microsoft fastest.

            Console.WriteLine("----------------Best Time-------------------");                      //an aleady sorted list.
            Sort(sample5, InsertionSort.Sort);
            Sort(sample5, BubbleSort.Sort);
            Sort(sample5, MergeSort.Sort);
            Sort(sample5, HeapSort.Sort);
            //Sort(sample5, QuickSort.Sort);
            Sort(sample5, QuickSort.RandomizedSort);
        }