From 186c7a2de85adc61cd7d2ae6b83d51ee21616fd1 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 19:37:21 -0400 Subject: [PATCH 01/17] add missing free for dynamic memory allocated --- search/binary_search.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index 9933c9816..d8a3a6631 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -30,5 +30,7 @@ int main(int argc, char const* argv[]) { std::cout << key << " found at index " << res << std::endl; else std::cout << key << " not found" << std::endl; + + delete[] a; return 0; } From 94f3ffe146f7f2daa4a8c4e8498d587e6ee27876 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 19:37:33 -0400 Subject: [PATCH 02/17] document binary search --- search/binary_search.cpp | 56 +++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index d8a3a6631..66da31d7f 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -1,35 +1,55 @@ +/** + * @file + * @brief [Binary search + * algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm) + */ #include -// binary_search function -int binary_search(int a[], int l, int r, int key) { + +/** binary_search function + * \param [in] a array to sort + * \param [in] r right hand limit = \f$n-1\f$ + * \param [in] key value to find + * \returns index if T is found + * \return -1 if T is not found + */ +int binary_search(int a[], int r, int key) { + int l = 0; + while (l <= r) { - int m = l + (r - l) / 2; - if (key == a[m]) - return m; - else if (key < a[m]) - r = m - 1; - else - l = m + 1; - } - return -1; - } + int m = l + (r - l) / 2; + if (key == a[m]) + return m; + else if (key < a[m]) + r = m - 1; + else + l = m + 1; + } + return -1; +} + +/** main function */ int main(int argc, char const* argv[]) { int n, key; std::cout << "Enter size of array: "; std::cin >> n; std::cout << "Enter array elements: "; + int* a = new int[n]; -// this loop use for store value in Array + + // this loop use for store value in Array for (int i = 0; i < n; i++) { std::cin >> a[i]; - } + } + std::cout << "Enter search key: "; std::cin >> key; -// this is use for find value in given array - int res = binary_search(a, 0, n - 1, key); + + // this is use for find value in given array + int res = binary_search(a, n - 1, key); if (res != -1) - std::cout << key << " found at index " << res << std::endl; + std::cout << key << " found at index " << res << std::endl; else - std::cout << key << " not found" << std::endl; + std::cout << key << " not found" << std::endl; delete[] a; return 0; From 605fa140cc34caa6cc1a9c00c6b40b9a89fc2929 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 19:54:24 -0400 Subject: [PATCH 03/17] free dynamically allocated memory --- search/exponential_search.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index 5f9e64217..79f09e38b 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -59,5 +59,6 @@ int main() { assert(struzik_search(sorted_array, 7, 50) == nullptr); assert(struzik_search(sorted_array, 7, 7) == sorted_array); // TEST CASES + delete[] sorted_array; return 0; } From 969e916871f3e168506c8968008fded8122e7a74 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 19:54:37 -0400 Subject: [PATCH 04/17] document exponential search --- search/exponential_search.cpp | 62 +++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index 79f09e38b..f57cbf96b 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -1,20 +1,42 @@ -// Copyright 2020 Divide-et-impera-11 +/** + * \file + * \brief [Exponential search + * algorithm](https://en.wikipedia.org/wiki/Exponential_search) + * \copyright 2020 Divide-et-impera-11 + * + * The algorithm try to search the range where the key should be. + * If it has been found we do a binary search there. + * The range of the search grows by exponential every time. + * 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. + */ #include #include #include -#include +#ifdef _MSC_VER +#include // use for MS Visual C++ +#else +#include // for all other compilers +#endif -// 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) +/** Binary Search Algorithm (used by ::struzik_search)\n + * * 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) + * \returns pointer to value in the array + * \returns `nullptr` if value not found + */ 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) @@ -22,21 +44,17 @@ inline Type* binary_s(Type* array, size_t size, Type key) { 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 -// Worst Time Complexity O(log i) -// Best Time Complexity Ω(1) -// Space Complexity O(1) -// Auxiliary Space Complexity O(1) -/* Tha algorithm try to search the range where the key should be. -If it has been found we do a binary search there. -The range of the search grows by exponential every time. -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. */ + +/** Struzik Search Algorithm(Exponential) + * * Time Complexity O(log i) where i is the position of search key in the list + * * Worst Time Complexity O(log i) + * * Best Time Complexity Ω(1) + * * Space Complexity O(1) + * * Auxiliary Space Complexity O(1) + */ template Type* struzik_search(Type* array, size_t size, Type key) { uint32_t block_front(0), block_size = size == 0 ? 0 : 1; @@ -51,6 +69,8 @@ Type* struzik_search(Type* array, size_t size, Type key) { } return nullptr; } + +/** Main function */ int main() { // TEST CASES int* sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; From d9ffc8aca168cade7093d13501c09b8d616a2df3 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 20:24:44 -0400 Subject: [PATCH 05/17] document hash search --- search/hash_search.cpp | 147 ++++++++++++++++++++++++++--------------- 1 file changed, 93 insertions(+), 54 deletions(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index 94d87b58a..6e4caffc3 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -1,100 +1,139 @@ -// Copyright 2020 Arctic2333 -#include -#include -#define MAX 6 // Determines how much data -# define HASHMAX 5 // Determines the length of the hash table /** - * Hash Search Algorithm - * Best Time Complexity Ω(1) - * In this algorithm, we use the method of division and reservation remainder to construct the hash function, - * and use the method of chain address to solve the conflict, that is, we link a chain list after the data, - * and store all the records whose keywords are synonyms in the same linear chain list. */ -int data[MAX] = { 1, 10, 15, 5, 8, 7}; // test data + * \file + * \brief Hash Search Algorithm - Best Time Complexity Ω(1) + * + * \copyright 2020 Arctic2333 + * + * In this algorithm, we use the method of division and reservation remainder to + * construct the hash function, and use the method of chain address to solve the + * conflict, that is, we link a chain list after the data, and store all the + * records whose keywords are synonyms in the same linear chain list. + * + * @warning This program is only for educational purposes. It has serious flaws + * in implementation with regards to memory management resulting in large + * amounts of memory leaks. + * @todo fix the program for memory leaks and better structure in C++ and not C + * fashion + */ +#include +#include + +#define MAX 6 ///< Determines how much data +#define HASHMAX 5 ///< Determines the length of the hash table + +int data[MAX] = {1, 10, 15, 5, 8, 7}; //!< test data + +/** + * a one-way linked list + */ typedef struct list { - int key; - struct list * next; -} -node, * link; -node hashtab[HASHMAX]; -int counter = 1; -/* int h(int key) + int key; //!< key value for node + struct list* next; //!< pointer to next link in the chain +} node, /**< define node as one item list */ + *link; ///< pointer to nodes + +node hashtab[HASHMAX]; ///< array of nodes + +// int counter = 1; + +/** * Mode of hash detection : - * Division method */ -int h(int key) { - return key % HASHMAX; -} -/* void create_list(int key) + * Division method + * \param [in] key to hash + * \returns hash value for `key` + */ +int h(int key) { return key % HASHMAX; } + +/** * The same after the remainder will be added after the same hash header * To avoid conflict, zipper method is used - * Insert elements into the linked list in the header */ + * Insert elements into the linked list in the header + * \param [in] key key to add to list + * \warning dynamic memory allocated to `n` never gets freed. + * \todo fix memory leak + */ void create_list(int key) { // Construct hash table link p, n; int index; - n = (link) malloc(sizeof(node)); - n -> key = key; - n -> next = NULL; + n = (link)malloc(sizeof(node)); + n->key = key; + n->next = NULL; index = h(key); p = hashtab[index].next; if (p != NULL) { - n -> next = p; + n->next = p; hashtab[index].next = n; } else { - hashtab[index].next = n; } + hashtab[index].next = n; + } } -/* int hash_search(int key) - * Input the key to be searched, and get the hash header position through the H (int key) function, - * then one-dimensional linear search. - * If found @return element depth and number of searches - * If not found @return -1 */ -int hash_search(int key) { // Hash lookup function + +/** + * Input the key to be searched, and get the hash header position through the H + * (int key) function, then one-dimensional linear search. If found @return + * element depth and number of searches If not found @return -1 + */ +int hash_search(int key, int* counter) { // Hash lookup function link pointer; int index; - counter = 0; + + *counter = 0; index = h(key); pointer = hashtab[index].next; - printf("data[%d]:", index); + + std::cout << "data[" << index << "]:"; + while (pointer != NULL) { - counter++; - printf("data[%d]:", pointer -> key); - if (pointer -> key == key) + counter[0]++; + std::cout << "data[" << pointer->key << "]:"; + if (pointer->key == key) return 1; else - pointer = pointer -> next; + pointer = pointer->next; } + return 0; } + +/** main function */ int main() { link p; - int key, index, i; // Key is the value to be found + int key, index, i, counter; // Key is the value to be found index = 0; + // You can write the input mode here while (index < MAX) { // Construct hash table create_list(data[index]); index++; } + for (i = 0; i < HASHMAX; i++) { // Output hash table - printf("hashtab [%d]", i); - printf("\n"); + std::cout << "hashtab [" << i << "]\n"; + p = hashtab[i].next; + while (p != NULL) { - printf("please int key:"); - if (p -> key > 0) - printf("[%d]", p -> key); - p = p -> next; + std::cout << "please int key:"; + if (p->key > 0) + std::cout << "[" << p->key << "]"; + p = p->next; } - printf("\n"); + std::cout << std::endl; } + while (key != -1) { // You can write the input mode here // test key = 10 key = 10; - if (hash_search(key)) - printf("search time = %d\n", counter); + if (hash_search(key, &counter)) + std::cout << "search time = " << counter << std::endl; else - printf("no found!\n"); + std::cout << "no found!\n"; key = -1; // Exit test - /* The test sample is returned as: data[0]:data[5]:data[15]:data[10]:search time = 3 - * The search is successful. There are 10 in this set of data */ + /* The test sample is returned as: + * data[0]:data[5]:data[15]:data[10]:search time = 3 The search is + * successful. There are 10 in this set of data */ } + return 0; } From 7fd12991f74280018724920d73761ef6328b51b8 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 20:31:41 -0400 Subject: [PATCH 06/17] document interpolation search --- search/interpolation_search.cpp | 89 +++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/search/interpolation_search.cpp b/search/interpolation_search.cpp index afa9e7c50..e12dbd157 100644 --- a/search/interpolation_search.cpp +++ b/search/interpolation_search.cpp @@ -1,36 +1,61 @@ -#include +/** + * \file + * \brief [Interpolation + * search](https://en.wikipedia.org/wiki/Interpolation_search) algorithm + */ +#include -// function to search the value in an array using interpolation search -int search(int arr[], int value, int len) { - int low = 0, high, mid; - high = len-1; - while (arr[low] <= value && arr[high] >= value) { - mid = (low + ((value-arr[low])*(high-low)) / (arr[high]-arr[low])); - if (arr[mid] > value) - high = mid-1; - else if (arr[mid] < value) - low = mid+1; - else - return mid; - } - if (arr[low] == value) - return low; - return 0; +/** function to search the value in an array using interpolation search + * \param [in] arr array to search in + * \param [in] value value to search for + * \param [in] len length of array + * \returns index where the value is found + * \returns 0 if not found + */ +int interpolation_search(int arr[], int value, int len) { + int low = 0, high, mid; + high = len - 1; + + while (arr[low] <= value && arr[high] >= value) { + mid = (low + + ((value - arr[low]) * (high - low)) / (arr[high] - arr[low])); + if (arr[mid] > value) + high = mid - 1; + else if (arr[mid] < value) + low = mid + 1; + else + return mid; + } + + if (arr[low] == value) + return low; + + return 0; } +/** main function */ int main() { - int n, value, array[100], re; - std::cout << "Enter the size of array(less than 100) : "; - std::cin >> n; - std::cout << "array in ascending (increasing) order : " << std::endl; - for (int i=0; i < n; i++) - std::cin >> array[i]; - std::cout << "Enter the value you want to search : "; - std::cin >> value; - re = search(array, value, n); - if (re == 0) - std::cout << "Entered value is not in the array" << std::endl; - else - std::cout << "The value is at the position " << re << std::endl; - return 0; - } + int n, value, re; + + std::cout << "Enter the size of array(less than 100) : "; + std::cin >> n; + + int *array = new int[n]; + + std::cout << "array in ascending (increasing) order : " << std::endl; + + for (int i = 0; i < n; i++) std::cin >> array[i]; + + std::cout << "Enter the value you want to search : "; + std::cin >> value; + + re = interpolation_search(array, value, n); + + if (re == 0) + std::cout << "Entered value is not in the array" << std::endl; + else + std::cout << "The value is at the position " << re << std::endl; + + delete[] array; + return 0; +} From 85020eea5faf69972a8e685b7b4be80abec052c7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 20:33:25 -0400 Subject: [PATCH 07/17] change error return value for function --- search/interpolation_search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/interpolation_search.cpp b/search/interpolation_search.cpp index e12dbd157..4339dc366 100644 --- a/search/interpolation_search.cpp +++ b/search/interpolation_search.cpp @@ -30,7 +30,7 @@ int interpolation_search(int arr[], int value, int len) { if (arr[low] == value) return low; - return 0; + return -1; } /** main function */ @@ -51,7 +51,7 @@ int main() { re = interpolation_search(array, value, n); - if (re == 0) + if (re == -1) std::cout << "Entered value is not in the array" << std::endl; else std::cout << "The value is at the position " << re << std::endl; From 4bc199ff1d9eda8faeb298c88c7c9dfeeb22f9b9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 20:35:12 -0400 Subject: [PATCH 08/17] document interpolation search 2 --- search/interpolation_search2.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/search/interpolation_search2.cpp b/search/interpolation_search2.cpp index 0e6d3b79d..93fa6cd83 100644 --- a/search/interpolation_search2.cpp +++ b/search/interpolation_search2.cpp @@ -1,4 +1,17 @@ +/** + * \file + * \brief [Interpolation + * search](https://en.wikipedia.org/wiki/Interpolation_search) algorithm + */ #include + +/** function to search the value in an array using interpolation search + * \param [in] arr array to search in + * \param [in] value value to search for + * \param [in] len length of array + * \returns index where the value is found + * \returns -1 if not found + */ int InterpolationSearch(int A[], int n, int x) { int low = 0; int high = n - 1; @@ -11,18 +24,21 @@ int InterpolationSearch(int A[], int n, int x) { else low = mid + 1; // x lies after mid } + return -1; } +/** main function */ 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 - if (index != -1) - std::cout << "Number " << x << " is at " << index; + + ///< passed array A inside the InterpolationSearch function + int index = InterpolationSearch(A, 8, x); + if (index < 0) + std::cout << "Number " << x << " not found" << std::endl; else - std::cout << "Number " << x << " not found"; + std::cout << "Number " << x << " is at " << index << std::endl; } // randomly set x bcoz array was defined by us , therefore not reasonable for From 2b57d1ff881b77636b88987978ae38d62741192f Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 20:36:44 -0400 Subject: [PATCH 09/17] document jump search --- search/jump_search.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/search/jump_search.cpp b/search/jump_search.cpp index aa2ee0bdb..f7b100a4e 100644 --- a/search/jump_search.cpp +++ b/search/jump_search.cpp @@ -1,9 +1,14 @@ -// C++ program to implement Jump Search - +/** + * \file + * \brief C++ program to implement [Jump + * Search](https://en.wikipedia.org/wiki/Jump_search) + */ #include #include #include +/** jump search implementation + */ int jumpSearch(int arr[], int x, int n) { // Finding block size to be jumped int step = std::sqrt(n); @@ -14,7 +19,8 @@ int jumpSearch(int arr[], int x, int n) { while (arr[std::min(step, n) - 1] < x) { prev = step; step += std::sqrt(n); - if (prev >= n) return -1; + if (prev >= n) + return -1; } // Doing a linear search for x in block @@ -24,10 +30,12 @@ int jumpSearch(int arr[], int x, int n) { // If we reached next block or end of // array, element is not present. - if (prev == std::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; } From 5ab43ad039f9aa943e596f6e47d26a36a1fb42a0 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 20:41:31 -0400 Subject: [PATCH 10/17] document linear search --- search/linear_search.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/search/linear_search.cpp b/search/linear_search.cpp index 8849549e5..142506951 100644 --- a/search/linear_search.cpp +++ b/search/linear_search.cpp @@ -1,5 +1,18 @@ +/** + * \file + * \brief [Linear search + * algorithm](https://en.wikipedia.org/wiki/Linear_search) + */ #include +/** + * Algorithm implementation + * \param [in] array array to search in + * \param [in] size length of array + * \param [in] key key value to search for + * \returns index where the key-value occurs in the array + * \returns -1 if key-value not found + */ int LinearSearch(int *array, int size, int key) { for (int i = 0; i < size; ++i) { if (array[i] == key) { @@ -10,6 +23,7 @@ int LinearSearch(int *array, int size, int key) { return -1; } +/** main function */ int main() { int size; std::cout << "\nEnter the size of the Array : "; From 8291db4f9fb6cf119f75248c795cf56540cee3e2 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 21:06:53 -0400 Subject: [PATCH 11/17] attempted to document median search --- search/median_search.cpp | 59 +++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index fa8ef3826..2134a0dbe 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -1,18 +1,21 @@ +/** + * \file + * \brief [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm + * \warning This core is erroneous and gives invorrect answers. Tested using + * cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) + * \ingroup median search + * \{ + */ #include -#include -#include #include -#include -#include #include -std::vector v; -std::vector s1; -std::vector s2; -std::vector s3; - +/** + * @todo add documentation + */ template -void comp(X x) { +void comp(X x, std::vector &s1, std::vector &s2, + std::vector &s3) { if (s1.size() >= x && s1.size() + s2.size() < x) { std::cout << s2[0] << " is the " << x + 1 << "th element from front"; } else if (s1.size() > x) { @@ -26,17 +29,32 @@ void comp(X x) { std::cout << x + 1 << " is invalid location"; } } + +#define MAX_NUM 20 ///< maximum number of values to sort from + +/** + * Main function + */ int main() { - for (int i = 0; i < 1000; i++) { - v.push_back(std::rand() % 1000); - } - for (int r : v) { - std::cout << r << " "; - } - int median = std::rand() % 1000; + std::vector v{25, 21, 98, 100, 76, 22, 43, 60, 89, 87}; + std::vector s1; + std::vector s2; + std::vector s3; + + // creates an array of random numbers + // for (int i = 0; i < MAX_NUM; i++) { + // int r = std::rand() % 1000; + // v.push_back(r); + // std::cout << r << " "; + // } + for (int r : v) std::cout << r << " "; + + int median = std::rand() % 1000; // initialize to a random numnber + std::cout << "\nmedian=" << median << std::endl; int avg1, avg2, avg3, sum1 = 0, sum2 = 0, sum3 = 0; - for (int i = 0; i < 1000; i++) { + + for (int i = 0; i < v.size(); i++) { // iterate through all numbers if (v.back() == v[median]) { avg1 = sum1 + v.back(); s2.push_back(v.back()); @@ -49,9 +67,12 @@ int main() { } v.pop_back(); } + int x; std::cout << "enter the no. to be searched form begining:- "; std::cin >> x; - comp(x - 1); + comp(x - 1, s1, s2, s3); + return 0; } +/// } From ecddfd2704b156886e159bd92c619eb53a0f130b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 21:30:42 -0400 Subject: [PATCH 12/17] use pointers instead --- search/median_search.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 2134a0dbe..7379cad26 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -14,16 +14,16 @@ * @todo add documentation */ template -void comp(X x, std::vector &s1, std::vector &s2, - std::vector &s3) { - if (s1.size() >= 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 +void comp(X x, std::vector *s1, std::vector *s2, + std::vector *s3) { + if (s1->size() >= 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"; @@ -71,7 +71,7 @@ int main() { int x; std::cout << "enter the no. to be searched form begining:- "; std::cin >> x; - comp(x - 1, s1, s2, s3); + comp(x - 1, &s1, &s2, &s3); return 0; } From 9dc580c045de9da759698d519ab86aba4b81e05b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 21:35:28 -0400 Subject: [PATCH 13/17] rename to more meaningful text_search --- search/{searching.cpp => text_search.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename search/{searching.cpp => text_search.cpp} (100%) diff --git a/search/searching.cpp b/search/text_search.cpp similarity index 100% rename from search/searching.cpp rename to search/text_search.cpp From a3c1f04b31ce57d89cf02ebfe743fc37ed01078f Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 21:36:01 -0400 Subject: [PATCH 14/17] replace platform specific system("pause") --- search/text_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search/text_search.cpp b/search/text_search.cpp index d01619738..903442fc4 100644 --- a/search/text_search.cpp +++ b/search/text_search.cpp @@ -28,7 +28,7 @@ int main() { << paragraph.find(word) << std::endl << std::endl; } - system("pause"); + std::cin.get(); } } } From b122d659c326a58ff9c305e544181855e7f47fad Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 21:36:11 -0400 Subject: [PATCH 15/17] document text_search --- search/text_search.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/search/text_search.cpp b/search/text_search.cpp index 903442fc4..ee66a506a 100644 --- a/search/text_search.cpp +++ b/search/text_search.cpp @@ -1,9 +1,17 @@ +/** + * \file + * \brief Search for words in a long textual paragraph. + */ #include #include -#include - -char paragraph; +#ifdef _MSC_VER +#include // required for MS Visual C++ +#else +#include +#endif +/** Main function + */ int main() { std::string paragraph; std::cout << "Please enter your paragraph: \n"; From 5698363bd7814048975ebeb7543535e184bb5043 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 28 May 2020 21:46:18 -0400 Subject: [PATCH 16/17] reformat documentation --- search/ternary_search.cpp | 67 ++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/search/ternary_search.cpp b/search/ternary_search.cpp index 4d1918d48..73b89da7a 100644 --- a/search/ternary_search.cpp +++ b/search/ternary_search.cpp @@ -1,49 +1,57 @@ -/* +/** + * \file + * \brief [Ternary search](https://en.wikipedia.org/wiki/Ternary_search) + * algorithm + * * This is a divide and conquer algorithm. * 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) + * * Time Complexity : O(log3 n) + * * Space Complexity : O(1) (without the array) */ #include -/* +/** * The absolutePrecision can be modified to fit preference but * it is recommended to not go lower than 10 due to errors that * may occur. - * + */ +#define absolutePrecision 10 +/** * The value of _target should be decided or can be decided later * by using the variable of the function. */ - #define _target 10 -#define absolutePrecision 10 -#define MAX 10000000 -int N = 21; -int A[MAX] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 10}; +#define MAX 10000000 ///< Maximum length of array -/* +/** * get_input function is to receive input from standard IO + * @todo @christianbender 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. -} +void get_input() {} -/* +/** * This is the iterative method of the ternary search which returns the index of * the element. + * \param[in] left lower interval limit + * \param[in] right upper interval limit + * \param[in] A array to search in + * \param[in] target value to search for + * \returns index where the target value was found + * \returns -1 if target value not found */ 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; + if (A[i] == target) + return i; return -1; } @@ -69,15 +77,22 @@ int it_ternary_search(int left, int right, int A[], int target) { } } -/* +/** * This is the recursive method of the ternary search which returns the index of * the element. + * \param[in] left lower interval limit + * \param[in] right upper interval limit + * \param[in] A array to search in + * \param[in] target value to search for + * \returns index where the target value was found + * \returns -1 if target value not found */ 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; + if (A[i] == target) + return i; return -1; } @@ -85,8 +100,10 @@ int rec_ternary_search(int left, int right, int A[], int target) { 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 (A[oneThird] == target) + return oneThird; + if (A[twoThird] == target) + return twoThird; if (target < A[oneThird]) return rec_ternary_search(left, oneThird - 1, A, target); @@ -99,10 +116,13 @@ int rec_ternary_search(int left, int right, int A[], int target) { } } -/* +/** * ternary_search is a template function * You could either use it_ternary_search or rec_ternary_search according to * preference. + * \param [in] N length of array + * \param[in] A array to search in + * \param[in] target value to search for */ void ternary_search(int N, int A[], int target) { std::cout << it_ternary_search(0, N - 1, A, target) << '\t'; @@ -110,7 +130,10 @@ void ternary_search(int N, int A[], int target) { std::cout << std::endl; } +/** Main function */ int main() { + int N = 21; + int A[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 10}; get_input(); ternary_search(N, A, _target); return 0; From a9e160fe26972dd2ed17d8784359d6cf825e670e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 29 May 2020 01:46:43 +0000 Subject: [PATCH 17/17] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 51e840ef4..740a61647 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -177,8 +177,8 @@ * [Jump Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/jump_search.cpp) * [Linear Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/linear_search.cpp) * [Median Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/median_search.cpp) - * [Searching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/searching.cpp) * [Ternary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/ternary_search.cpp) + * [Text Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/text_search.cpp) ## Sorting * [Bead Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bead_sort.cpp)