Exemple #1
0
        public void Quick3WaySort_String_TestOnUnsortedArrayWithDuplicates()
        {
            var array = new string[] { "f", "b", "g", "a", "d", "e", "a", "c", "b" };

            Quick3Way.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Exemple #2
0
        public void BinarySearchTinyWT()
        {
            var input = new int[] { 84, 48, 68, 10, 18, 98, 12, 23, 54, 57, 33, 16, 77, 11, 29 };

            Quick3Way <int> .Sort(input);

            Assert.Equal(-1, BinarySearch.Rank(50, input));
            Assert.NotEqual(-1, BinarySearch.Rank(input[0], input));
            Assert.NotEqual(-1, BinarySearch.Rank(input[14], input));

            input = new int[] { 23, 50, 10, 99, 18, 23, 98, 84, 11, 10, 48, 77, 13, 54, 98, 77, 77, 68 };
            Quick3Way <int> .Sort(input);

            Assert.NotEqual(-1, BinarySearch.Rank(50, input));

            var r0 = BinarySearch.Rank(input[0], input);

            Assert.NotEqual(-1, r0);
            // on equal indices higher one returned
            //Assert.Equal(0, r0);

            var r17 = BinarySearch.Rank(input[17], input);

            Assert.NotEqual(-1, r17);
            Assert.Equal(17, r17);
        }
Exemple #3
0
        public void Quick3WaySort_Int_TestOnEmptyArray()
        {
            var array = new int[] { };

            Quick3Way.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Exemple #4
0
        public void Quick3WaySort_String_TestOnSortedArray()
        {
            var array = new string[] { "a", "b", "c", "d", "e", "f", "g" };

            Quick3Way.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Exemple #5
0
        public void Quick3WaySort_Int_TestOnUnsortedArrayWithDuplicates()
        {
            var array = new int[] { 0, 3, 9, 7, 1, 4, 5, 2, 7, 8, 6, 3, 1 };

            Quick3Way.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Exemple #6
0
        public void Quick3WaySort_Int_TestOnReverseSortedArray()
        {
            var array = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

            Quick3Way.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Exemple #7
0
        public void Quick3WaySort_Int_TestOnSortedArray()
        {
            var array = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Quick3Way.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Exemple #8
0
        public void Quick3WaySort_Int_TestOnArrayWithOneElement()
        {
            var array = new int[] { 1 };

            Quick3Way.Sort(array);
            Assert.IsTrue(array.IsSorted());
        }
Exemple #9
0
        static void Main(string[] args)
        {
            //Console.WriteLine(sizeof(int));
            //Console.WriteLine(sizeof(double));
            //Console.WriteLine(sizeof(char));
            //Console.WriteLine(sizeof(bool));
            //int x = 5;
            //Console.WriteLine(x+=5*3);
            //Console.WriteLine();

            int[] array = new[] { 8, 5, 1, 9, 3, 15, 7, 25, 6, 32, 35 };
            //QuickSort.Sort(array, 0, array.Length - 1);
            //Console.WriteLine();

            Quick3Way.Sort(array, 0, array.Length - 1);
            foreach (var i in array)
            {
                Console.WriteLine(i);
            }


            //Graph 换成 DirectedGraph        ConnectedComponent 换成 KosarajuConnectedComponent 即有向无环图的连通分量
            Graph g = new Graph();
            ConnectedComponent cc = new ConnectedComponent(g);

            int m = cc.Count;

            ConcurrentBag <int>[] components = new ConcurrentBag <int> [m];
            for (int i = 0; i < m; i++)
            {
                components[i] = new ConcurrentBag <int>();
            }

            for (int i = 0; i < g.VertexSize; i++)
            {
                components[cc.Id(i)].Add(i);
            }

            for (int i = 0; i < m; i++)
            {
                foreach (var i1 in components[i])
                {
                    Console.Write($"{i1} ");
                }
                Console.WriteLine();
            }

            KnuthMorrisPrattSearch search = new KnuthMorrisPrattSearch("aabaaaaa");

            Console.WriteLine(search.Search("aabaabaaaaaa"));
        }
Exemple #10
0
        public void Test07()
        {
            String[] a = new String[]
            {
                "S", "H", "E", "L", "L", "S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"
            };
            SortBase s = new Quick3Way();

            s.Sort(a);
            if (!s.IsSorted(a))
            {
                Console.WriteLine("排序失败");
                throw new Exception("排序失败");
            }

            s.Show(a);
        }
        private void ArrayRanks(int[] array, int[] ins, int[] outs)
        {
            // in place sort
            Quick3Way <int> .Sort(array);

            // contained
            foreach (int i in ins)
            {
                int rank = BinarySearch.Rank(i, array);
                Assert.NotEqual(-1, rank);
            }

            // missing
            foreach (int i in outs)
            {
                int rank = BinarySearch.Rank(i, array);
                Assert.Equal(-1, rank);
            }
        }
Exemple #12
0
        public void SortingPerformanceTest()
        {
            var stopwatch   = new Stopwatch();
            var sortedArray = new int[size];

            for (int i = 0; i < size; i++)
            {
                sortedArray[i] = i;
            }
            var sorts = new SortInfo[]
            {
                new SortInfo {
                    Name = "Selection Sort", Act = array => Selection.Sort(array), Enabled = false
                },
                new SortInfo {
                    Name = "Insertion Sort", Act = array => Insertion.Sort(array), Enabled = false
                },
                new SortInfo {
                    Name = "Shell Sort", Act = array => Shell.Sort(array), Enabled = false
                },
                new SortInfo {
                    Name = "Mergesort", Act = array => Merge.Sort(array), Enabled = true
                },
                new SortInfo {
                    Name = "Quicksort", Act = array => Quick.Sort(array), Enabled = true
                },
                new SortInfo {
                    Name = "3-way Quicksort", Act = array => Quick3Way.Sort(array), Enabled = true
                },
                new SortInfo {
                    Name = "Heapsort", Act = array => Heap.Sort(array), Enabled = true
                }
            };

            foreach (var sort in sorts.Where(s => s.Enabled))
            {
                sortedArray.Shuffle();
                stopwatch.Restart();
                sort.Act(sortedArray);
                stopwatch.Stop();
                Console.WriteLine("{0}:\t{1}", sort.Name, stopwatch.Elapsed);
            }
        }
Exemple #13
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;
            }
        }