Advertisement
Shepardcmndr

JaggedArraySort

Mar 25th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             int[][] arrTaxi = new int[7][];
  6.             arrTaxi[0] = new int[] {5};
  7.             arrTaxi[1] = new int[] {1, 24, 8, 9};
  8.             arrTaxi[2] = new int[] {2, 7};
  9.             arrTaxi[3] = new int[] {6, 67, 3, 15, 9};
  10.             arrTaxi[4] = new int[] {12};
  11.             arrTaxi[5] = new int[] {7, 98, 45, 5, 6};
  12.             arrTaxi[6] = new int[] {7, 9};
  13.  
  14.             ShowArray(arrTaxi);
  15.             TaxiArraySort(arrTaxi);
  16.  
  17.             Console.WriteLine();
  18.             ShowArray(arrTaxi);
  19.  
  20.             Console.ReadKey();
  21.         }
  22.  
  23.         /*
  24.          * Функция сортирующая входной массив jaggedInput методом пузырьковой сортриовки        
  25.          */
  26.         public static int[][] TaxiArraySort(int[][] jaggedInput)
  27.         {
  28.             for (int i = jaggedInput.GetLength(0) - 1; i >= 0; i--)
  29.             {
  30.                 for (int j = jaggedInput.GetLength(0) - 1; j >= 0; j--)
  31.                 {
  32.                     if (jaggedInput[i].Length < jaggedInput[j].Length)
  33.                     {
  34.                         SwitchArrays(jaggedInput, j, i);
  35.                     }
  36.                     else if (jaggedInput[i].Length == jaggedInput[j].Length &&
  37.                              SumOfArrayValues(jaggedInput[i]) < SumOfArrayValues(jaggedInput[j]))
  38.                     {
  39.                         SwitchArrays(jaggedInput, j, i);
  40.                     }                    
  41.                 }
  42.             }
  43.  
  44.             return jaggedInput;
  45.         }
  46.  
  47.         /*
  48.          * Функция для перемены местами двух массивов с индексами firstArrayIndex
  49.          * и secondArrayIndex из массива inputArrray
  50.          */
  51.         public static int[][] SwitchArrays(int[][] inputArrray, int firstArrayIndex, int secondArrayIndex)
  52.         {
  53.             int[] tempArray = new int[inputArrray[firstArrayIndex].Length];
  54.             Array.Copy(inputArrray[firstArrayIndex], tempArray, tempArray.Length);
  55.  
  56.             if (inputArrray[firstArrayIndex].Length != inputArrray[secondArrayIndex].Length)
  57.             {
  58.                 Array.Resize(ref inputArrray[firstArrayIndex], inputArrray[secondArrayIndex].Length);
  59.             }
  60.  
  61.             Array.Copy(inputArrray[secondArrayIndex], inputArrray[firstArrayIndex], inputArrray[secondArrayIndex].Length);
  62.  
  63.             if (inputArrray[secondArrayIndex].Length != tempArray.Length)
  64.             {
  65.                 Array.Resize(ref inputArrray[secondArrayIndex], tempArray.Length);
  66.             }
  67.  
  68.             Array.Copy(tempArray, inputArrray[secondArrayIndex], tempArray.Length);
  69.  
  70.             return inputArrray;
  71.         }
  72.  
  73.  
  74.         /*
  75.          * Функция для подсчета суммы значений в массиве
  76.          */
  77.         public static int SumOfArrayValues(int[] inputArray)
  78.         {
  79.             int sum = 0;
  80.  
  81.             for (int i = 0; i < inputArray.Length; i++)
  82.             {
  83.                 sum += inputArray[i];
  84.             }
  85.  
  86.             return sum;
  87.         }
  88.  
  89.         /*
  90.          * Функция для вывода массива inputArray в консоль
  91.          */
  92.         public static void ShowArray(int[][] inputArray)
  93.         {
  94.             for (int i = 0; i < inputArray.GetLength(0); i++)
  95.             {
  96.                 for (int j = 0; j < inputArray[i].Length; j++)
  97.                 {
  98.                     Console.Write(inputArray[i][j] + " ");
  99.                 }
  100.                 Console.WriteLine();
  101.             }
  102.         }
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement