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 InsertionSortTest()
        {
            SortingAlgorithms <int> .InsertionSort(_Items);

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

            Assert.AreEqual(tmp, _ExpectedResult);
        }
 public void TestInsertionSort()
 {
     int[] input  = new int[] { 1, 5, 6, 2, 3, 4 };
     int[] output = SortingAlgorithms.InsertionSort(input);
     for (int i = 0; i < output.Length; i++)
     {
         Assert.IsTrue(output[i] == i + 1);
     }
 }
Exemple #5
0
        public void InsertionSortTest()
        {
            //Arrange
            //int[] unsorted = new int[] { 2, 5, 4, 0, 9, 1 };
            int[] unsorted = new int[] { 9, 1, 1, 5, 4, 0 };
            int[] expected = new int[] { 0, 1, 1, 4, 5, 9 };

            //Act
            int[] actual = SortingAlgorithms.InsertionSort(unsorted);

            //Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Exemple #6
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);
            });
        }
Exemple #8
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);
            });
        }
Exemple #9
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);
            });
        }
Exemple #11
0
    public static string[] ComputeArrayWithoutDelegate(string[] data, SortingTypes sortingType)
    {
        // Do some stuff on the array

        // Order the array
        switch (sortingType)
        {
        case SortingTypes.BubbleSort:
            data = SortingAlgorithms.BubbleSort(data);
            break;

        case SortingTypes.QuickSort:
            data = SortingAlgorithms.QuickSort(data);
            break;

        case SortingTypes.InsertionSort:
            data = SortingAlgorithms.InsertionSort(data);
            break;
        }

        // Do other stuff on the array

        return(data);
    }
 public void Run()
 {
     int[] inputArray  = new int[] { 2, 5, 3, 7, 4, 1, 6 };
     int[] sortedArray = SortingAlgorithms.InsertionSort(inputArray);
 }
 private void SortCollidableObjects()
 {
     SortingAlgorithms.InsertionSort <ICollidableObject>(CollisionItems, _comparer);
 }