예제 #1
0
        public void BubbleSortByMinOfRowElementsAscendingUsingDelegateTest()
        {
            int[][] array1   = new int[4][] { new int[] { -4, 0 }, new int[] { 10, 10, 12, -7 }, new int[] { 0, 0, 0, 0 }, new int[] { 19, 19, 20 } };
            int[][] sorted   = new int[4][] { new int[] { 10, 10, 12, -7 }, new int[] { -4, 0 }, new int[] { 0, 0, 0, 0 }, new int[] { 19, 19, 20 } };
            var     comparer = new AdapterForDelegate(new SortByMinAscending().Compare);

            BubbleSortWithDelegate(array1, comparer);
            CollectionAssert.AreEqual(sorted, array1);
        }
예제 #2
0
        public void BubbleSortByMinOfRowElementsDescendingUsingDelegateTest()
        {
            int[][] array1   = new int[3][] { new int[] { -15, 26 }, new int[] { 4, 55, 8, -90 }, new int[] { 1, 3, -10, -7 } };
            int[][] sorted   = new int[3][] { new int[] { 1, 3, -10, -7 }, new int[] { -15, 26 }, new int[] { 4, 55, 8, -90 } };
            var     comparer = new AdapterForDelegate(new SortByMinDescending().Compare);

            BubbleSortWithDelegate(array1, comparer);
            CollectionAssert.AreEqual(sorted, array1);
        }
예제 #3
0
        public void BubbleSortBySumOfRowElementsDescendingUsingDelegateTest()
        {
            int[][] array1   = new int[3][] { new int[] { 1, 2, 3, -8 }, new int[] { 5, -5, 12, -9 }, new int[] { 33, 22, -40, 10 } };
            int[][] sorted   = new int[3][] { new int[] { 33, 22, -40, 10 }, new int[] { 5, -5, 12, -9 }, new int[] { 1, 2, 3, -8 } };
            var     comparer = new AdapterForDelegate(new SortBySumDescending().Compare);

            BubbleSortWithDelegate(array1, comparer);
            CollectionAssert.AreEqual(sorted, array1);
        }
예제 #4
0
        public void BubbleSortByMaxOfRowElementsDescendingUsingDelegateTest()
        {
            int[][] array1   = new int[3][] { new int[] { 5, 12, -3 }, new int[] { 0, 3, 14 }, new int[] { 7, 8, 9 } };
            int[][] sorted   = new int[3][] { new int[] { 0, 3, 14 }, new int[] { 5, 12, -3 }, new int[] { 7, 8, 9 } };
            var     comparer = new AdapterForDelegate(new SortByMaxDescending().Compare);

            BubbleSortWithDelegate(array1, comparer);
            CollectionAssert.AreEqual(sorted, array1);
        }
예제 #5
0
        /// <summary>
        /// Method that sorts given array by given criterion (instance of AdapterForDelegate class) using Bubble Sort algorythm
        /// </summary>
        /// <param name="array">Jagged unsorted array</param>
        /// <param name="adapterForDelegate">Instance of AdapterForDelegate class that incapsulates delegate as sorting type</param>
        public static void BubbleSortWithDelegate(int[][] array, AdapterForDelegate adapterForDelegate)
        {
            if (array == null)
            {
                throw new ArgumentNullException($"{nameof(array)} is empty.");
            }

            if (array.Length == 1)
            {
                return;
            }

            if (adapterForDelegate is null)
            {
                throw new ArgumentNullException(nameof(adapterForDelegate));
            }

            BubbleSort(array, adapterForDelegate);
        }