Пример #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]));
                    }
                }
            }
        }
Пример #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 }));
        }
Пример #3
0
        public BucketSortTests()
        {
            var sort = new BucketSort <int>();

            func      = array => sort.Sort(array);
            this.sort = sort;
            algorithm = nameof(BucketSort <int>);
        }
Пример #4
0
 public static void Main(string[] args)
 {
     int[] array = new int[] { 23, 24, 22, 21, 26, 25, 27, 28, 21, 21 };
     BucketSort.sort(array, 20, 30);
     for (int i = 0; i < array.Length; i++)
     {
         Console.Write(array[i] + " ");
     }
 }
        public void BucketSort_Smoke_Test()
        {
            var result = BucketSort.Sort(TestArray, 4);

            for (int i = 0; i < TestArray.Length; i++)
            {
                Assert.AreEqual(i, result[i]);
            }
        }
        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]);
            }
        }
Пример #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);
        }
Пример #9
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));
        }
Пример #10
0
        public void SortWithEmptyArray()
        {
            float[]    arr        = new float[] {};
            float[]    resultArr  = new float[] {};
            BucketSort bucketSort = new BucketSort();

            bucketSort.Sort(arr);

            Assert.Equal(resultArr, arr);
        }
        public void SortsInAscendingOrderTest()
        {
            int[] array       = new int[] { 5, 4, 3, 2, 1 };
            var   sortedArray = new BucketSort().Sort(array);

            Assert.AreEqual(sortedArray.Length, array.Length);
            for (int i = 0; i < array.Length - 1; i++)
            {
                Assert.IsTrue(sortedArray[i].CompareTo(sortedArray[i + 1]) <= 0);
            }
        }
Пример #12
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));
     }
 }
Пример #13
0
        static void Main(string[] args)
        {
            MyDatabase db      = new MyDatabase();
            var        shirts  = db.Shirts;
            var        shirts2 = db.Shirts;
            var        shirts3 = db.Shirts;
            var        shirts4 = db.Shirts;
            var        shirts5 = db.Shirts;
            var        shirts6 = db.Shirts;
            var        shirts7 = db.Shirts;
            var        shirts8 = db.Shirts;

            PrintAllItems(shirts);

            BubbleSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Size > shirt2.Size); //Size in ascending
            PrintAllItems(shirts);

            BubbleSort.SortAll(shirts2, (shirt1, shirt2) => shirt1.Size < shirt2.Size); //Size in descending
            PrintAllItems(shirts2);

            BubbleSort.SortAll(shirts3, (shirt1, shirt2) => shirt1.Color > shirt2.Color); //Color in ascending
            PrintAllItems(shirts3);

            BubbleSort.SortAll(shirts4, (shirt1, shirt2) => shirt1.Color < shirt2.Color); //Color in descending
            PrintAllItems(shirts4);
            //BubbleSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Fabric > shirt2.Fabric); //Fabric in ascending
            //BubbleSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Fabric < shirt2.Fabric); //Fabric in descending
            //BubbleSort.SizeColorFabricAsc(shirts); //Size, color and fabric in ascending
            //BubbleSort.SizeColorFabricDesc(shirts); //Size, color and fabric in descending


            //QuickSort.SortFacade(shirts, (shirt1, shirt2) => shirt1.Size > shirt2.Size); //Size in ascending
            //QuickSort.SortFacade(shirts, (shirt1, shirt2) => shirt1.Size < shirt2.Size); //Size in descending
            //QuickSort.SortFacade(shirts, (shirt1, shirt2) => shirt1.Color > shirt2.Color); //Color in ascending
            //QuickSort.SortFacade(shirts, (shirt1, shirt2) => shirt1.Color < shirt2.Color); //Color in descending
            QuickSort.SortFacade(shirts5, (shirt1, shirt2) => shirt1.Fabric > shirt2.Fabric); //Fabric in ascending
            PrintAllItems(shirts5);
            QuickSort.SortFacade(shirts6, (shirt1, shirt2) => shirt1.Fabric < shirt2.Fabric); //Fabric in descending
            PrintAllItems(shirts6);
            //QuickSort.SizeColorFabricAsc(shirts); //Size, color and fabric in ascending
            //QuickSort.SizeColorFabricDesc(shirts); //Size, color and fabric in descending

            //BucketSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Size > shirt2.Size, 1); //Size in ascending
            //BucketSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Size < shirt2.Size, 4); //Size in descending
            //BucketSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Color > shirt2.Color, 2); //Color in ascending
            //BucketSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Color < shirt2.Color, 5); //Color in descending
            //BucketSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Fabric > shirt2.Fabric, 3); //Fabric in ascending
            //BucketSort.SortAll(shirts, (shirt1, shirt2) => shirt1.Fabric < shirt2.Fabric, 6); //Fabric in descending
            BucketSort.SizeColorFabricAsc(shirts7); //Size, color and fabric in ascending
            PrintAllItems(shirts7);
            BucketSort.SizeColorFabricDesc(shirts); //Size, color and fabric in descending
            PrintAllItems(shirts8);
        }
Пример #14
0
        protected bool SelectFromMenu()
        {
            Check check = new Check();

            int input = Check.InsertAndCheckNumber(9);

            switch (input)
            {
            case 1:
                BubbleSort.ExecuteBubbleSort(Database.shirts, (x, y) => x.size > y.size);
                return(false);

            case 2:
                BubbleSort.ExecuteBubbleSort(Database.shirts, (x, y) => x.size < y.size);
                return(false);

            case 3:
                QuickSort.ExecuteQuickSort(Database.shirts, 0, Database.shirts.Count - 1, (x, y) => x.color > y.color);
                return(false);

            case 4:
                QuickSort.ExecuteQuickSort(Database.shirts, 0, Database.shirts.Count - 1, (x, y) => x.color < y.color);
                return(false);

            case 5:
                InsertionSort.ExecuteInsertionSort(Database.shirts, (x, y) => x.fabric > y.fabric);
                return(false);

            case 6:
                InsertionSort.ExecuteInsertionSort(Database.shirts, (x, y) => x.fabric < y.fabric);
                return(false);

            case 7:
                BucketSort.ExecuteBucketSort(Database.shirts, (x, y) => x.size > y.size, (x, y) => x.color > y.color, (x, y) => x.fabric > y.fabric);
                return(false);

            case 8:
                BucketSort.ExecuteBucketSort(Database.shirts, (x, y) => x.size < y.size, (x, y) => x.color < y.color, (x, y) => x.fabric < y.fabric);
                return(false);

            case 9:
                decimal amount = Shirt.CalculatePriceOfTShirts(Database.shirts);
                Shirt.DisplayThePriceOfTshirts(amount);
                PaymetnMethod payment = SelectPaymentMethod(AskForPaymentMethod());
                payment.DisplayTransactionApprouved(payment.Pay(amount));
                return(true);

            default:
                BubbleSort.ExecuteBubbleSort(Database.shirts, (x, y) => x.size > y.size);
                return(false);
            }
        }
Пример #15
0
        public List <TShirt> Sort(SortingEnum sortingMethod, List <TShirt> tshirts)
        {
            switch ((int)sortingMethod)
            {
            case 1:
                var tshirtsArr = tshirts.ToArray();
                QuickSort.QuickSortMain(tshirtsArr, 0, tshirtsArr.Length - 1, true, "price");
                var sorted = tshirtsArr.ToList();
                return(sorted);

            case 2:
                tshirtsArr = tshirts.ToArray();
                QuickSort.QuickSortMain(tshirtsArr, 0, tshirtsArr.Length - 1, false, "price");
                sorted = tshirtsArr.ToList();
                return(sorted);

            case 3:
                return(BubbleSort.Ascending(tshirts));

            case 4:
                return(BubbleSort.Descending(tshirts));

            case 5:
                tshirtsArr = tshirts.ToArray();
                QuickSort.QuickSortMain(tshirtsArr, 0, tshirtsArr.Length - 1, true, "size");
                sorted = tshirtsArr.ToList();
                return(sorted);

            case 6:
                tshirtsArr = tshirts.ToArray();
                QuickSort.QuickSortMain(tshirtsArr, 0, tshirtsArr.Length - 1, false, "size");
                sorted = tshirtsArr.ToList();
                return(sorted);

            case 7:
                return(BucketSort.BucketFabricSort(tshirts, true));

            case 8:
                return(BucketSort.BucketFabricSort(tshirts, false));

            case 9:
                return(BubbleSort.AllAsc(tshirts));

            case 10:
                return(BubbleSort.AllDesc(tshirts));

            default:
                return(tshirts);
            }
        }
Пример #16
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 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]);
            }
        }
        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);
        }
Пример #20
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);
        }
 //Bucket Sort best: O(n+m) avg: Θ(n+m) worst:O(n2)
 //Where N is numbers and M is buckets
 //TIME = O(n), if m<n if bucket count is smaller than number count
 //Memory O(N)- buckets increase with N Or The space complexity for Bucket Sort is O(n+k) if buckets +numbers are added as sublist of a bucket
 public void ExecuteBucketSort(int[] array)
 {
     BucketSort.Execute(array);
 }
Пример #22
0
        public static void Print()
        {
            //-------------- ALL Synthetic Data -----------------------
            MyDatabase db = new MyDatabase();

            PrintAllItems(db.Shirts);

            //========================= QUICK SORT =========================

            //-------------- ALL Synthetic Data Size ASC
            QuickShort.SizeAsc(db.Shirts, 0, db.Shirts.Count() - 1);
            PrintAllItems(db.Shirts);

            //-------------- ALL Synthetic Data Size DESC
            QuickShort.SizeDesc(db.Shirts, 0, db.Shirts.Count() - 1);
            PrintAllItems(db.Shirts);

            //========================= BUCKET SORT =========================

            //-------------- ALL Synthetic Data Color ASC
            BucketSort.ColorAsc(db.Shirts);
            PrintAllItems(db.Shirts);

            //-------------- ALL Synthetic Data Color DESC
            BucketSort.ColorDesc(db.Shirts);
            PrintAllItems(db.Shirts);

            //========================= BUBBLE SORT =========================

            //-------------- ALL Synthetic Data Fabric ASC
            //BubbleSort.FabricAsc(db.Shirts);
            //PrintAllItems(db.Shirts);

            //-------------- ALL Synthetic Data Fabric DESC
            //BubbleSort.FabricDesc(db.Shirts);
            //PrintAllItems(db.Shirts);

            //-------------- ALL Synthetic Data Fabric ASC and DESC with Delegate
            // creating object of class with name bubble
            BubbleSort bubble = new BubbleSort();

            // creating delegate object, name as "bubbleDele" and pass the 1st method "FabricAsc"
            //as parameter by class object "bubble"
            BubbleSort.bubbleSortDelegate bubbleDele = bubble.FabricAsc;

            // call 2nd method "FabricDesc" Multicasting
            bubbleDele += bubble.FabricDesc;
            // pass the values in two method by using "Invoke" method
            bubbleDele.Invoke(db.Shirts);

            PrintAllItems(db.Shirts);



            //-------------- ALL Synthetic Data Size and Color and Fabric ASC
            BubbleSort.SizeColorFabricAsc(db.Shirts);
            PrintAllItems(db.Shirts);
            //-------------- ALL Synthetic Data Size and Color and Fabric DESC
            BubbleSort.SizeColorFabricDesc(db.Shirts);
            PrintAllItems(db.Shirts);
        }
Пример #23
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);
        }
Пример #24
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;

            Console.WriteLine("Please choose your shorting method :");
            Console.WriteLine();
            Console.WriteLine("<<1>>:BubbleShort based on Size In Ascending");
            Console.WriteLine();
            Console.WriteLine("<<2>>:BubbleShort based on Size in Descending");
            Console.WriteLine();
            Console.WriteLine("<<3>>:BubbleShort based on Color in Ascending");
            Console.WriteLine();
            Console.WriteLine("<<4>>:BubbleShort based on Color in Descending");
            Console.WriteLine();
            Console.WriteLine("<<5>>:BubbleShort based on Fabric in Ascending");
            Console.WriteLine();
            Console.WriteLine("<<6>>:BubbleShort based on Fabric in Descending");
            Console.WriteLine();
            Console.WriteLine("<<7>>:QuickSort based on Size in Ascending");
            Console.WriteLine();
            Console.WriteLine("<<8>>:QuickSort based on Size in Descending");
            Console.WriteLine();
            Console.WriteLine("<<9>>:BucketSort based on Color in Ascending");
            Console.WriteLine();
            Console.WriteLine("<<10>>:BucketSort based on Size in Ascending");
            Console.WriteLine();
            Console.WriteLine("<<11>>):BucketSort based on Fabric in Ascending");
            Console.WriteLine();
            Console.WriteLine("<<12>>:BucketSort based on Size in Descending");
            Console.WriteLine();
            Console.WriteLine("<<13>>:List Sorted with BulletSort based on Size, Color and Fabric in Ascending");
            Console.WriteLine();
            Console.WriteLine("<<14>>:List Sorted with BulletSort based on Size, Color and Fabric in Descending");
            Console.ResetColor();
            Console.WriteLine("Enter a number from above");

            int number = Convert.ToInt32(Console.ReadLine());

            switch (number)
            {
            case 1:
                MyDatabase db       = new MyDatabase();
                Stopwatch  watch    = new Stopwatch();
                var        newLista = new List <Shirt>(db.Shirts);
                Console.WriteLine("List sorted with bubbleShort based on Size In Ascending");
                BubbleSort.SortShirtsAsc(newLista);
                foreach (var item in newLista)
                {
                    item.Output();
                }
                watch.Restart();
                break;

            case 2:
                MyDatabase db1       = new MyDatabase();
                Stopwatch  watch1    = new Stopwatch();
                var        newLista1 = new List <Shirt>(db1.Shirts);
                Console.WriteLine("List sorted with bubbleShort based on Size in Descending");
                BubbleSort.SortShirtsDesc(newLista1);
                foreach (var item in newLista1)
                {
                    item.Output();
                }
                break;

            case 3:
                MyDatabase db2       = new MyDatabase();
                Stopwatch  watch2    = new Stopwatch();
                var        newLista2 = new List <Shirt>(db2.Shirts);

                BubbleSort.SortShirtsclAsc(newLista2);
                Console.WriteLine("List sorted with bubbleShort based on Color in Ascending");
                foreach (var item in newLista2)
                {
                    item.Output();
                }
                break;

            case 4:
                MyDatabase db3       = new MyDatabase();
                Stopwatch  watch3    = new Stopwatch();
                var        newLista3 = new List <Shirt>(db3.Shirts);

                BubbleSort.SortShirtsclDesc(newLista3);
                Console.WriteLine("List sorted with bubbleShort based on Color in Descending");
                foreach (var item in newLista3)
                {
                    item.Output();
                }
                break;

            case 5:
                MyDatabase db4       = new MyDatabase();
                Stopwatch  watch4    = new Stopwatch();
                var        newLista4 = new List <Shirt>(db4.Shirts);
                BubbleSort.SortShirtsfbAsc(newLista4);
                Console.WriteLine("List sorted with bubbleShort based on Fabric in Ascending");
                foreach (var item in newLista4)
                {
                    item.Output();
                }

                break;

            case 6:
                MyDatabase db5       = new MyDatabase();
                Stopwatch  watch5    = new Stopwatch();
                var        newLista5 = new List <Shirt>(db5.Shirts);
                BubbleSort.SortShirtsfbDesc(newLista5);
                Console.WriteLine("List sorted with bubbleShort based on Fabric in Descending");
                foreach (var item in newLista5)
                {
                    item.Output();
                }
                break;

            case 7:
                MyDatabase db6       = new MyDatabase();
                Stopwatch  watch6    = new Stopwatch();
                var        newLista6 = new List <Shirt>(db6.Shirts);
                QuickSort.SortShirtsAscS(newLista6);
                Console.WriteLine("List sorted with QuickSort based on Size in Ascending");
                foreach (var item in newLista6)
                {
                    item.Output();
                }
                break;

            case 8:
                MyDatabase db7       = new MyDatabase();
                Stopwatch  watch7    = new Stopwatch();
                var        newLista7 = new List <Shirt>(db7.Shirts);
                QuickSort.SortShirtsDescscS(newLista7);
                Console.WriteLine("List sorted with QuickSort based on Size in Desscending");
                foreach (var item in newLista7)
                {
                    item.Output();
                }
                break;

            case 9:
                MyDatabase db8              = new MyDatabase();
                Stopwatch  watch8           = new Stopwatch();
                var        newLista8        = new List <Shirt>(db8.Shirts);
                var        sortedListBucket = BucketSort.InsertionSortBucketColor(newLista8);
                Console.WriteLine("List Sorted with Bucket short based on Color in Ascending!!!");
                foreach (var item in sortedListBucket)
                {
                    item.Output();
                }
                break;

            case 10:
                MyDatabase db9                  = new MyDatabase();
                Stopwatch  watch9               = new Stopwatch();
                var        newLista9            = new List <Shirt>(db9.Shirts);
                var        sortedListBucketSize = BucketSort.InsertionSortBucketSize(newLista9);
                Console.WriteLine("List Sorted with Bucket short based on Size in Ascending");
                foreach (var item in sortedListBucketSize)
                {
                    item.Output();
                }
                break;

            case 11:
                MyDatabase db10                   = new MyDatabase();
                Stopwatch  watch10                = new Stopwatch();
                var        newLista10             = new List <Shirt>(db10.Shirts);
                var        sortedListBucketFabric = BucketSort.InsertionSortBucketFabric(newLista10);
                Console.WriteLine("List Sorted with Bucket short based on Fabric in Ascending");
                foreach (var item in sortedListBucketFabric)
                {
                    item.Output();
                }
                break;

            case 12:
                MyDatabase db11       = new MyDatabase();
                Stopwatch  watch11    = new Stopwatch();
                var        newLista11 = new List <Shirt>(db11.Shirts);
                var        sortedListBucketFabricdesc = BucketSort.InsertionSortBucketSizeDesc(newLista11);
                Console.WriteLine("List Sorted with Bucket short based on Size in Descending");
                foreach (var item in sortedListBucketFabricdesc)
                {
                    item.Output();
                }
                break;

            case 13:
                MyDatabase db12       = new MyDatabase();
                Stopwatch  watch12    = new Stopwatch();
                var        newLista12 = new List <Shirt>(db12.Shirts);
                BubbleSort.SortShirtsAscLast(newLista12);
                Console.WriteLine("List Sorted with BulletSort based on Size, Color and Fabric in Ascending");
                foreach (var item in newLista12)
                {
                    item.Output();
                }
                break;

            case 14:
                MyDatabase db13       = new MyDatabase();
                Stopwatch  watch13    = new Stopwatch();
                var        newLista13 = new List <Shirt>(db13.Shirts);
                BubbleSort.SortShirtsAscLastDesc(newLista13);
                Console.WriteLine("List Sorted with BulletSort based on Size, Color and Fabric in Descending");
                foreach (var item in newLista13)
                {
                    item.Output();
                }
                break;
            }


            List <Shirt> shirts = new List <Shirt>();


            Console.WriteLine("Choose a number from 1...7 for your color:");
            Console.WriteLine("1: BLUE");
            Console.WriteLine("2: GREEN");
            Console.WriteLine("3: INDIGO");
            Console.WriteLine("4: ORANGE");
            Console.WriteLine("5: RED");
            Console.WriteLine("6: VIOLET");
            Console.WriteLine("7: YELLOW");

            int Color = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Choose a number from 1...7 for your Fabric:");
            Console.WriteLine("1: CASHMERE");
            Console.WriteLine("2: COTTON");
            Console.WriteLine("3: LINEN");
            Console.WriteLine("4: POLYESTER");
            Console.WriteLine("5: RAYON");
            Console.WriteLine("6: SILK");
            Console.WriteLine("7: WOOL");

            int Fabric = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("Choose a number from 1...6 for your Size:");
            Console.WriteLine("1: XS");
            Console.WriteLine("2: S");
            Console.WriteLine("3: M");
            Console.WriteLine("4: L");
            Console.WriteLine("5: XXL");
            Console.WriteLine("6: XXXL");

            int Size = Convert.ToInt32(Console.ReadLine());



            Console.WriteLine("You picked a Tshirt with color number Color.No:{0} , Fabric.No:{1} and Size.No:{2}", Color, Fabric, Size);

            Shirt sh = new Shirt();

            Console.WriteLine(sh.CalculatePrice(Fabric));


            Console.WriteLine();
            Console.WriteLine("Please Choose the payment method.Type:");
            Console.WriteLine("1: Bank transfer");
            Console.WriteLine("2: Debit Card");
            Console.WriteLine("Type anything else to pay with cash");

            int num = Convert.ToInt32(Console.ReadLine());

            StrategyPayment strategycontext = new StrategyPayment(num);

            IpaymentStrategy strategy = strategycontext.GetpaymentMethod(num);
        }
Пример #25
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);
                }
            }
        }
Пример #26
0
        static void Main(string[] args)
        {
            Database db     = new Database();
            var      shirts = db.Shirts.ToArray();

            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("BubbleSort");
            Console.WriteLine("~~~~~~~~~~~");

            Console.WriteLine("Ascending order by size, color and fabric");
            BubbleSort.SizeColorFabricAscendingOrder(shirts);
            PrintAllItems(shirts);

            Console.WriteLine("Descending order by size, color and fabric");
            BubbleSort.SizeColorFabricDescendingOrder(shirts);
            PrintAllItems(shirts);

            Console.WriteLine("Ascending order by fabric");
            BubbleSort.FabricAscendingOrder(shirts);
            PrintAllItems(shirts);

            Console.WriteLine("Descending order by fabric");
            BubbleSort.FabricDescendingOrder(shirts);
            PrintAllItems(shirts);

            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("QuickSort");
            Console.WriteLine("~~~~~~~~~~~");

            Console.WriteLine("Ascending order by color");
            QuickSort.ColorAscending(shirts);
            PrintAllItems(shirts);

            Console.WriteLine("Descending order by color");
            QuickSort.ColorDescending(shirts);
            PrintAllItems(shirts);

            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("BucketSort");
            Console.WriteLine("~~~~~~~~~~~");

            Console.WriteLine("Ascending order by size");
            var listSizeAsc = BucketSort.SizeAscending(shirts);

            PrintAllItems(listSizeAsc.ToArray());

            Console.WriteLine("Descending order by size");
            var listSizeDesc = BucketSort.SizeDescending(shirts);

            PrintAllItems(listSizeDesc.ToArray());



            UserApplication.BuyShirt();

            Console.ReadKey();
        }
Пример #27
0
        public void scenarioOne()
        {
            int[] problemSizes = { 1024, 5120, 25600, 128000 };;
            //integers 1 to N shuffled using the Fisher-Yates algorthm
            for (int i = 0; i < problemSizes.Length; i++)
            {
                int[] array = generateArray(problemSizes[i], false);
                Console.WriteLine("Problem Size " + problemSizes[i] + " : ");
                FisherYatesShuffle fShuffle = new FisherYatesShuffle();
                array = fShuffle.Shuffle(array);
                Stopwatch stopWatch = new Stopwatch();

                //mergeSort testing
                MergeSort <int> mergeS = new MergeSort <int>(array);

                long[] runSpeeds    = new long[100];
                long   runSpeedsSum = 0;
                for (int a = 0; a < runSpeeds.Length; a++)
                {
                    stopWatch.Start();

                    var arraySorted = mergeS.sort();

                    stopWatch.Stop();
                    runSpeeds[i]  = stopWatch.ElapsedTicks;
                    runSpeedsSum += runSpeeds[i];
                }

                //Average
                Console.WriteLine("Merge Sort (Average): " + runSpeedsSum / runSpeeds.Length);
                //heapSort testing
                stopWatch = new Stopwatch();

                //mergeSort testing
                Heap <int> Heap = new Heap <int>(array);
                runSpeeds    = new long[100];
                runSpeedsSum = 0;
                for (int a = 0; a < runSpeeds.Length; a++)
                {
                    stopWatch.Start();
                    var arraySorted = Heap.sortHeap();
                    stopWatch.Stop();
                    runSpeeds[i]  = stopWatch.ElapsedTicks;
                    runSpeedsSum += runSpeeds[i];
                }



                Console.WriteLine("Heap Sort (Average): " + runSpeedsSum / runSpeeds.Length);
                //bucket Sort
                BucketSort <int> bucketSort = new BucketSort <int>(array);
                runSpeeds    = new long[100];
                runSpeedsSum = 0;
                for (int a = 0; a < runSpeeds.Length; a++)
                {
                    stopWatch.Start();
                    var arraySorted = bucketSort.bucketSort();
                    stopWatch.Stop();
                    runSpeeds[i]  = stopWatch.ElapsedTicks;
                    runSpeedsSum += runSpeeds[i];
                }

                Console.WriteLine("Bucket Sort (Average): " + runSpeedsSum / runSpeeds.Length);
            }
        }
Пример #28
0
 public void Test_BucketSort()
 {
     BucketSort.SortIterative(arr);
     //HeapSort.SortRecursive(arr, arr.Length);
 }
 //This will show how the num list runs on each algorithm
 //This allows the user to compare them
 public void hitSpeedCompButton()
 {
     algView = !algView;            //Change whether you are looking at the algorithm or speed comparison
     if (algView)                   //If you are now viewing at the algorithm pseudocode
     {
         algText.text = oldAlgText; //Change the text back to the pseudocode
         if (!half)
         {
             algDescription.enabled = true;
         }
         speedCompText.text = "Speed Comparison";
     }
     else//Otherwise display the speed comparison
     {
         oldAlgText = algText.text;
         string storageText = "Bubble Sort:{0} μs\nCocktail Sort: {1} μs\nComb Sort: {2} μs\nHeap Sort: {3}" +
                              " μs\nInsertion Sort: {4} μs\nMerge Sort:{5} μs\nQuick Sort:{6} μs\nShell Sort:{7} μs\nFlash Sort:{8} μs" +
                              "\nBucket Sort:{9} μs\nRadix Sort:{10} μs";
         //Format the text to show each element in microseconds
         algText.text = string.Format(storageText, BubbleSort.BubbleSortTime(new List <int>(copy)),
                                      CocktailSort.CocktailSortTime(new List <int>(copy)), CombSort.CombSortTime(new List <int>(copy)),
                                      HeapSort.HeapSortStartTime(new List <int>(copy)), InsertionSort.InsertionSortTime(new List <int>(copy)),
                                      Mergesort.MergeSortTime(new List <int>(copy), 0, copy.Count - 1), QuickSort.QuickSortStartTime(new List <int>(copy), 0, copy.Count - 1),
                                      ShellSort.ShellSortTime(new List <int>(copy)), FlashSort.FlashSortTime(new List <int>(copy)), BucketSort.BucketSortTime(new List <int>(copy), 34)
                                      , RadixSort.RadixSortTime(new List <int>(copy)));
         algDescription.enabled = false;
         speedCompText.text     = "Algorithm";
     }
 }
        public static void Run()
        {
            MyDatabase db = new MyDatabase();


            var bubbleShirts = db.Shirts;
            var quickshirts  = db.Shirts.ToArray();
            var bucketShirts = db.Shirts.ToArray();

            //// ---------------------------------------------------------------------------------------------- BUBBLE SORT-------------------------------------------------------------------


            Console.WriteLine("---BUBBLE SORTING---");


            BubbleSort.Sort(bubbleShirts, (s1, s2) => s1.Size > s2.Size);  //Sorting Asceding by the Size.
            PrintAllItems(bubbleShirts, Element.Size, Order.Asceding);

            BubbleSort.Sort(bubbleShirts, (s1, s2) => s1.Color > s2.Color);  //Sorting Asceding by the Color.
            PrintAllItems(bubbleShirts, Element.Color, Order.Asceding);

            BubbleSort.Sort(bubbleShirts, (s1, s2) => s1.Fabric > s2.Fabric);  //Sorting Asceding by the Fabric.
            PrintAllItems(bubbleShirts, Element.Fabric, Order.Asceding);



            BubbleSort.Sort(bubbleShirts, (s1, s2) => s1.Size < s2.Size);  //Sorting Desceding by the Size.
            PrintAllItems(bubbleShirts, Element.Size, Order.Desceding);

            BubbleSort.Sort(bubbleShirts, (s1, s2) => s1.Color < s2.Color);  //Sorting Desceding by the Color.
            PrintAllItems(bubbleShirts, Element.Color, Order.Desceding);

            BubbleSort.Sort(bubbleShirts, (s1, s2) => s1.Fabric < s2.Fabric);  //Sorting Desceding by the Fabric.
            PrintAllItems(bubbleShirts, Element.Fabric, Order.Desceding);



            BubbleSort.SizeColorFabricAsc(bubbleShirts);  // Facade tha gives (ASC) ordered  Shirts By ---  Size,Color,Fabric ---
            PrintAllItems(bubbleShirts, Element.Everything, Order.Asceding);

            BubbleSort.SizeColorFabricDesc(bubbleShirts);  // Facade tha gives (DESc) ordered Shirts By ---  Size,Color,Fabric ---
            PrintAllItems(bubbleShirts, Element.Everything, Order.Desceding);



            //// ---------------------------------------------------------------------------------------------- QUICK SORT-------------------------------------------------------------------



            Console.WriteLine("---QUICK SORTING---");


            QuickSort.SortingFacade(quickshirts, (s1, s2) => s1.Size > s2.Size);  //Sorting Asceding by the Size.
            PrintAllItems(quickshirts, Element.Size, Order.Asceding);

            QuickSort.SortingFacade(quickshirts, (s1, s2) => s1.Color > s2.Color);   //Sorting Asceding by the Color.
            PrintAllItems(quickshirts, Element.Color, Order.Asceding);

            QuickSort.SortingFacade(quickshirts, (s1, s2) => s1.Fabric > s2.Fabric);   //Sorting Asceding by the Fabric.
            PrintAllItems(quickshirts, Element.Fabric, Order.Asceding);



            QuickSort.SortingFacade(quickshirts, (s1, s2) => s1.Size < s2.Size);  //Sorting Desceding by the Size.
            PrintAllItems(quickshirts, Element.Size, Order.Desceding);

            QuickSort.SortingFacade(quickshirts, (s1, s2) => s1.Color < s2.Color);   //Sorting Desceding by the Color.
            PrintAllItems(quickshirts, Element.Color, Order.Desceding);

            QuickSort.SortingFacade(quickshirts, (s1, s2) => s1.Fabric < s2.Fabric);   //Sorting Desceding by the Fabric.
            PrintAllItems(quickshirts, Element.Fabric, Order.Desceding);


            QuickSort.SizeColorFabricAsc(quickshirts);
            PrintAllItems(quickshirts, Element.Everything, Order.Asceding);      // Facade tha gives (ASC) ordered  Shirts By ---  Size,Color,Fabric ---
            QuickSort.SizeColorFabricDesc(quickshirts);
            PrintAllItems(quickshirts, Element.Everything, Order.Desceding);     // Facade tha gives (DESc) ordered Shirts By ---  Size,Color,Fabric ---



            //// ---------------------------------------------------------------------------------------------- BUCKET SORT-------------------------------------------------------------------


            Console.WriteLine("---BUCKET SORTING---");


            BucketSort.SortColorAsc(bucketShirts);
            PrintAllItems(bucketShirts, Element.Color, Order.Asceding);

            BucketSort.SortSizeAsc(bucketShirts);
            PrintAllItems(bucketShirts, Element.Size, Order.Asceding);

            BucketSort.SortFabricAsc(bucketShirts);
            PrintAllItems(bucketShirts, Element.Fabric, Order.Asceding);
        }