diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..8dd1d40 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -6,33 +6,39 @@ namespace CourseApp.Module2 { public class BubbleSort { - public static void BubbleSortMethod() + public static void BubbleSortMeth() { - int n = int.Parse(Console.ReadLine()); - string s = Console.ReadLine(); - string[] sValues = s.Split(' '); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) - { - arr[i] = int.Parse(sValues[i]); - } + int arr_size = int.Parse(Console.ReadLine()); + string[] arr_str = Console.ReadLine().Split(' '); + int[] arr = new int[arr_size]; - for (int i = 0; i < arr.Length - 1; i++) - { - for (int j = 0; j < arr.Length - i - 1; j++) + //Строковый массив в целочисенный массив + for(int i = 0; i < arr_size; ++i) { - if (arr[j] > arr[j + 1]) + arr[i] = int.Parse(arr_str[i]); + } + + //Были перестановки или нет + bool flag = false; + //Сортировка + for(int i = 0; i < arr_size; ++i) + { + for(int j = 0; j < arr_size - i - 1; ++j) { - // int temp = arr[j]; - // arr[j] = arr[j + 1]; - // arr[j+1] = temp; - (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]); + if(arr[j] > arr[j+1]) + { + (arr[j], arr[j+1]) = (arr[j+1], arr[j]); + string reslt = string.Join(" ", arr); + Console.WriteLine(reslt); + flag = true; + } } } - } - - string result = string.Join(" ", arr); - Console.WriteLine(result); + + if(flag == false) + { + Console.WriteLine("0"); + } } } -} +} \ No newline at end of file diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..f4b7458 --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,33 @@ + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static int[] Merge(int[] a, int[] b) + { + int indxA = 0; + int indxB = 0; + int k = 0; + int[] result = new int[a.Length + b.Length]; + int n = a.Length + b.Length; + + while(k < result.Length) + { + if(indxB == b.Length || (indxA < a.Length && a[indxA] < b[indxB])) + { + result[k] = a[indxA]; + indxA++; + } + else + { + result[k] = b[indxA]; + indxB++; + } + k++; + } + + return result; + } + } + +} \ No newline at end of file diff --git a/CourseApp/Module2/PairBublSort.cs b/CourseApp/Module2/PairBublSort.cs new file mode 100644 index 0000000..f2e6e82 --- /dev/null +++ b/CourseApp/Module2/PairBublSort.cs @@ -0,0 +1,57 @@ +using System; + +namespace CourseApp.Module2 +{ + public class PairBublSort + { + public static void PairBubbleSort() + { + + int n = Convert.ToInt32(Console.ReadLine()); + string[] arr_str; + int[,] arr = new int[n, 2]; + + for(int i = 0; i < n; ++i) + { + //Получение строк + string str = Console.ReadLine(); + arr_str = str.Split(' '); + + //Преобразование строк в числа + arr[i, 0] = int.Parse(arr_str[0]); + arr[i, 1] = int.Parse(arr_str[1]); + } + + for(int i = 0; i < n - 1; ++i) + { + for(int j = 0; j < n - 1; ++j) + { + //Сортировка по цене + if(arr[j, 1] < arr[j + 1, 1]) + { + (arr[j + 1, 1], arr[j, 1]) = (arr[j, 1], arr[j + 1, 1]); + (arr[j + 1, 0], arr[j, 0]) = (arr[j, 0], arr[j + 1, 0]); + } + //Сортируем по идентификационному номеру, когда цена одинакова + else if(arr[j, 1] == arr[j + 1, 1]) + { + if(arr[j, 0] > arr[j + 1, 0]) + { + (arr[j + 1, 0], arr[j, 0]) = (arr[j, 0], arr[j + 1, 0]); + } + } + } + } + Console.WriteLine(); + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < 2; ++j) + { + Console.Write(arr[i, j]); + Console.Write(" "); + } + Console.WriteLine(); + } + } + } +} diff --git a/README.md b/README.md index e58af8a..2d63cee 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# Tprogramming_42_2020 +# Tprogramming_147_2022 -Master branch :) \ No newline at end of file +Shokhin Akim 1/147 \ No newline at end of file