Пример #1
0
        /// <summary>
        /// Method that sorts given array by given criterion.
        /// </summary>
        /// <param name="matrix"> Jagged unsorted array.</param>
        /// <param name="comparer"> Criterion of sorting.</param>
        /// <returns> Sorting matrix.</returns>
        public static int[][] BubbleSort(int[][] matrix, IArrayCompare comparer)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException($"{nameof(matrix)} is empty");
            }

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

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

            var tmpMatrix = (int[][])matrix.Clone();

            for (int j = 0; j < MaxRowLength(tmpMatrix); j++)
            {
                for (int i = 0; i < tmpMatrix.Length - 1; i++)
                {
                    if (comparer.Compare(tmpMatrix[i], tmpMatrix[i + 1]) > 0)
                    {
                        Swap(ref tmpMatrix[i], ref tmpMatrix[i + 1]);
                    }
                }
            }

            return(tmpMatrix);
        }
 public static void Sort(int[][] array,IArrayCompare comparator)
 {
     for (int i = array.Length - 1; i >= 0; i--)
     {
         for (int j = 0; j < i; j++)
         {
             if (comparator.Compare(array[j], array[j + 1])==1)
             {
                   SwapArray(ref array[j], ref array[j+1]);
             }
         }
     }
 }