コード例 #1
0
        int[,] IMatrixSort.SortMatrix(bool descending)
        {
            var bubbleSortedMatrix = MatrixHelper.BubbleSortByRow(inputMatrix, rows, columns);
            var minimumValues      = MatrixHelper.MinimumRowValue(bubbleSortedMatrix, rows, columns);
            var tempRows           = new int[rows];

            minimumValues.CopyTo(tempRows, 0);
            Array.Sort(minimumValues, indexes);
            if (descending)
            {
                Array.Reverse(indexes);
            }
            var sortedMatrix = new int[rows, columns];

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < columns; j++)
                {
                    sortedMatrix[i, j] = bubbleSortedMatrix[indexes[i], j];
                }
            }
            return(sortedMatrix);
        }
コード例 #2
0
        int[,] IMatrixSort.SortMatrix(bool descending)
        {
            var rowSum   = MatrixHelper.RowsSum(inputMatrix, row, column);
            var tempRows = new int[row];

            rowSum.CopyTo(tempRows, 0);

            Array.Sort(rowSum, indexes);
            if (descending)
            {
                Array.Reverse(indexes);
            }
            var sortedMatrix = new int[row, column];

            for (var i = 0; i < row; i++)
            {
                for (var j = 0; j < column; j++)
                {
                    sortedMatrix[i, j] = inputMatrix[indexes[i], j];
                }
            }
            return(sortedMatrix);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: FursovaKate/SummerTraining
        static void Main()
        {
            Console.WriteLine("Enter number of elements in row:\n");
            var row = InputHelper.ParseInput();

            Console.WriteLine("Enter number of elements in column:\n");
            var column = InputHelper.ParseInput();

            var matrix  = MatrixHelper.CreateMatrix(row, column);
            var indexes = MatrixHelper.MatrixIndexes(matrix, row, column);
            var result  = matrix;

            Console.WriteLine("Choose sorting type\n1 - sort by row sum (ascending);\n2 - sort by row sum (descending);\n3 - sort by max element in row (ascending);\n4 - sort by max element in row (descending)\n5 - sort by min element in row (ascending)\n6 - sort by min element in row(descending)\n");
            var sortingType = InputHelper.ParseInput();

            MatrixHelper.PrintMatrix(matrix, row, column);

            var             descending            = false;
            SortingStrategy chooseSortingStrategy = null;

            MatrixSortBySum sortBySum = new MatrixSortBySum(result, row, column, indexes);

            if (sortingType == 1)
            {
                chooseSortingStrategy = new SortingStrategy(((IMatrixSort)sortBySum).SortMatrix);
            }

            if (sortingType == 2)
            {
                descending            = true;
                chooseSortingStrategy = new SortingStrategy(((IMatrixSort)sortBySum).SortMatrix);
            }

            MatrixSortByMaxElement sortByMaxElement = new MatrixSortByMaxElement(result, row, column, indexes);

            if (sortingType == 3)
            {
                chooseSortingStrategy = new SortingStrategy(((IMatrixSort)sortByMaxElement).SortMatrix);
            }

            if (sortingType == 4)
            {
                descending            = true;
                chooseSortingStrategy = new SortingStrategy(((IMatrixSort)sortByMaxElement).SortMatrix);
            }

            MatrixSortByMinElement sortByMinElement = new MatrixSortByMinElement(result, row, column, indexes);

            if (sortingType == 5)
            {
                chooseSortingStrategy = new SortingStrategy(((IMatrixSort)sortByMinElement).SortMatrix);
            }

            if (sortingType == 6)
            {
                descending            = true;
                chooseSortingStrategy = new SortingStrategy(((IMatrixSort)sortByMinElement).SortMatrix);
            }

            result = chooseSortingStrategy.Invoke(descending);

            Console.WriteLine("Sorted matrix:");
            for (var i = 0; i < row; i++)
            {
                for (var j = 0; j < column; j++)
                {
                    Console.Write(result[i, j] + " ");
                }
                Console.WriteLine();
            }
        }