/*public static long multiply(long x, long y)
         * {
         *  long result = 0;
         *
         *  int size1 = GetSize(x);
         *  int size2 = GetSize(y);
         *
         *  //find the max size of two integers
         *  int N = Math.Max(size1, size2);
         *
         *  if (N < 2)
         *      return x * y;
         *
         *  //Max length divided by two and rounded up
         *  N = (N / 2) + (N % 2);
         *
         *  //The mulitplying factor for calculating a,b,c,d
         *  long m = (long)Math.Pow(10, N);
         *
         *  long b = x % m;
         *  long a = x / m;
         *  long c = y / m;
         *  long d = y % m;
         *
         *  long z0 = multiply(a, c);
         *  long z1 = multiply(b, d);
         *  long z2 = multiply(a + b, c + d);
         *
         *  result = ((long)Math.Pow(10, N * 2) * z0) + z1 + ((z2 - z1 - z0) * m);
         *  return result;
         * }*/
        static void Main(string[] args)
        {
            Console.WriteLine("G = {0},\nK = {1},\nP = {2},\nX = ",
                              MathAlgorithms.GradeSchoolMultiplication(999999999, 999999999),
                              MathAlgorithms.KaratsubaMultiplication(999999999, 999999999),
                              999999999L * 999999999L);

            int[] arr = { 2, 1, 6, 3, 5, 8, 11, 20, 4, 465, 4645, 645, 675, 765, 7, 567, 65 };
            Console.WriteLine(SearchingAlgorithms.BinarySearch <int>(arr, 20));

            List <string> list = new List <string>()
            {
                "Ahmed", "ahmed", "sara", "amgad"
            };

            Console.WriteLine(SearchingAlgorithms.BinarySearch <string>(list, "ahmed"));

            SortingAlgorithms.SelectionSort <int>(arr);
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            SortingAlgorithms.SelectionSort <string>(list);
            foreach (var item in list)
            {
                Console.Write(item + " ");
            }

            arr = new int[] { 2, 1, 6, 3, 5, 8, 11, 20, 4, 465, 4645, 645, 675, 765, 7, 567, 65 };
            SortingAlgorithms.MergeSort <int>(arr, 0, arr.Length - 1);
        }
Beispiel #2
0
        public void IntBinarySearchTest()
        {
            //Arrange
            int[] array    = new int[] { 0, 1, 1, 4, 5, 9 };
            int?  expected = 0;

            //Act
            int?actual = SearchingAlgorithms.IntBinarySearch(array, 0);

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #3
0
    public static void Main()
    {
        int[] arr = new int[] { 3, -1, 15, 4, 17, 2, 33, 0 };
        Console.WriteLine("arr = [{0}]", string.Join(", ", arr));
        SortingAlgorithms.SelectionSort(arr);
        Console.WriteLine("sorted = [{0}]", string.Join(", ", arr));

        SortingAlgorithms.SelectionSort(new int[0]); // Test sorting empty array
        SortingAlgorithms.SelectionSort(new int[1]); // Test sorting single element array

        Console.WriteLine(SearchingAlgorithms.BinarySearch(arr, -1000));
        Console.WriteLine(SearchingAlgorithms.BinarySearch(arr, 0));
        Console.WriteLine(SearchingAlgorithms.BinarySearch(arr, 17));
        Console.WriteLine(SearchingAlgorithms.BinarySearch(arr, 10));
        Console.WriteLine(SearchingAlgorithms.BinarySearch(arr, 1000));
    }
Beispiel #4
0
        public static void Main(string[] args)
        {
            CreateHeader();
            while (true)
            {
                Console.WriteLine("Input problem #:");
                string userInput     = "lis";
                int    problemNumber = 0;

                if (userInput == "sort")
                {
                    SortingAlgorithms SortingAlgorithms = new SortingAlgorithms();
                    SortingAlgorithms.Main();
                }
                else if (userInput == "LinkedList")
                {
                    LinkedListTest linkedList = new LinkedListTest();
                    linkedList.Main();
                }
                else if (userInput == "Fib")
                {
                    Fib fib = new Fib();
                    fib.Main();
                }
                else if (userInput == "lis")
                {
                    LISS lis = new LISS();
                    lis.Main();
                }
                else if (userInput == "search")
                {
                    SearchingAlgorithms search = new SearchingAlgorithms();
                    search.Main();
                }
                else
                {
                    bool validInput = Int32.TryParse(userInput, out problemNumber);
                    if (validInput)
                    {
                        switch (problemNumber)
                        {
                        case 1:
                            Problem1 problem1 = new Problem1();
                            problem1.Main();
                            break;

                        case 21:
                            Problem21 problem21 = new Problem21();
                            problem21.Main();
                            break;

                        case 29:
                            Problem29 problem29 = new Problem29();
                            problem29.Main();
                            break;

                        case 37:
                            Problem37 problem37 = new Problem37();
                            problem37.Main();
                            break;

                        case 44:
                            Problem44 problem44 = new Problem44();
                            problem44.Main();
                            break;

                        case 57:
                            Problem57 problem57 = new Problem57();
                            problem57.Main();
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }