mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #4 from kvedala/document/search
Document `search` folder
This commit is contained in:
commit
29bdf4438b
@ -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)
|
||||
|
@ -1,6 +1,20 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Binary search
|
||||
* algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm)
|
||||
*/
|
||||
#include <iostream>
|
||||
// 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])
|
||||
@ -12,23 +26,31 @@ int binary_search(int a[], int l, int r, int key) {
|
||||
}
|
||||
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
|
||||
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);
|
||||
int res = binary_search(a, n - 1, key);
|
||||
if (res != -1)
|
||||
std::cout << key << " found at index " << res << std::endl;
|
||||
else
|
||||
std::cout << key << " not found" << std::endl;
|
||||
|
||||
delete[] a;
|
||||
return 0;
|
||||
}
|
||||
|
@ -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 <cassert>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#ifdef _MSC_VER
|
||||
#include <string> // use for MS Visual C++
|
||||
#else
|
||||
#include <cstring> // 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 <class Type>
|
||||
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 <class Type>
|
||||
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};
|
||||
@ -59,5 +79,6 @@ int main() {
|
||||
assert(struzik_search<int>(sorted_array, 7, 50) == nullptr);
|
||||
assert(struzik_search<int>(sorted_array, 7, 7) == sorted_array);
|
||||
// TEST CASES
|
||||
delete[] sorted_array;
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,32 +1,57 @@
|
||||
// Copyright 2020 Arctic2333
|
||||
#include <stdlib.h>
|
||||
#include<stdio.h>
|
||||
#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 <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
@ -39,62 +64,76 @@ void create_list(int key) { // Construct hash table
|
||||
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);
|
||||
counter[0]++;
|
||||
std::cout << "data[" << pointer->key << "]:";
|
||||
if (pointer->key == key)
|
||||
return 1;
|
||||
else
|
||||
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:");
|
||||
std::cout << "please int key:";
|
||||
if (p->key > 0)
|
||||
printf("[%d]", p -> key);
|
||||
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;
|
||||
}
|
||||
|
@ -1,11 +1,24 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Interpolation
|
||||
* search](https://en.wikipedia.org/wiki/Interpolation_search) algorithm
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
// function to search the value in an array using interpolation search
|
||||
int search(int arr[], int value, int len) {
|
||||
/** 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]));
|
||||
mid = (low +
|
||||
((value - arr[low]) * (high - low)) / (arr[high] - arr[low]));
|
||||
if (arr[mid] > value)
|
||||
high = mid - 1;
|
||||
else if (arr[mid] < value)
|
||||
@ -13,24 +26,36 @@ int search(int arr[], int value, int len) {
|
||||
else
|
||||
return mid;
|
||||
}
|
||||
|
||||
if (arr[low] == value)
|
||||
return low;
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** main function */
|
||||
int main() {
|
||||
int n, value, array[100], re;
|
||||
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];
|
||||
|
||||
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)
|
||||
|
||||
re = interpolation_search(array, value, n);
|
||||
|
||||
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;
|
||||
|
||||
delete[] array;
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,4 +1,17 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Interpolation
|
||||
* search](https://en.wikipedia.org/wiki/Interpolation_search) algorithm
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
/** 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
|
||||
|
@ -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 <algorithm>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
@ -1,5 +1,18 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Linear search
|
||||
* algorithm](https://en.wikipedia.org/wiki/Linear_search)
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 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 : ";
|
||||
|
@ -1,42 +1,60 @@
|
||||
/**
|
||||
* \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 <algorithm>
|
||||
#include <cmath>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
std::vector<int> v;
|
||||
std::vector<int> s1;
|
||||
std::vector<int> s2;
|
||||
std::vector<int> s3;
|
||||
|
||||
/**
|
||||
* @todo add documentation
|
||||
*/
|
||||
template <class X>
|
||||
void comp(X x) {
|
||||
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<int> *s1, std::vector<int> *s2,
|
||||
std::vector<int> *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";
|
||||
}
|
||||
}
|
||||
|
||||
#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<int> v{25, 21, 98, 100, 76, 22, 43, 60, 89, 87};
|
||||
std::vector<int> s1;
|
||||
std::vector<int> s2;
|
||||
std::vector<int> 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;
|
||||
}
|
||||
/// }
|
||||
|
@ -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 <iostream>
|
||||
|
||||
/*
|
||||
/**
|
||||
* 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;
|
||||
|
@ -1,9 +1,17 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief Search for words in a long textual paragraph.
|
||||
*/
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
char paragraph;
|
||||
#ifdef _MSC_VER
|
||||
#include <string> // required for MS Visual C++
|
||||
#else
|
||||
#include <cstring>
|
||||
#endif
|
||||
|
||||
/** Main function
|
||||
*/
|
||||
int main() {
|
||||
std::string paragraph;
|
||||
std::cout << "Please enter your paragraph: \n";
|
||||
@ -28,7 +36,7 @@ int main() {
|
||||
<< paragraph.find(word) << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
system("pause");
|
||||
std::cin.get();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user