From 52ff5e8c9215705acedb21c181b3baaf14d6aaf2 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Sat, 31 Aug 2024 11:30:18 +0530 Subject: [PATCH] fix: add to search/** --- search/binary_search.cpp | 1 + search/exponential_search.cpp | 3 +- search/floyd_cycle_detection_algo.cpp | 2 +- search/interpolation_search.cpp | 1 + search/saddleback_search.cpp | 51 ++++++++++++++++----------- search/sublist_search.cpp | 1 + 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index 5e455dd17..20c8b1ab1 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -36,6 +36,7 @@ #include /// for std::sort function #include /// for std::assert +#include /// for integral typedefs #include /// for IO operations #include /// for std::vector /****************************************************************************** diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index f57cbf96b..c8370580a 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -14,7 +14,8 @@ */ #include #include -#include +#include +#include /// for integral typedefs #ifdef _MSC_VER #include // use for MS Visual C++ #else diff --git a/search/floyd_cycle_detection_algo.cpp b/search/floyd_cycle_detection_algo.cpp index c7dd95aea..4b4a34c31 100644 --- a/search/floyd_cycle_detection_algo.cpp +++ b/search/floyd_cycle_detection_algo.cpp @@ -12,9 +12,9 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for IO operations #include /// for std::vector - /** * @namespace search * @brief Search algorithms diff --git a/search/interpolation_search.cpp b/search/interpolation_search.cpp index 5ed757749..f2b0205ea 100644 --- a/search/interpolation_search.cpp +++ b/search/interpolation_search.cpp @@ -31,6 +31,7 @@ #include /// for std::sort function #include /// for std::assert +#include /// for integral typedefs #include /// for IO operations #include /// for std::vector diff --git a/search/saddleback_search.cpp b/search/saddleback_search.cpp index dab5adcf0..4f2a5606d 100644 --- a/search/saddleback_search.cpp +++ b/search/saddleback_search.cpp @@ -1,28 +1,34 @@ /** * @file - * @brief Implementation of [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array) for 2D arrays. + * @brief Implementation of [Saddleback + * Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array) + * for 2D arrays. * * @details * Saddleback Algorithm is an algorithm that searches 2D array in linear time, - * i.e, O(m + n), where m is number of rows and n is number of columns of 2D array. Also, each row and - * column of the matrix should be sorted beforehand for this algorithm to work. + * i.e, O(m + n), where m is number of rows and n is number of columns of 2D + * array. Also, each row and column of the matrix should be sorted beforehand + * for this algorithm to work. * * @author [Hashir Niazi](https://github.com/HashirGJ8842) */ -#include /// for assert -#include /// for io operations, and std::pair -#include /// for std::vector +#include /// for assert +#include /// for integral typedefs +#include /// for io operations, and std::pair +#include /// for std::vector /** \namespace search * \brief Algorithms for searching */ namespace search { /** \namespace saddleback - * \brief Function for implementing [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array). + * \brief Function for implementing [Saddleback + * Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array). */ namespace saddleback { /** - * This function implements [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array), + * This function implements [Saddleback + * Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array), * on a sorted 2D array, and finds the location of the element needed to search * @param matrix 2D matrix which is sorted on the basis of rows and columns * @param element element to be searched @@ -30,16 +36,17 @@ namespace saddleback { * element is present. * @return An std::pair with (0, 0), if the element is not present. */ -std::pair saddleback(std::vector> matrix, - int32_t element) { +std::pair saddleback( + std::vector> matrix, int32_t element) { uint32_t left_index = 0; uint32_t right_index = matrix[0].size() - 1; // Start from top right corner - while (left_index < matrix.size()) { // Exit once the value of indexes get out of range. + while (left_index < + matrix.size()) { // Exit once the value of indexes get out of range. if (element == matrix[left_index] [right_index]) { // If value on this position of matrix is // equal to element, return (row, column). - return std::make_pair(left_index+1, right_index+1); + return std::make_pair(left_index + 1, right_index + 1); } else if (element > matrix[left_index] [right_index]) { // Else if value on this position of @@ -51,14 +58,15 @@ std::pair saddleback(std::vector> matri [right_index]) { // Else if value on this position of // matrix is greater than the // element, move down. - if(!right_index) + if (!right_index) break; - else --right_index; + else + --right_index; } } return std::make_pair( 0, 0); // If the program reaches here, that means one of the index - // went out of index, hence no element present. + // went out of index, hence no element present. } } // namespace saddleback } // namespace search @@ -69,15 +77,16 @@ std::pair saddleback(std::vector> matri */ static void test() { std::vector> matrix = {{1, 10, 100, 1000, 10000}, - {2, 20, 200, 2000, 20000}, - {3, 30, 300, 3000, 30000}, - {4, 40, 400, 4000, 40000}, - {5, 50, 500, 5000, 50000}}; + {2, 20, 200, 2000, 20000}, + {3, 30, 300, 3000, 30000}, + {4, 40, 400, 4000, 40000}, + {5, 50, 500, 5000, 50000}}; std::pair not_found = std::make_pair(0, 0); std::pair test_answer; // Test 1 - std::pair answer1 = search::saddleback::saddleback(matrix, 123); + std::pair answer1 = + search::saddleback::saddleback(matrix, 123); assert(not_found == answer1); // Test 2 answer1 = search::saddleback::saddleback(matrix, 0); @@ -101,6 +110,6 @@ static void test() { * @returns 0 on exit */ int main() { - test(); // execute the tests + test(); // execute the tests return 0; } diff --git a/search/sublist_search.cpp b/search/sublist_search.cpp index 0954173d2..7b485f041 100644 --- a/search/sublist_search.cpp +++ b/search/sublist_search.cpp @@ -26,6 +26,7 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for IO operations #include /// for std::vector