diff --git a/Recursion/Recursion.csproj b/Chapter4_QuickSort/Chapter4_QuickSort.csproj similarity index 100% rename from Recursion/Recursion.csproj rename to Chapter4_QuickSort/Chapter4_QuickSort.csproj diff --git a/Chapter4_QuickSort/Program.cs b/Chapter4_QuickSort/Program.cs new file mode 100644 index 0000000..65b9f80 --- /dev/null +++ b/Chapter4_QuickSort/Program.cs @@ -0,0 +1,19 @@ +// Задача 4.1 - сумма массива в виде рекурсивной функции +int[] arr = [1, 2, 3]; +Console.WriteLine($"4.1. Сумма: [{string.Join(", ", arr)}] равна {Task4_1.Calculate(arr)}"); + +// Задача 4.2 - количество элементов в списке в виде рекурсивной функции +LinkedList list = new LinkedList([10, 20, 30, 40, 50]); +Console.WriteLine($"4.2. Количество элементов в списке: {Task4_2.Count(list.First!)}"); + +// Задача 4.3 - поиск максимального элемента в LinkedList с помощью рекурсии +// Пример: находим максимум в списке [7, 2, 9, 4, 5] +LinkedList list2 = new LinkedList([7, 2, 9, 4, 5]); +Console.WriteLine($"4.3. Максимум в списке: {Task4_3.Max(list2.First!)}"); + +// Задача 4.4 - бинарный поиск в отсортированной коллекции с помощью рекурсии +int[] sortedArr = [10, 20, 30, 40, 50, 60]; +int target = 42; +int index = Task4_4.BinSearch(sortedArr, target, 0, sortedArr.Length - 1); +Console.WriteLine($"4.4. Индекс числа {target} в массиве: {index}"); + diff --git a/Chapter4_QuickSort/Task4_1_Sum.cs b/Chapter4_QuickSort/Task4_1_Sum.cs new file mode 100644 index 0000000..4a1ca63 --- /dev/null +++ b/Chapter4_QuickSort/Task4_1_Sum.cs @@ -0,0 +1,9 @@ +static class Task4_1 +{ + public static int Calculate(int[] arr, int index = 0) + { + if (index == arr.Length - 1) + return arr[index]; + return arr[index] + Calculate(arr, index+1); + } +} \ No newline at end of file diff --git a/Chapter4_QuickSort/Task4_2_Count.cs b/Chapter4_QuickSort/Task4_2_Count.cs new file mode 100644 index 0000000..0ebb6f0 --- /dev/null +++ b/Chapter4_QuickSort/Task4_2_Count.cs @@ -0,0 +1,9 @@ +static class Task4_2 +{ + public static int Count(LinkedListNode listNode, int currentLen = 0) + { + if (listNode.Next != null) + return Count(listNode.Next, currentLen + 1); + return currentLen + 1; + } +} \ No newline at end of file diff --git a/Chapter4_QuickSort/Task4_3_Max.cs b/Chapter4_QuickSort/Task4_3_Max.cs new file mode 100644 index 0000000..a417df4 --- /dev/null +++ b/Chapter4_QuickSort/Task4_3_Max.cs @@ -0,0 +1,10 @@ +static class Task4_3 +{ + public static int Max(LinkedListNode listNode, int currMax = int.MinValue) + { + currMax = Math.Max(currMax, listNode.Value); + if (listNode.Next != null) + return Max(listNode.Next, currMax); + return currMax; + } +} \ No newline at end of file diff --git a/Chapter4_QuickSort/Task4_4_BinSearch.cs b/Chapter4_QuickSort/Task4_4_BinSearch.cs new file mode 100644 index 0000000..b337f70 --- /dev/null +++ b/Chapter4_QuickSort/Task4_4_BinSearch.cs @@ -0,0 +1,16 @@ +static class Task4_4 +{ + public static int BinSearch(IEnumerable collection, int target, int left, int right) + { + if (left > right) + return left; // позиция для вставки + + var mid = (left + right) / 2; + var value = collection.ElementAt(mid); + if (value == target) + return mid; + if (value < target) + return BinSearch(collection, target, mid + 1, right); + return BinSearch(collection, target, left, mid - 1); + } +} \ No newline at end of file diff --git a/GrokAlgorithms.sln b/GrokAlgorithms.sln index b59c8ce..7b69c3f 100644 --- a/GrokAlgorithms.sln +++ b/GrokAlgorithms.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Recursion", "Recursion\Recursion.csproj", "{7F895D96-6E79-40BC-A778-89E76BC981F6}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter4_QuickSort", "Chapter4_QuickSort\Chapter4_QuickSort.csproj", "{4088F633-80E4-44DF-9DE4-996A4B3E3088}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -18,5 +18,13 @@ Global {7F895D96-6E79-40BC-A778-89E76BC981F6}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.Build.0 = Release|Any CPU + {C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Release|Any CPU.Build.0 = Release|Any CPU + {4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Recursion/Program.cs b/Recursion/Program.cs deleted file mode 100644 index d0bc281..0000000 --- a/Recursion/Program.cs +++ /dev/null @@ -1,2 +0,0 @@ -Console.WriteLine("Hello, World!"); -