diff --git a/CMakeLists.txt b/CMakeLists.txt index f2940c962..aafeb69d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ find_package(Doxygen OPTIONAL_COMPONENTS dot dia) if(DOXYGEN_FOUND) set(DOXYGEN_GENERATE_MAN NO) set(DOXYGEN_USE_MATHJAX YES) + set(DOXYGEN_FILE_PATTERNS *.cpp *.h *.hpp *.md) set(DOXYGEN_GENERATE_HTML YES) set(DOXYGEN_INLINE_SOURCES YES) set(DOXYGEN_CREATE_SUBDIRS YES) @@ -50,6 +51,7 @@ endif() add_subdirectory(math) add_subdirectory(others) +add_subdirectory(search) add_subdirectory(strings) add_subdirectory(sorting) add_subdirectory(computer_oriented_statistical_methods) diff --git a/search/CMakeLists.txt b/search/CMakeLists.txt new file mode 100644 index 000000000..5fd1ae591 --- /dev/null +++ b/search/CMakeLists.txt @@ -0,0 +1,18 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. RELATIVE may makes it easier to extract an executable name +# automatically. +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) +foreach( testsourcefile ${APP_SOURCES} ) + # I used a simple string replace, to cut off .cpp. + string( REPLACE ".cpp" "" testname ${testsourcefile} ) + add_executable( ${testname} ${testsourcefile} ) + + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) + if(OpenMP_CXX_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_CXX) + endif() + install(TARGETS ${testname} DESTINATION "bin/search") + +endforeach( testsourcefile ${APP_SOURCES} ) diff --git a/search/Linear Search.cpp b/search/Linear Search.cpp deleted file mode 100644 index 97e820437..000000000 --- a/search/Linear Search.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -using namespace std; - -int LinearSearch(int *array, int size, int key) -{ - for (int i = 0; i < size; ++i) - { - if (array[i] == key) - { - return i; - } - } - - return -1; -} - -int main() -{ - int size; - cout << "\nEnter the size of the Array : "; - cin >> size; - - int array[size]; - int key; - - //Input array - cout << "\nEnter the Array of " << size << " numbers : "; - for (int i = 0; i < size; i++) - { - cin >> array[i]; - } - - cout << "\nEnter the number to be searched : "; - cin >> key; - - int index = LinearSearch(array, size, key); - if (index != -1) - { - cout << "\nNumber found at index : " << index; - } - else - { - cout << "\nNot found"; - } - - return 0; -} diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index b8343fa02..5f9e64217 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -1,23 +1,28 @@ // Copyright 2020 Divide-et-impera-11 -#include +#include +#include #include #include -using namespaces std; + // Binary Search Algorithm(use by struzik algorithm) // Time Complexity O(log n) where 'n' is the number of elements // Worst Time Complexity O(log n) // Best Time Complexity Ω(1) // Space Complexity O(1) // Auxiliary Space Complexity O(1) -template inline Type* binary_s(Type *array, size_t size, Type key) { -int32_t lower_index(0), upper_index(size - 1), middle_index; -while (lower_index <= upper_index) { - middle_index = floor((lower_index + upper_index) / 2); - if (*(array + middle_index) < key) lower_index = (middle_index + 1); - else if (*(array + middle_index) > key)upper_index = (middle_index - 1); - else return (array + middle_index); - } -return nullptr; +template +inline Type* binary_s(Type* array, size_t size, Type key) { + int32_t lower_index(0), upper_index(size - 1), middle_index; + while (lower_index <= upper_index) { + middle_index = std::floor((lower_index + upper_index) / 2); + if (*(array + middle_index) < key) + lower_index = (middle_index + 1); + else if (*(array + middle_index) > key) + upper_index = (middle_index - 1); + else + return (array + middle_index); + } + return nullptr; } // Struzik Search Algorithm(Exponential) // Time Complexity O(log i)where i is the position of search key in the list @@ -32,25 +37,27 @@ If the key is larger than the last element of array, the start of block(block_front) will be equal to the end of block(block_size) and the algorithm return null ponter, every other cases the algoritm return fom the loop. */ -template Type* struzik_search(Type* array, size_t size, Type key) { - uint32_t block_front(0), block_size = size == 0 ? 0 : 1; - while (block_front != block_size) { +template +Type* struzik_search(Type* array, size_t size, Type key) { + uint32_t block_front(0), block_size = size == 0 ? 0 : 1; + while (block_front != block_size) { if (*(array + block_size - 1) < key) { - block_front = block_size; - (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; - continue; + block_front = block_size; + (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; + continue; } - return binary_s(array + block_front, (block_size - block_front), key); - } -return nullptr; + return binary_s(array + block_front, (block_size - block_front), + key); + } + return nullptr; } int main() { -// TEST CASES -int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; -assert(struzik_search(sorted_array, 7, 0) == nullptr); -assert(struzik_search(sorted_array, 7, 1000) == nullptr); -assert(struzik_search(sorted_array, 7, 50) == nullptr); -assert(struzik_search(sorted_array, 7, 7) == sorted_array); -// TEST CASES -return 0; + // TEST CASES + int* sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; + assert(struzik_search(sorted_array, 7, 0) == nullptr); + assert(struzik_search(sorted_array, 7, 1000) == nullptr); + assert(struzik_search(sorted_array, 7, 50) == nullptr); + assert(struzik_search(sorted_array, 7, 7) == sorted_array); + // TEST CASES + return 0; } diff --git a/search/Interpolation Search.cpp b/search/interpolation_search2.cpp similarity index 50% rename from search/Interpolation Search.cpp rename to search/interpolation_search2.cpp index f52ce4f4f..0e6d3b79d 100644 --- a/search/Interpolation Search.cpp +++ b/search/interpolation_search2.cpp @@ -1,31 +1,30 @@ #include -int InterpolationSearch(int A[], int n, int x) -{ +int InterpolationSearch(int A[], int n, int x) { int low = 0; int high = n - 1; - while (low <= high) - { + while (low <= high) { int mid = low + (((high - 1) * (x - A[low])) / (A[high] - A[low])); if (x == A[mid]) - return mid; // Found x, return (exit) + return mid; // Found x, return (exit) else if (x < A[mid]) - high = mid - 1; // X lies before mid + high = mid - 1; // X lies before mid else - low = mid + 1; // x lies after mid + low = mid + 1; // x lies after mid } return -1; } -int main() -{ +int main() { int A[] = {2, 4, 5, 7, 13, 14, 15, 23}; int x = 17; - int index = InterpolationSearch(A, 8, x); // passed array A inside the InterpolationSearch function + int index = InterpolationSearch( + A, 8, x); // passed array A inside the InterpolationSearch function if (index != -1) std::cout << "Number " << x << " is at " << index; else std::cout << "Number " << x << " not found"; } -// randomly set x bcoz array was defined by us , therefore not reasonable for asking input. -// We could have asked for input if array elements were inputed by the user. +// randomly set x bcoz array was defined by us , therefore not reasonable for +// asking input. We could have asked for input if array elements were inputed by +// the user. diff --git a/search/jump_search.cpp b/search/jump_search.cpp index 0ee7e4e00..aa2ee0bdb 100644 --- a/search/jump_search.cpp +++ b/search/jump_search.cpp @@ -1,47 +1,40 @@ // C++ program to implement Jump Search -#include -using namespace std; +#include +#include +#include -int jumpSearch(int arr[], int x, int n) -{ +int jumpSearch(int arr[], int x, int n) { // Finding block size to be jumped - int step = sqrt(n); + int step = std::sqrt(n); // Finding the block where element is // present (if it is present) int prev = 0; - while (arr[min(step, n)-1] < x) - { + while (arr[std::min(step, n) - 1] < x) { prev = step; - step += sqrt(n); - if (prev >= n) - return -1; + step += std::sqrt(n); + if (prev >= n) return -1; } // Doing a linear search for x in block // beginning with prev. - while (arr[prev] < x) - { + while (arr[prev] < x) { prev++; // If we reached next block or end of // array, element is not present. - if (prev == min(step, n)) - return -1; + if (prev == std::min(step, n)) return -1; } // If element is found - if (arr[prev] == x) - return prev; + if (arr[prev] == x) return prev; return -1; } // Driver program to test function -int main() -{ - int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, - 34, 55, 89, 144, 233, 377, 610 }; +int main() { + int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}; int x = 55; int n = sizeof(arr) / sizeof(arr[0]); @@ -49,6 +42,6 @@ int main() int index = jumpSearch(arr, x, n); // Print the index where 'x' is located - cout << "\nNumber " << x << " is at index " << index; + std::cout << "\nNumber " << x << " is at index " << index; return 0; } diff --git a/search/linear_search.cpp b/search/linear_search.cpp new file mode 100644 index 000000000..8849549e5 --- /dev/null +++ b/search/linear_search.cpp @@ -0,0 +1,39 @@ +#include + +int LinearSearch(int *array, int size, int key) { + for (int i = 0; i < size; ++i) { + if (array[i] == key) { + return i; + } + } + + return -1; +} + +int main() { + int size; + std::cout << "\nEnter the size of the Array : "; + std::cin >> size; + + int *array = new int[size]; + int key; + + // Input array + std::cout << "\nEnter the Array of " << size << " numbers : "; + for (int i = 0; i < size; i++) { + std::cin >> array[i]; + } + + std::cout << "\nEnter the number to be searched : "; + std::cin >> key; + + int index = LinearSearch(array, size, key); + if (index != -1) { + std::cout << "\nNumber found at index : " << index; + } else { + std::cout << "\nNot found"; + } + + delete[] array; + return 0; +} diff --git a/search/median_search.cpp b/search/median_search.cpp index be95b599f..fa8ef3826 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -1,72 +1,57 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; -vectorv; -vectors1; -vectors2; -vectors3; +#include +#include +#include +#include +#include +#include +#include + +std::vector v; +std::vector s1; +std::vector s2; +std::vector s3; + template -void comp(X x) -{ - if(s1.size()>=x && s1.size()+s2.size()x) - { - sort(s1.begin(),s1.end()); - cout<x) - { - sort(s3.begin(),s3.end()); - cout<= x && s1.size() + s2.size() < x) { + std::cout << s2[0] << " is the " << x + 1 << "th element from front"; + } else if (s1.size() > x) { + std::sort(s1.begin(), s1.end()); + std::cout << s1[x] << " is the " << x + 1 << "th element from front"; + } else if (s1.size() + s2.size() <= x && s3.size() > x) { + std::sort(s3.begin(), s3.end()); + std::cout << s3[x - s1.size() - s2.size()] << " is the " << x + 1 + << "th element from front"; + } else { + std::cout << x + 1 << " is invalid location"; } } -int main() -{ - for(int i=0;i<1000;i++) - { - v.push_back(rand()%1000); +int main() { + for (int i = 0; i < 1000; i++) { + v.push_back(std::rand() % 1000); } - for(int r:v) - { - cout<>x; - comp(x-1); + std::cout << "enter the no. to be searched form begining:- "; + std::cin >> x; + comp(x - 1); return 0; } diff --git a/search/searching.cpp b/search/searching.cpp index 6a9dcac7d..d01619738 100644 --- a/search/searching.cpp +++ b/search/searching.cpp @@ -1,38 +1,32 @@ +#include #include #include -#include -using namespace std; char paragraph; -int main() -{ - string paragraph; - cout << "Please enter your paragraph: \n"; - getline(cin, paragraph); - cout << "\nHello, your paragraph is:\n " << paragraph << "!\n"; - cout << "\nThe size of your paragraph = " << paragraph.size() << " characters. \n\n"; +int main() { + std::string paragraph; + std::cout << "Please enter your paragraph: \n"; + std::getline(std::cin, paragraph); + std::cout << "\nHello, your paragraph is:\n " << paragraph << "!\n"; + std::cout << "\nThe size of your paragraph = " << paragraph.size() + << " characters. \n\n"; - if (paragraph.empty()) - { - cout << "\nThe paragraph is empty" << endl; - } - else - { - while (true) - { - string word; - cout << "Please enter the word you are searching for: "; - getline(cin, word); - cout << "Hello, your word is " << word << "!\n"; - if (paragraph.find(word) == string::npos) - { - cout << word << " does not exist in the sentence" << endl; - } - else - { - cout << "The word " << word << " is now found at location " << paragraph.find(word) << endl - << endl; + if (paragraph.empty()) { + std::cout << "\nThe paragraph is empty" << std::endl; + } else { + while (true) { + std::string word; + std::cout << "Please enter the word you are searching for: "; + std::getline(std::cin, word); + std::cout << "Hello, your word is " << word << "!\n"; + if (paragraph.find(word) == std::string::npos) { + std::cout << word << " does not exist in the sentence" + << std::endl; + } else { + std::cout << "The word " << word << " is now found at location " + << paragraph.find(word) << std::endl + << std::endl; } system("pause"); } diff --git a/search/ternary_search.cpp b/search/ternary_search.cpp index 4cb8d19d9..4d1918d48 100644 --- a/search/ternary_search.cpp +++ b/search/ternary_search.cpp @@ -3,16 +3,15 @@ * It does this by dividing the search space by 3 parts and * using its property (usually monotonic property) to find * the desired index. - * + * * Time Complexity : O(log3 n) * Space Complexity : O(1) (without the array) */ #include -using namespace std; /* - * The absolutePrecision can be modified to fit preference but + * The absolutePrecision can be modified to fit preference but * it is recommended to not go lower than 10 due to errors that * may occur. * @@ -30,99 +29,89 @@ int A[MAX] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 10}; /* * get_input function is to receive input from standard IO */ -void get_input() -{ - // TODO: Get input from STDIO or write input to memory as done above. +void get_input() { + // TODO(christianbender): Get input from STDIO or write input to memory as + // done above. } /* - * This is the iterative method of the ternary search which returns the index of the element. + * This is the iterative method of the ternary search which returns the index of + * the element. */ -int it_ternary_search(int left, int right, int A[], int target) -{ - while (1) - { - if (left < right) - { - if (right - left < absolutePrecision) - { - for (int i = left; i <= right; i++) - if (A[i] == target) - return i; +int it_ternary_search(int left, int right, int A[], int target) { + while (1) { + if (left < right) { + if (right - left < absolutePrecision) { + for (int i = left; i <= right; i++) + if (A[i] == target) return i; - return -1; - } + return -1; + } - int oneThird = (left + right) / 3 + 1; - int twoThird = (left + right) * 2 / 3 + 1; + int oneThird = (left + right) / 3 + 1; + int twoThird = (left + right) * 2 / 3 + 1; - if (A[oneThird] == target) - return oneThird; - else if (A[twoThird] == target) - return twoThird; + if (A[oneThird] == target) + return oneThird; + else if (A[twoThird] == target) + return twoThird; - else if (target > A[twoThird]) - left = twoThird + 1; - else if (target < A[oneThird]) - right = oneThird - 1; + else if (target > A[twoThird]) + left = twoThird + 1; + else if (target < A[oneThird]) + right = oneThird - 1; - else - left = oneThird + 1, right = twoThird - 1; + else + left = oneThird + 1, right = twoThird - 1; + } else { + return -1; + } } - else - return -1; - } } -/* - * This is the recursive method of the ternary search which returns the index of the element. +/* + * This is the recursive method of the ternary search which returns the index of + * the element. */ -int rec_ternary_search(int left, int right, int A[], int target) -{ - if (left < right) - { - if (right - left < absolutePrecision) - { - for (int i = left; i <= right; i++) - if (A[i] == target) - return i; +int rec_ternary_search(int left, int right, int A[], int target) { + if (left < right) { + if (right - left < absolutePrecision) { + for (int i = left; i <= right; i++) + if (A[i] == target) return i; - return -1; + return -1; + } + + int oneThird = (left + right) / 3 + 1; + int twoThird = (left + right) * 2 / 3 + 1; + + if (A[oneThird] == target) return oneThird; + if (A[twoThird] == target) return twoThird; + + if (target < A[oneThird]) + return rec_ternary_search(left, oneThird - 1, A, target); + if (target > A[twoThird]) + return rec_ternary_search(twoThird + 1, right, A, target); + + return rec_ternary_search(oneThird + 1, twoThird - 1, A, target); + } else { + return -1; } - - int oneThird = (left + right) / 3 + 1; - int twoThird = (left + right) * 2 / 3 + 1; - - if (A[oneThird] == target) - return oneThird; - if (A[twoThird] == target) - return twoThird; - - if (target < A[oneThird]) - return rec_ternary_search(left, oneThird - 1, A, target); - if (target > A[twoThird]) - return rec_ternary_search(twoThird + 1, right, A, target); - - return rec_ternary_search(oneThird + 1, twoThird - 1, A, target); - } - else - return -1; } /* * ternary_search is a template function - * You could either use it_ternary_search or rec_ternary_search according to preference. + * You could either use it_ternary_search or rec_ternary_search according to + * preference. */ -void ternary_search(int N, int A[], int target) -{ - cout << it_ternary_search(0, N - 1, A, target) << '\t'; - cout << rec_ternary_search(0, N - 1, A, target) << '\t'; - cout << '\n'; +void ternary_search(int N, int A[], int target) { + std::cout << it_ternary_search(0, N - 1, A, target) << '\t'; + std::cout << rec_ternary_search(0, N - 1, A, target) << '\t'; + std::cout << std::endl; } -int main() -{ - get_input(); - ternary_search(N, A, _target); - return 0; +int main() { + get_input(); + ternary_search(N, A, _target); + return 0; }