Example #1
0
        static void Main()
        {
            double selection;

            do
            {
                Console.WriteLine("4,1 CUSTOM SORT.");
                Console.WriteLine("4,2 CUSTOM SORT DEMO.");
                Console.WriteLine("4,3 SORTING UNIT.");
                Console.WriteLine("4,4 NUMBER ARRAY SUM.");
                Console.WriteLine("4,5 TO INT OR NOT TO INT?");
                Console.WriteLine("4,6 I SEEK YOU.");
                Console.WriteLine("0 Exit.");
                Console.WriteLine();
                if (double.TryParse(Console.ReadLine(), out selection))
                {
                    switch (selection)
                    {
                    case 4.1:
                        CustomSort.ShowFirstTask();
                        break;

                    case 4.2:
                        CustomSort.ShowSecondTask();
                        break;

                    case 4.3:
                        SortingUnit.Show();
                        break;

                    case 4.4:
                        NumberArraySum.ShowFourthTask();
                        break;

                    case 4.5:
                        NumberArraySum.ShowFifthTask();
                        break;

                    case 4.6:
                        ISeekYou.Show();
                        break;

                    case 0:
                        break;
                    }
                }
            } while (selection != 0);
        }
Example #2
0
        public static void ConsoleInterface()
        {
            //Initializing array
            int[] array = new int[128000];
            Initialize(array);
            //Show(array);
            Stopwatch sw = new Stopwatch();

            long[] timeIntervals       = new long[50];
            Func <int, int, int> deleg = delegate(int first, int second)
            {
                if (first > second)
                {
                    return(1);
                }
                else if (first < second)
                {
                    return(-1);
                }
                return(0);
            };
            int result = -1;

            Console.WriteLine("Using simple method.");
            for (int i = 0; i < 50; i++)
            {
                sw.Reset();
                sw.Start();
                //Simple method
                result = ISeekYou <int> .BinarySearch(array, 100);

                sw.Stop();
                timeIntervals[i] = sw.ElapsedMilliseconds;
            }
            Console.WriteLine("Result: " + result);
            Console.WriteLine("Average elapsed time: " + timeIntervals.Average());
            Console.WriteLine("Using method with an instance of delegate.");
            for (int i = 0; i < 50; i++)
            {
                sw.Reset();
                sw.Start();
                //Instance of delegate
                result = ISeekYou <int> .BinarySearch(array, 100, deleg);

                sw.Stop();
                timeIntervals[i] = sw.ElapsedMilliseconds;
            }
            Console.WriteLine("Result: " + result);
            Console.WriteLine("Average elapsed time: " + timeIntervals.Average());
            Console.WriteLine("Using method with an anonymous method.");

            for (int i = 0; i < 50; i++)
            {
                sw.Reset();
                sw.Start();
                //Anonymous method
                result = ISeekYou <int> .BinarySearch(array, 100, delegate(int first, int second) {
                    if (first > second)
                    {
                        return(1);
                    }
                    else if (first < second)
                    {
                        return(-1);
                    }
                    return(0);
                });

                sw.Stop();
                timeIntervals[i] = sw.ElapsedMilliseconds;
            }
            Console.WriteLine("Result: " + result);
            Console.WriteLine("Average elapsed time: " + timeIntervals.Average());
            Console.WriteLine("Using method with a lambda expression.");
            for (int i = 0; i < 50; i++)
            {
                sw.Reset();
                sw.Start();
                //Lambda expression
                result = ISeekYou <int> .BinarySearch(array, 100, (first, second) => (first > second ? 1: first < second? -1 : 0));

                sw.Stop();
                timeIntervals[i] = sw.ElapsedMilliseconds;
            }
            Console.WriteLine("Result: " + result);
            Console.WriteLine("Average elapsed time: " + timeIntervals.Average());
            Console.WriteLine("Using LINQ.");
            for (int i = 0; i < 50; i++)
            {
                sw.Reset();
                sw.Start();
                //LINQ
                var result2 = from item in array
                              where item == 100
                              select item;
                sw.Stop();
                timeIntervals[i] = sw.ElapsedMilliseconds;
            }
            Console.WriteLine("Result: " + result);
            Console.WriteLine("Average elapsed time: " + timeIntervals.Average());
        }