/*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);
        }
Ejemplo n.º 2
0
    static void Main(string[] args)
    {
        int[] OriginalArray = { 5, 89, 43, 13, 67, 11, 45 };
        int[] A = new int[OriginalArray.Length];
        SortingAlgorithms sortAlgo = new SortingAlgorithms();

        // insertion sort demo
        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.InsertionSort(A);
        Console.WriteLine("After insertion sort list contains: ");
        foreach (var item in A)
            Console.Write(" {0}", item);
        Console.WriteLine();

        // Selection sort demo
        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.SelectionSort(A);
        Console.WriteLine("After Selection sort list contains: ");
        foreach (var item in A)
            Console.Write(" {0}", item);
        Console.WriteLine();

        // Bubble sort demo
        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.BubbleSort(A);
        Console.WriteLine("After Bubble sort list contains: ");
        foreach (var item in A)
            Console.Write(" {0}", item);
        Console.WriteLine();
    }
    private static void DisplaySortingAlgorithmsComparison <T>(T[] array, bool sortBeforehand, Comparison <T> comparison = null)
        where T : IComparable <T>
    {
        if (sortBeforehand)
        {
            Array.Sort(array, comparison);
        }

        T[] array1 = (T[])array.Clone();
        T[] array2 = (T[])array.Clone();
        T[] array3 = (T[])array.Clone();

        string insertionSortMessage = string.Format("Insertion sort for {0}[]", typeof(T).Name).PadRight(35, '.') + ": ";

        DisplayExecutionTime(() =>
        {
            SortingAlgorithms.InsertionSort(array1);
        }, insertionSortMessage);

        string selectionSortMessage = string.Format("Selection sort for {0}[]", typeof(T).Name).PadRight(35, '.') + ": ";

        DisplayExecutionTime(() =>
        {
            SortingAlgorithms.SelectionSort(array2);
        }, selectionSortMessage);

        string quicksortMessage = string.Format("Quicksort for {0}[]", typeof(T).Name).PadRight(35, '.') + ": ";

        DisplayExecutionTime(() =>
        {
            SortingAlgorithms.Quicksort(array3, 0, array3.Length - 1);
        }, quicksortMessage);
    }
        public void SelectionSortTest()
        {
            int[] unsorted = new int[20000];

            Random random = new Random();

            for (int i = 0; i < 20000; i++)
            {
                unsorted[i] = (random.Next(1, 200));
            }
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            int[] result = _object.SelectionSort(unsorted);
            stopwatch.Stop();
            string totaltime = stopwatch.Elapsed.ToString();

            Assert.AreEqual(totaltime, 10);

            /*
             * 20,000  -00:00:00.1306896
             * 2,00,000-00:00:02.6504209
             * 20,00,000-
             */
        }
        public void SelectionSortTest()
        {
            SortingAlgorithms <int> .SelectionSort(_Items);

            string tmp = string.Join(",", _Items);

            Assert.AreEqual(tmp, _ExpectedResult);
        }
Ejemplo n.º 6
0
        public void TestSelectionSort()
        {
            // Arrange
            int[] arr      = new int[] { 4, 10, 2, 1, 8, 9, 5, 7, 6, 3 };
            int[] expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            // Act
            SortingAlgorithms.SelectionSort(arr);

            // Assert
            CollectionAssert.AreEqual(expected, arr);
        }
Ejemplo n.º 7
0
        public void SelectionSortTest()
        {
            //Arrange
            int[] unsorted = new int[] { 9, 1, 5, 2, 4, 0 };
            int[] expected = new int[] { 0, 1, 2, 4, 5, 9 };

            //Act
            SortingAlgorithms.SelectionSort(unsorted);

            //Assert
            CollectionAssert.AreEqual(expected, unsorted);
        }
Ejemplo n.º 8
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));
    }
Ejemplo n.º 9
0
        private static void StringSorting()
        {
            string[] arrayToSort = new string[] { "hehe", "wow", "isurual", "whatIf", "isRael", "isnt", "rael", "huh", "whaddaboutdat", "tahtsIT", "noMoreStrings" };
            Logger.Log("String array sorting");

            TimeTracker.MeasureTime("Insertion sort", () =>
            {
                SortingAlgorithms.InsertionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Selection sort", () =>
            {
                SortingAlgorithms.SelectionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Quick sort", () =>
            {
                SortingAlgorithms.QuickSort(arrayToSort, 0, arrayToSort.Length - 1);
            });
        }
        private static void IntSorting()
        {
            GenerateArray(213, 8743267);

            Console.WriteLine("Int array sorting");

            TimeTracker.MeasureTime("Insertion sort", () =>
            {
                SortingAlgorithms.InsertionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Selection sort", () =>
            {
                SortingAlgorithms.SelectionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Quick sort", () =>
            {
                SortingAlgorithms.QuickSort(arrayToSort, 0, arrayToSort.Length - 1);
            });
        }
Ejemplo n.º 11
0
        private static void DoubleSorting()
        {
            var arrayToSort = RandomArrayGenerator.GenerateDoubleArray(-30, 23242342);

            Logger.Log("Double array sorting");

            TimeTracker.MeasureTime("Insertion sort", () =>
            {
                SortingAlgorithms.InsertionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Selection sort", () =>
            {
                SortingAlgorithms.SelectionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Quick sort", () =>
            {
                SortingAlgorithms.QuickSort(arrayToSort, 0, arrayToSort.Length - 1);
            });
        }
Ejemplo n.º 12
0
        //Chooses the proper algorithm based on the AlgorithmsComboBox.SelectedItem
        private void Sort(ref List <int> n)
        {
            ComboBoxItem item = AlgorithmsComboBox.SelectedItem as ComboBoxItem;

            switch (item.Content.ToString())
            {
            case "Bubble Sort":
                SortingAlgorithms.BubbleSort(ref n);
                break;

            case "Insertion Sort":
                SortingAlgorithms.InsertionSort(ref n);
                break;

            case "Selection Sort":
                SortingAlgorithms.SelectionSort(ref n);
                break;

            case "Merge Sort":
                n = SortingAlgorithms.MergeSort(n);
                break;
            }
        }
        private static void DoubleSorting()
        {
            var array = GenerateArray(132, 783245238)
                        .Select(x => (double)(x + 1.01))
                        .ToArray();

            Console.WriteLine("Double array sorting");

            TimeTracker.MeasureTime("Insertion sort", () =>
            {
                SortingAlgorithms.InsertionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Selection sort", () =>
            {
                SortingAlgorithms.SelectionSort(arrayToSort);
            });

            TimeTracker.MeasureTime("Quick sort", () =>
            {
                SortingAlgorithms.QuickSort(arrayToSort, 0, arrayToSort.Length - 1);
            });
        }
Ejemplo n.º 14
0
    static void Main(string[] args)
    {
        int[]             OriginalArray = { 5, 89, 43, 13, 67, 11, 45 };
        int[]             A             = new int[OriginalArray.Length];
        SortingAlgorithms sortAlgo      = new SortingAlgorithms();

        // insertion sort demo
        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.InsertionSort_v1(A);
        Console.WriteLine("After insertion sort v1 list contains: ");
        Console.WriteLine(string.Join(" ", A));

        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.InsertionSort_v2(A);
        Console.WriteLine("After insertion sort v2 list contains: ");
        Console.WriteLine(string.Join(" ", A));

        // Selection sort demo
        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.SelectionSort(A);
        Console.WriteLine("After Selection sort list contains: ");
        Console.WriteLine(string.Join(" ", A));

        // Bubble sort demo
        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.BubbleSort_v1(A);
        Console.WriteLine("After Bubble sort v1 list contains: ");
        foreach (var item in A)
        {
            Console.Write(" {0}", item);
        }
        Console.WriteLine();
        Array.Copy(OriginalArray, A, A.Length);
        sortAlgo.BubbleSort_v2(A);
        Console.WriteLine("After Bubble sort v2 list contains: ");
        Console.WriteLine(string.Join(" ", A));
    }