Reemind

Untitled

May 7th, 2021 (edited)
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Transactions;
  4.  
  5. namespace _07_05
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[,] matrix = new[,]
  12.             {
  13.                 { 5, 6, 1, 7, 7},
  14.                 {-1, 50, 8, 15, 3}
  15.             };
  16.  
  17.             //Sort(matrix);
  18.  
  19.             Setrid(matrix);
  20.  
  21.             var rowLen = matrix.GetUpperBound(0) + 1;
  22.             var colLen = matrix.GetUpperBound(1) + 1;
  23.  
  24.             var m = EnterInt("Enter n[2,20]: ", 2, 20);
  25.             var n = EnterInt("Enter n[2,20]: ", 2, 10);
  26.  
  27.             var row = EnterInt($"Enter row[1,{rowLen}]: ", 1, rowLen) - 1;
  28.             var position = EnterInt($"Enter position[1,{colLen}]: ", 1, colLen) - 1;
  29.  
  30.             Console.WriteLine($"Value is {matrix[row,position]}");
  31.  
  32.             var value = EnterInt("Enter new value: ");
  33.  
  34.             matrix[row, position] = value;
  35.  
  36.             Console.WriteLine($"New value is {matrix[row, position]}");
  37.         }
  38.  
  39.         static void Setrid(int[,] array)
  40.         {
  41.             int rows = array.GetUpperBound(0) + 1;
  42.             for (int row = 0; row < rows; row++)
  43.             {
  44.                 int temp;
  45.                 for (int i = 0; i < array.Length / rows; i++)
  46.                 {
  47.                     for (int j = i + 1; j < array.Length / rows; j++)
  48.                     {
  49.                         if (array[row,i] < array[row, j])
  50.                         {
  51.                             temp = array[row, i];
  52.                             array[row, i] = array[row, j];
  53.                             array[row, j] = temp;
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.  
  60.         static int EnterInt(string text, int min = int.MinValue, int max = int.MaxValue)
  61.         {
  62.             int value;
  63.             do
  64.                 Console.Write(text);
  65.             while (!(int.TryParse(Console.ReadLine(), out value) &&
  66.                    value >= min &&
  67.                    value <= max));
  68.  
  69.             return value;
  70.         }
  71.  
  72.         static int Partition<T>(T[,] m, int a, int b, int rows) where T : IComparable<T>
  73.         {
  74.             int rowLength = m.Length / rows;
  75.             int i = a;
  76.             for (int j = a; j <= b; j++)
  77.             {
  78.                 if (m[j / rowLength, j % rowLength].CompareTo(m[b / rowLength, b % rowLength]) <= 0)
  79.                 {
  80.                     T t = m[i / rowLength, i % rowLength];
  81.                     m[i / rowLength, i % rowLength] = m[j / rowLength, j % rowLength];
  82.                     m[j / rowLength, j % rowLength] = t;
  83.                     i++;
  84.                 }
  85.             }
  86.             return i - 1;
  87.         }
  88.  
  89.         public static void Sort<T>(T[,] m) where T : IComparable<T>
  90.         {
  91.             Sort(m, 0, m.Length - 1, m.GetUpperBound(0) + 1);
  92.         }
  93.  
  94.         static void Sort<T>(T[,] m, int a, int b, int rows) where T : IComparable<T>
  95.         {
  96.             if (a >= b) return;
  97.             int c = Partition(m, a, b, rows);
  98.             Sort(m, a, c - 1, rows);
  99.             Sort(m, c + 1, b, rows);
  100.         }
  101.     }
  102. }
  103.  
Add Comment
Please, Sign In to add comment