Ejemplo n.º 1
0
        public void SortingTest()
        {
            Action <int[]>[] actions = new Action <int[]>[]
            {
                BubbleSort.Sort,
                data => BucketSort.Sort(data, SortingTests.MaxValue),
                data => CountingSort.Sort(data, SortingTests.MaxValue),
                HeapSort.Sort,
                InsertionSort.Sort,
                MergeSort.Sort,
                QuickSort.Sort,
                data => RadixSort.Sort(data, SortingTests.MaxValue),
            };

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    int[]   data    = ArrayUtilities.CreateRandomArray(j, 0, SortingTests.MaxValue);
                    int[][] results = new int[actions.Length][];

                    for (int k = 0; k < actions.Length; k++)
                    {
                        results[k] = new int[data.Length];
                        Array.Copy(data, results[k], data.Length);

                        actions[k](results[k]);
                        Assert.IsTrue(ArrayUtilities.AreEqual(results[k], results[0]));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void TestSort()
        {
            var arr = new[] { 3, 41, 52, 26, 38, 57, 9, 49 };

            BucketSort.Sort(arr);
            Assert.IsTrue(arr.SequenceEqual(new[] { 3, 9, 26, 38, 41, 49, 52, 57 }));
        }
Ejemplo n.º 3
0
        public BucketSortTests()
        {
            var sort = new BucketSort <int>();

            func      = array => sort.Sort(array);
            this.sort = sort;
            algorithm = nameof(BucketSort <int>);
        }
        public void BucketSort_Smoke_Test()
        {
            var result = BucketSort.Sort(TestArray, 4);

            for (int i = 0; i < TestArray.Length; i++)
            {
                Assert.AreEqual(i, result[i]);
            }
        }
Ejemplo n.º 5
0
        public void Test_BucketSort()
        {
            int[] arr = new int[] { -1, 25, -58964, 8547, -119, 0, 78596 };

            var result = sort.Sort(arr, arr.Length - 1);

            int[] expectedAsc = { -58964, -119, -1, 0, 25, 8547, 78596 };
            Assert.Equal(expectedAsc, result);
        }
        public void BucketSort_Descending_Smoke_Test()
        {
            var result = BucketSort.Sort(testArray, 11, SortDirection.Descending);

            for (int i = 0; i < testArray.Length; i++)
            {
                Assert.AreEqual(testArray.Length - i - 1, result[i]);
            }
        }
        public void BucketSort_Ascending_Smoke_Test()
        {
            var result = BucketSort.Sort(testArray, 11);

            for (int i = 0; i < testArray.Length; i++)
            {
                Assert.AreEqual(i, result[i]);
            }
        }
Ejemplo n.º 8
0
        public void Sort()
        {
            float[]    arr        = new float[] { 0.897F, 0.565F, 0.656F, 0.1234F, 0.665F, 0.3434F };
            float[]    resultArr  = new float[] { 0.1234F, 0.3434F, 0.565F, 0.656F, 0.665F, 0.897F };
            BucketSort bucketSort = new BucketSort();

            bucketSort.Sort(arr);
            Assert.Equal(resultArr, arr);
        }
Ejemplo n.º 9
0
        public void SortWithEmptyArray()
        {
            float[]    arr        = new float[] {};
            float[]    resultArr  = new float[] {};
            BucketSort bucketSort = new BucketSort();

            bucketSort.Sort(arr);

            Assert.Equal(resultArr, arr);
        }
Ejemplo n.º 10
0
        public static void Main()
        {
            var elements = Console.ReadLine()
                           .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                           .Select(int.Parse)
                           .ToArray();

            BucketSort.Sort(elements, elements.Length);
            Console.WriteLine(string.Join(" ", elements));
        }
Ejemplo n.º 11
0
 public void BucketSortTest()
 {
     for (int i = 0; i < 100; i++)
     {
         int[] originData = Util.GenRandomIntArr();
         int[] sortedData = (int[])originData.Clone();
         Array.Sort(sortedData);
         BucketSort.Sort(originData);
         Assert.IsTrue(Util.CompareList(originData, sortedData));
     }
 }
Ejemplo n.º 12
0
        public void ArraySorted([Random(0, 1000, 100, Distinct = true)] int n)
        {
            // Arrange
            var sorter      = new BucketSort();
            var intComparer = new IntComparer();

            var(correctArray, testArray) = RandomHelper.GetArrays(n);

            // Act
            sorter.Sort(testArray, intComparer);
            Array.Sort(correctArray);

            // Assert
            Assert.AreEqual(correctArray, testArray);
        }
        public void BucketSort_Stress_Test()
        {
            var rnd           = new Random();
            var nodeCount     = 1000 * 1000;
            var randomNumbers = Enumerable.Range(1, nodeCount)
                                .OrderBy(x => rnd.Next())
                                .ToList();

            var result = BucketSort.Sort(randomNumbers.ToArray(), 11);

            for (int i = 1; i <= nodeCount; i++)
            {
                Assert.AreEqual(i, result[i - 1]);
            }
        }
        public void Test(float[] data)
        {
            // Arrange
            var data2 = new float[data.Length];

            Array.Copy(data, data2, data.Length);
            Array.Sort(data2);
            var sorter = new BucketSort();

            // Act
            sorter.Sort(data);

            // Assert
            CollectionAssert.AreEqual(data, data2);
        }
        public void BucketSort_Descending_Stress_Test()
        {
            var rnd           = new Random();
            var nodeCount     = 1000;
            var randomNumbers = Enumerable.Range(1, nodeCount)
                                .OrderBy(x => rnd.Next())
                                .ToList();

            var result = BucketSort.Sort(randomNumbers.ToArray(), 4, SortDirection.Descending);

            for (int i = 0; i < nodeCount; i++)
            {
                Assert.AreEqual(randomNumbers.Count - i, result[i]);
            }
        }
Ejemplo n.º 16
0
        public void SortTest()
        {
            List <int> items = new List <int> {
                1, 5, 3, 9, 5, 6, 8, 7, 2, 8
            };
            List <int> expectedItems = new List <int> {
                1, 2, 3, 5, 5, 6, 7, 8, 8, 9
            };

            int numBuckets = 10;

            items = (List <int>)BucketSort.Sort(items, numBuckets);

            CollectionAssert.AreEqual(expectedItems, items);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Checks if a given graph is planar and provides a planar embedding if so.
        /// </summary>
        /// <param name="graph">Graph</param>
        /// <param name="embedding">Planar embedding if a given graph is planar, null otherwise</param>
        /// <returns>True if planar, false otherwise</returns>
        public bool IsPlanar(IGraph <T> graph, out PlanarEmbedding <T> embedding)
        {
            // Transforms input graph
            TransformGraph(graph);

            // Init helper collections
            selfLoopsNew  = new List <IEdge <Vertex <T> > >();
            mergeStackNew = new Stack <MergeInfo>();

            // Use DFS traversal to add basic information to each of the vertices
            var visitor = new DFSTraversalVisitor <T>();

            DFSTraversal.Traverse(transformedGraph, visitor);

            // Sort vertices by dfs number ASC
            verticesByDFSNumberNew = BucketSort.Sort(transformedGraph.Vertices, x => x.DFSNumber, transformedGraph.VerticesCount);

            // Sort vertices by low point ASC
            verticesByLowPointNew = BucketSort.Sort(transformedGraph.Vertices, x => x.LowPoint, transformedGraph.VerticesCount);

            // Init vertex fields
            foreach (var vertex in transformedGraph.Vertices)
            {
                vertex.BackEdges    = new List <IEdge <Vertex <T> > >();
                vertex.Visited      = int.MaxValue;
                vertex.BackedgeFlag = transformedGraph.VerticesCount + 1;
                vertex.Flipped      = false;

                var dfsParent = vertex.Parent;

                if (vertex != dfsParent)
                {
                    var parentEdge = vertex.DFSEdge;
                    vertex.FaceHandle     = new FaceHandle <Vertex <T> >(vertex, parentEdge);
                    vertex.DFSChildHandle = new FaceHandle <Vertex <T> >(dfsParent, parentEdge);
                }
                else
                {
                    vertex.FaceHandle     = new FaceHandle <Vertex <T> >(vertex, (Vertex <T>)null);             // TODO: change
                    vertex.DFSChildHandle = new FaceHandle <Vertex <T> >(dfsParent, (Vertex <T>)null);
                }

                vertex.CanonicalDFSChild     = vertex;
                vertex.PertinentRoots        = new LinkedList <FaceHandle <Vertex <T> > >();
                vertex.SeparatedDFSChildList = new LinkedList <Vertex <T> >();
            }

            // Init separated dfs child lists
            //
            // Original Boost comment:
            // We need to create a list of not-yet-merged depth-first children for
            // each vertex that will be updated as bicomps get merged. We sort each
            // list by ascending lowpoint, which allows the externally_active
            // function to run in constant time, and we keep a pointer to each
            // vertex's representation in its parent's list, which allows merging
            //in constant time.
            foreach (var vertex in verticesByLowPointNew)
            {
                var dfsParent = vertex.Parent;

                if (vertex != dfsParent)
                {
                    var node = dfsParent.SeparatedDFSChildList.AddLast(vertex);
                    vertex.SeparatedNodeInParentList = node;
                }
            }

            // Call the main algorithm
            var isPlanar = IsPlanar();

            if (!isPlanar)
            {
                embedding = null;
                return(false);
            }

            embedding = GetPlanarEmbedding();
            return(true);
        }
Ejemplo n.º 18
0
        public void Run(IInputSample <int>[] items)
        {
            foreach (var item in items)
            {
                if (item.Samples != null)
                {
                    Init(item.Samples);

                    // -- Exchange -- //

                    // Bubble Sort
                    RunSort(new BubbleSort <int>(), item);

                    // OddEven Sort
                    RunSort(new OddEvenSort <int>(), item);

                    // Cocktail Shaker Sort
                    RunSort(new CocktailShakerSort <int>(), item);
                    RunSort(new CocktailShakerSort2 <int>(), item);

                    // Comb Sort
                    RunSort(new CombSort <int>(), item);

                    // Cycle Sort
                    RunSort(new CycleSort <int>(), item);

                    // Stooge Sort
                    RunSort(new StoogeSort <int>(), item);

                    // Too slow....
                    if (item.Samples.Length < 100)
                    {
                        // Bogo Sort
                        RunSort(new BogoSort <int>(), item);
                    }

                    if (item.Samples.Length < 1000)
                    {
                        // Slow Sort
                        RunSort(new SlowSort <int>(), item);
                    }

                    // Gnome Sort
                    RunSort(new GnomeSort <int>(), item);
                    RunSort(new GnomeSort1 <int>(), item);
                    RunSort(new GnomeSort2 <int>(), item);
                    RunSort(new GnomeSort3 <int>(), item);

                    // -- Selection -- //

                    // Selection Sort
                    RunSort(new SelectionSort <int>(), item);

                    // Heap Sort
                    RunSort(new HeapSort <int>(), item);

                    // Smooth Sort
                    RunSort(new SmoothSort <int>(), item);

                    // -- Insertion -- //

                    // Insert Sort
                    RunSort(new InsertSort <int>(), item);

                    // Binary Insert Sort
                    RunSort(new BinaryInsertSort <int>(), item);

                    // Shell Sort
                    RunSort(new ShellSort <int>(), item);

                    // Binary Tree Sort
                    RunSort(new BinaryTreeSort <int>(), item);

                    // -- Partitionig -- //

                    // Quick Sort Median3
                    RunSort(new QuickSortMedian3 <int>(), item);

                    // Quick Sort Median9
                    RunSort(new QuickSortMedian9 <int>(), item);

                    // Dual Pivot QuickSort
                    RunSort(new QuickDualPivotSort <int>(), item);

                    // QuickSort Median3 (Quick + Insert)
                    RunSort(new QuickSortMedian3Insert <int>(), item);

                    // QuickSort Median9 (Quick + Insert)
                    RunSort(new QuickSortMedian9Insert <int>(), item);

                    // Dual Pivot Quick Sort (Quick + Insert)
                    RunSort(new QuickDualPivotSortInsert <int>(), item);

                    // QuickSort Median3 (Quick + BinaryInsert)
                    RunSort(new QuickSortMedian3BinaryInsert <int>(), item);

                    // QuickSort Median9 (Quick + BinaryInsert)
                    RunSort(new QuickSortMedian9BinaryInsert <int>(), item);

                    // Dual Pivot Quick Sort (Quick + BinaryInsert)
                    RunSort(new QuickDualPivotSortBinaryInsert <int>(), item);

                    // -- Merge -- //

                    // Merge Sort
                    RunSort(new MergeSort <int>(), item);
                    RunSort(new MergeSort2 <int>(), item);

                    // Shift Sort
                    RunSort(new ShiftSort <int>(), item);

                    // DropMerge Sort
                    RunSort(new DropMergeSort <int>(), item);

                    // -- Distribution -- //

                    // Bucket Sort
                    var bucketSort = new BucketSort <int>();
                    RunSort(bucketSort, array => bucketSort.Sort(array), item);

                    // Radix Sort(LSD)
                    var radix10Sort = new RadixLSD10Sort <int>();
                    RunSort(radix10Sort, array => radix10Sort.Sort(array), item);

                    var radix4Sort = new RadixLSD4Sort <int>();
                    RunSort(radix4Sort, array => radix4Sort.Sort(array), item);

                    // Counting Sort
                    var countingSort = new CountingSort <int>();
                    RunSort(countingSort, array => countingSort.Sort(array), item);

                    // -- Hybrid -- //

                    // IntroSort Median3 (Quick + Heap + Insert)
                    //RunSort(new IntroSortMedian3<int>(), item);

                    // IntroSort Median9 (Quick + Heap + Insert)
                    RunSort(new IntroSortMedian9 <int>(), item);

                    // IntroSort Median9 (Insert + Merge)
                    //RunSort(new TimSort<int>(), item);

                    // -- Other -- //

                    // Pancake Sort
                    RunSort(new PancakeSort <int>(), item);
                }

                // BucketSort<T>
                if (item.Samples == null && item.DictionarySamples != null)
                {
                    Init(item.DictionarySamples);

                    // -- Distribution -- //

                    RunBucketTSort(new BucketSortT <KeyValuePair <int, string> >(), x => x.Key, item.DictionarySamples.Max(x => x.Key), item);
                }
            }
        }