From 17bfd9e6c7f38f482642ab25a5c0e229de7f9e39 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 5 Sep 2022 08:05:36 +0000 Subject: [PATCH 1/2] Setting up GitHub Classroom Feedback From 4070635461f6ac6924f7585ac8762383cac7d937 Mon Sep 17 00:00:00 2001 From: lookisrr Date: Mon, 5 Sep 2022 11:09:25 +0300 Subject: [PATCH 2/2] f --- README.md | 4 ++-- src/binary_search_iterative.cpp | 22 +++++++++++++++++++++- src/binary_search_recursive.cpp | 20 ++++++++++++++++---- src/linear_search_iterative.cpp | 7 +++++-- src/linear_search_recursive.cpp | 12 +++++++++--- src/linear_search_two_pointers.cpp | 12 ++++++++++++ src/two_sum.cpp | 20 +++++++++++++++++--- 7 files changed, 82 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2545edd..fe39655 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ ## 1. Информация о студенте -**Номер группы**: 00-000 +**Номер группы**: 11-109 -**Фамилия и Имя**: Иванов Иван +**Фамилия и Имя**: Шамсутдинов Рафаэль ## 2. Описание задания diff --git a/src/binary_search_iterative.cpp b/src/binary_search_iterative.cpp index a6963fb..03589cf 100644 --- a/src/binary_search_iterative.cpp +++ b/src/binary_search_iterative.cpp @@ -2,11 +2,31 @@ namespace assignment { - std::optional BinarySearchIterative::Search(const std::vector& arr, int search_elem) const { + std::optional BinarySearchIterative::Search(const std::vector& data, int search_element) const { // Tips: // 1. Заведите две переменные: (а) индекс левой границы и (б) индекс правой границы. + int l = 0; + int r = data.size(); // 2. Поиск ведется пока индекс левой границы не превысил индекс правой. + if (r == 0){ + return std::nullopt; + } + while (l<=r){ + int m = (l+r)/2; + if ((m<0) || (m>=data.size())){ + return std::nullopt; + } + if (data[m] == search_element){ + return m; + } + else if (data[m]>search_element){ + r = m-1; + } + else { + l = m+1; + } + } // 3. Каждую итерацию вычисляйте индекс середины внутри области, задаваемой индексами левой и правой границы. // 4. Рассмотрите 3 случая: // 1) Целевой элемент равен элементу посередине. diff --git a/src/binary_search_recursive.cpp b/src/binary_search_recursive.cpp index 07cc0a2..dcc0202 100644 --- a/src/binary_search_recursive.cpp +++ b/src/binary_search_recursive.cpp @@ -7,14 +7,26 @@ namespace assignment { std::optional BinarySearchRecursive::Search(const std::vector& arr, int search_elem) const { - // запускаем рекурсивный метод с границами поиска от начала массива до конца return search(arr, search_elem, 0, static_cast(arr.size() - 1)); } - std::optional BinarySearchRecursive::search(const std::vector& arr, int search_elem, int start, int stop) const { + std::optional BinarySearchRecursive::search(const std::vector& arr, int search_elem, int left_index, int right_index) const { + int mid_index = (left_index + right_index) / 2; + if (left_index > right_index){ + return std::nullopt; + } + if (arr[mid_index] == search_elem){ + return mid_index; + } + if (arr[mid_index] > search_elem) { + return search(arr, search_elem, left_index, mid_index - 1); + } + if (arr[mid_index] < search_elem) { + return search(arr, search_elem, mid_index + 1, right_index); + } // Tips: - // 1. Рассмотрите базовые случаи выхода и рекурсии: + // 1. Рассмотрите базовые случаи выхода из рекурсии: // 1) индекс левого элемента стал больше индекса правого элемента // 2) целевой элемент найден // 2. Вызовите рекурсивный метод, изменив границы поиска @@ -23,4 +35,4 @@ namespace assignment { return std::nullopt; } -} // namespace assignment +} // namespace assignment \ No newline at end of file diff --git a/src/linear_search_iterative.cpp b/src/linear_search_iterative.cpp index ebc9475..a3bb3e0 100644 --- a/src/linear_search_iterative.cpp +++ b/src/linear_search_iterative.cpp @@ -4,8 +4,11 @@ namespace assignment { std::optional LinearSearchIterative::Search(const std::vector& arr, int search_elem) const { - // Tips: итеративно пройдитесь по элементам массива - + for (int i = 0; i LinearSearchRecursive::search(const std::vector& arr, int search_elem, int curr_index) const { + if (curr_index < 0) { + return std::nullopt; + } + + if (arr[curr_index] == search_elem) { + return curr_index; + } + + return search(arr, search_elem, curr_index - 1); // Tips: // 1. Укажите случаи выхода из рекурсии: (а) обошли все элементы и (б) элемент найден. // 2. Вызовите рекурсивный метод с другим индексом. - - return std::nullopt; } - } // namespace assignment \ No newline at end of file diff --git a/src/linear_search_two_pointers.cpp b/src/linear_search_two_pointers.cpp index ae521fa..2165912 100644 --- a/src/linear_search_two_pointers.cpp +++ b/src/linear_search_two_pointers.cpp @@ -9,6 +9,18 @@ namespace assignment { // 2. Проверяйте наличие целевого элемента по индексам. // 3. Обновляйте индексы пока левый не станет больше правого. + int left = 0; + int right = static_cast(arr.size() - 1); + while (left <= right) { + if (arr[left] == search_elem) { + return left; + } + if (arr[right] == search_elem) { + return right; + } + left++; + right--; + } return std::nullopt; } diff --git a/src/two_sum.cpp b/src/two_sum.cpp index 50bb41b..76cd32f 100644 --- a/src/two_sum.cpp +++ b/src/two_sum.cpp @@ -2,10 +2,24 @@ namespace assignment { - std::optional> two_sum(const std::vector& arr, int sum) { - + std::optional> two_sum(const std::vector& data, int sum) { + if (data.empty()) { + return std::nullopt; + } // Tips: для создания пары (pair) используйте функцию std::make_pair - + int left = 0; + int right = static_cast(data.size() - 1); + while (data[left] + data[right] != sum and left != right) { + if (data[left] + data[right] < sum) { + left++; + } + else { + right--; + } + } + if (left != right){ + return std::make_pair(left, right); + } return std::nullopt; }