Beispiel #1
0
 public static void SortMatrixMode(this int[][] matrix, IArrayComparer sortmodes)
 {
     if (matrix == null)
     {
         throw new ArgumentException();
     }
     BubbleSort.Sort(matrix, sortmodes.Compare);
 }
 private static void BubleSort(int[][] mass, IArrayComparer comparator)
 {
     for (int i = 0; i < mass.Length; i++)
     {
         for (int j = i + 1; j < mass.Length; j++)
         {
             if (comparator.Compare(mass[i], mass[j]))
             {
                 var temp = mass[i];
                 mass[i] = mass[j];
                 mass[j] = temp;
             }
         }
     }
 }
        /// <summary>
        /// Sort matrix rows using specified compare method.
        /// </summary>
        /// <param name="array">Matrix to be sorted.</param>
        /// <param name="compareMethod">Method that compares two arrays and returns positive number if the first is greater, zero if equals and negative if smaller.</param>
        /// <param name="increasing">Specifies the order of sorting.</param>
        public static void SortRows(int[][] array, IArrayComparer comparer)
        {
            int i = 1;
            bool found = true;

            while (i < array.Length && found)
            {
                found = false;
                for (int j = array.Length - 1; j >= i; j--)
                {
                    if (comparer.Compare(array[j - 1], array[j]) > 0)
                    {
                        SwapElements(array, j, j - 1);
                    }
                    found = true;
                }
                i++;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Method that sorts given array by given criterion (instance of IArrayComparer type) using Bubble Sort algorythm
        /// </summary>
        /// <param name="array">Jagged unsorted array</param>
        /// <param name="comparer">Criterion of sorting</param>
        public static void BubbleSort(int[][] array, IArrayComparer comparer)
        {
            if (array == null)
            {
                throw new ArgumentNullException($"{nameof(array)} is empty.");
            }

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

            for (int j = 0; j < MaxRowLength(array); j++)
            {
                for (int i = 0; i < array.Length - 1; i++)
                {
                    if (comparer.Compare(array[i], array[i + 1]) > 0)
                    {
                        Swap(ref array[i], ref array[i + 1]);
                    }
                }
            }
        }
 public static void Sort(int[][] mass, IArrayComparer comparator)
 {
     if(mass.Length == 0)
         throw new ArgumentNullException("The jagged array to sort is empty");
     BubleSort(mass,comparator);
 }
 public AscendingDecorator(IArrayComparer comparator, bool isAscending)
 {
     this.comparator = comparator;
     this.IsAscending = isAscending;
 }