Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- int[][] arrTaxi = new int[7][];
- arrTaxi[0] = new int[] {5};
- arrTaxi[1] = new int[] {1, 24, 8, 9};
- arrTaxi[2] = new int[] {2, 7};
- arrTaxi[3] = new int[] {6, 67, 3, 15, 9};
- arrTaxi[4] = new int[] {12};
- arrTaxi[5] = new int[] {7, 98, 45, 5, 6};
- arrTaxi[6] = new int[] {7, 9};
- ShowArray(arrTaxi);
- TaxiArraySort(arrTaxi);
- Console.WriteLine();
- ShowArray(arrTaxi);
- Console.ReadKey();
- }
- /*
- * Функция сортирующая входной массив jaggedInput методом пузырьковой сортриовки
- */
- public static int[][] TaxiArraySort(int[][] jaggedInput)
- {
- for (int i = jaggedInput.GetLength(0) - 1; i >= 0; i--)
- {
- for (int j = jaggedInput.GetLength(0) - 1; j >= 0; j--)
- {
- if (jaggedInput[i].Length < jaggedInput[j].Length)
- {
- SwitchArrays(jaggedInput, j, i);
- }
- else if (jaggedInput[i].Length == jaggedInput[j].Length &&
- SumOfArrayValues(jaggedInput[i]) < SumOfArrayValues(jaggedInput[j]))
- {
- SwitchArrays(jaggedInput, j, i);
- }
- }
- }
- return jaggedInput;
- }
- /*
- * Функция для перемены местами двух массивов с индексами firstArrayIndex
- * и secondArrayIndex из массива inputArrray
- */
- public static int[][] SwitchArrays(int[][] inputArrray, int firstArrayIndex, int secondArrayIndex)
- {
- int[] tempArray = new int[inputArrray[firstArrayIndex].Length];
- Array.Copy(inputArrray[firstArrayIndex], tempArray, tempArray.Length);
- if (inputArrray[firstArrayIndex].Length != inputArrray[secondArrayIndex].Length)
- {
- Array.Resize(ref inputArrray[firstArrayIndex], inputArrray[secondArrayIndex].Length);
- }
- Array.Copy(inputArrray[secondArrayIndex], inputArrray[firstArrayIndex], inputArrray[secondArrayIndex].Length);
- if (inputArrray[secondArrayIndex].Length != tempArray.Length)
- {
- Array.Resize(ref inputArrray[secondArrayIndex], tempArray.Length);
- }
- Array.Copy(tempArray, inputArrray[secondArrayIndex], tempArray.Length);
- return inputArrray;
- }
- /*
- * Функция для подсчета суммы значений в массиве
- */
- public static int SumOfArrayValues(int[] inputArray)
- {
- int sum = 0;
- for (int i = 0; i < inputArray.Length; i++)
- {
- sum += inputArray[i];
- }
- return sum;
- }
- /*
- * Функция для вывода массива inputArray в консоль
- */
- public static void ShowArray(int[][] inputArray)
- {
- for (int i = 0; i < inputArray.GetLength(0); i++)
- {
- for (int j = 0; j < inputArray[i].Length; j++)
- {
- Console.Write(inputArray[i][j] + " ");
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement