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)
|
* [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)
|
* [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)
|
* [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)
|
* [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
|
## Sorting
|
||||||
* [Bead Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bead_sort.cpp)
|
* [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>
|
#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) {
|
while (l <= r) {
|
||||||
int m = l + (r - l) / 2;
|
int m = l + (r - l) / 2;
|
||||||
if (key == a[m])
|
if (key == a[m])
|
||||||
@ -12,23 +26,31 @@ int binary_search(int a[], int l, int r, int key) {
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** main function */
|
||||||
int main(int argc, char const* argv[]) {
|
int main(int argc, char const* argv[]) {
|
||||||
int n, key;
|
int n, key;
|
||||||
std::cout << "Enter size of array: ";
|
std::cout << "Enter size of array: ";
|
||||||
std::cin >> n;
|
std::cin >> n;
|
||||||
std::cout << "Enter array elements: ";
|
std::cout << "Enter array elements: ";
|
||||||
|
|
||||||
int* a = new int[n];
|
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++) {
|
for (int i = 0; i < n; i++) {
|
||||||
std::cin >> a[i];
|
std::cin >> a[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Enter search key: ";
|
std::cout << "Enter search key: ";
|
||||||
std::cin >> key;
|
std::cin >> key;
|
||||||
|
|
||||||
// this is use for find value in given array
|
// 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)
|
if (res != -1)
|
||||||
std::cout << key << " found at index " << res << std::endl;
|
std::cout << key << " found at index " << res << std::endl;
|
||||||
else
|
else
|
||||||
std::cout << key << " not found" << std::endl;
|
std::cout << key << " not found" << std::endl;
|
||||||
|
|
||||||
|
delete[] a;
|
||||||
return 0;
|
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 <cassert>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iostream>
|
#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)
|
/** Binary Search Algorithm (used by ::struzik_search)\n
|
||||||
// Time Complexity O(log n) where 'n' is the number of elements
|
* * Time Complexity O(log n) where 'n' is the number of elements
|
||||||
// Worst Time Complexity O(log n)
|
* * Worst Time Complexity O(log n)
|
||||||
// Best Time Complexity Ω(1)
|
* * Best Time Complexity Ω(1)
|
||||||
// Space Complexity O(1)
|
* * Space Complexity O(1)
|
||||||
// Auxiliary 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>
|
template <class Type>
|
||||||
inline Type* binary_s(Type* array, size_t size, Type key) {
|
inline Type* binary_s(Type* array, size_t size, Type key) {
|
||||||
int32_t lower_index(0), upper_index(size - 1), middle_index;
|
int32_t lower_index(0), upper_index(size - 1), middle_index;
|
||||||
|
|
||||||
while (lower_index <= upper_index) {
|
while (lower_index <= upper_index) {
|
||||||
middle_index = std::floor((lower_index + upper_index) / 2);
|
middle_index = std::floor((lower_index + upper_index) / 2);
|
||||||
|
|
||||||
if (*(array + middle_index) < key)
|
if (*(array + middle_index) < key)
|
||||||
lower_index = (middle_index + 1);
|
lower_index = (middle_index + 1);
|
||||||
else if (*(array + middle_index) > key)
|
else if (*(array + middle_index) > key)
|
||||||
@ -22,21 +44,17 @@ inline Type* binary_s(Type* array, size_t size, Type key) {
|
|||||||
else
|
else
|
||||||
return (array + middle_index);
|
return (array + middle_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
// Struzik Search Algorithm(Exponential)
|
|
||||||
// Time Complexity O(log i)where i is the position of search key in the list
|
/** Struzik Search Algorithm(Exponential)
|
||||||
// Worst Time Complexity O(log i)
|
* * Time Complexity O(log i) where i is the position of search key in the list
|
||||||
// Best Time Complexity Ω(1)
|
* * Worst Time Complexity O(log i)
|
||||||
// Space Complexity O(1)
|
* * Best Time Complexity Ω(1)
|
||||||
// Auxiliary Space Complexity O(1)
|
* * Space Complexity O(1)
|
||||||
/* Tha algorithm try to search the range where the key should be.
|
* * Auxiliary Space Complexity O(1)
|
||||||
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. */
|
|
||||||
template <class Type>
|
template <class Type>
|
||||||
Type* struzik_search(Type* array, size_t size, Type key) {
|
Type* struzik_search(Type* array, size_t size, Type key) {
|
||||||
uint32_t block_front(0), block_size = size == 0 ? 0 : 1;
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
int main() {
|
||||||
// TEST CASES
|
// TEST CASES
|
||||||
int* sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203};
|
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, 50) == nullptr);
|
||||||
assert(struzik_search<int>(sorted_array, 7, 7) == sorted_array);
|
assert(struzik_search<int>(sorted_array, 7, 7) == sorted_array);
|
||||||
// TEST CASES
|
// TEST CASES
|
||||||
|
delete[] sorted_array;
|
||||||
return 0;
|
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
|
* \file
|
||||||
* Best Time Complexity Ω(1)
|
* \brief 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,
|
* \copyright 2020 Arctic2333
|
||||||
* 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
|
* 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 {
|
typedef struct list {
|
||||||
int key;
|
int key; //!< key value for node
|
||||||
struct list * next;
|
struct list* next; //!< pointer to next link in the chain
|
||||||
}
|
} node, /**< define node as one item list */
|
||||||
node, * link;
|
*link; ///< pointer to nodes
|
||||||
node hashtab[HASHMAX];
|
|
||||||
int counter = 1;
|
node hashtab[HASHMAX]; ///< array of nodes
|
||||||
/* int h(int key)
|
|
||||||
|
// int counter = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
* Mode of hash detection :
|
* Mode of hash detection :
|
||||||
* Division method */
|
* Division method
|
||||||
int h(int key) {
|
* \param [in] key to hash
|
||||||
return key % HASHMAX;
|
* \returns hash value for `key`
|
||||||
}
|
*/
|
||||||
/* void create_list(int key)
|
int h(int key) { return key % HASHMAX; }
|
||||||
|
|
||||||
|
/**
|
||||||
* The same after the remainder will be added after the same hash header
|
* The same after the remainder will be added after the same hash header
|
||||||
* To avoid conflict, zipper method is used
|
* 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
|
void create_list(int key) { // Construct hash table
|
||||||
link p, n;
|
link p, n;
|
||||||
int index;
|
int index;
|
||||||
@ -39,62 +64,76 @@ void create_list(int key) { // Construct hash table
|
|||||||
n->next = p;
|
n->next = p;
|
||||||
hashtab[index].next = n;
|
hashtab[index].next = n;
|
||||||
} else {
|
} 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
|
* Input the key to be searched, and get the hash header position through the H
|
||||||
* If not found @return -1 */
|
* (int key) function, then one-dimensional linear search. If found @return
|
||||||
int hash_search(int key) { // Hash lookup function
|
* element depth and number of searches If not found @return -1
|
||||||
|
*/
|
||||||
|
int hash_search(int key, int* counter) { // Hash lookup function
|
||||||
link pointer;
|
link pointer;
|
||||||
int index;
|
int index;
|
||||||
counter = 0;
|
|
||||||
|
*counter = 0;
|
||||||
index = h(key);
|
index = h(key);
|
||||||
pointer = hashtab[index].next;
|
pointer = hashtab[index].next;
|
||||||
printf("data[%d]:", index);
|
|
||||||
|
std::cout << "data[" << index << "]:";
|
||||||
|
|
||||||
while (pointer != NULL) {
|
while (pointer != NULL) {
|
||||||
counter++;
|
counter[0]++;
|
||||||
printf("data[%d]:", pointer -> key);
|
std::cout << "data[" << pointer->key << "]:";
|
||||||
if (pointer->key == key)
|
if (pointer->key == key)
|
||||||
return 1;
|
return 1;
|
||||||
else
|
else
|
||||||
pointer = pointer->next;
|
pointer = pointer->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** main function */
|
||||||
int main() {
|
int main() {
|
||||||
link p;
|
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;
|
index = 0;
|
||||||
|
|
||||||
// You can write the input mode here
|
// You can write the input mode here
|
||||||
while (index < MAX) { // Construct hash table
|
while (index < MAX) { // Construct hash table
|
||||||
create_list(data[index]);
|
create_list(data[index]);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < HASHMAX; i++) { // Output hash table
|
for (i = 0; i < HASHMAX; i++) { // Output hash table
|
||||||
printf("hashtab [%d]", i);
|
std::cout << "hashtab [" << i << "]\n";
|
||||||
printf("\n");
|
|
||||||
p = hashtab[i].next;
|
p = hashtab[i].next;
|
||||||
|
|
||||||
while (p != NULL) {
|
while (p != NULL) {
|
||||||
printf("please int key:");
|
std::cout << "please int key:";
|
||||||
if (p->key > 0)
|
if (p->key > 0)
|
||||||
printf("[%d]", p -> key);
|
std::cout << "[" << p->key << "]";
|
||||||
p = p->next;
|
p = p->next;
|
||||||
}
|
}
|
||||||
printf("\n");
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (key != -1) {
|
while (key != -1) {
|
||||||
// You can write the input mode here
|
// You can write the input mode here
|
||||||
// test key = 10
|
// test key = 10
|
||||||
key = 10;
|
key = 10;
|
||||||
if (hash_search(key))
|
if (hash_search(key, &counter))
|
||||||
printf("search time = %d\n", counter);
|
std::cout << "search time = " << counter << std::endl;
|
||||||
else
|
else
|
||||||
printf("no found!\n");
|
std::cout << "no found!\n";
|
||||||
key = -1; // Exit test
|
key = -1; // Exit test
|
||||||
/* The test sample is returned as: data[0]:data[5]:data[15]:data[10]:search time = 3
|
/* The test sample is returned as:
|
||||||
* The search is successful. There are 10 in this set of data */
|
* 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [Interpolation
|
||||||
|
* search](https://en.wikipedia.org/wiki/Interpolation_search) algorithm
|
||||||
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// function to search the value in an array using interpolation search
|
/** function to search the value in an array using interpolation search
|
||||||
int search(int arr[], int value, int len) {
|
* \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;
|
int low = 0, high, mid;
|
||||||
high = len - 1;
|
high = len - 1;
|
||||||
|
|
||||||
while (arr[low] <= value && arr[high] >= value) {
|
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)
|
if (arr[mid] > value)
|
||||||
high = mid - 1;
|
high = mid - 1;
|
||||||
else if (arr[mid] < value)
|
else if (arr[mid] < value)
|
||||||
@ -13,24 +26,36 @@ int search(int arr[], int value, int len) {
|
|||||||
else
|
else
|
||||||
return mid;
|
return mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arr[low] == value)
|
if (arr[low] == value)
|
||||||
return low;
|
return low;
|
||||||
return 0;
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** main function */
|
||||||
int main() {
|
int main() {
|
||||||
int n, value, array[100], re;
|
int n, value, re;
|
||||||
|
|
||||||
std::cout << "Enter the size of array(less than 100) : ";
|
std::cout << "Enter the size of array(less than 100) : ";
|
||||||
std::cin >> n;
|
std::cin >> n;
|
||||||
|
|
||||||
|
int *array = new int[n];
|
||||||
|
|
||||||
std::cout << "array in ascending (increasing) order : " << std::endl;
|
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::cout << "Enter the value you want to search : ";
|
||||||
std::cin >> value;
|
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;
|
std::cout << "Entered value is not in the array" << std::endl;
|
||||||
else
|
else
|
||||||
std::cout << "The value is at the position " << re << std::endl;
|
std::cout << "The value is at the position " << re << std::endl;
|
||||||
|
|
||||||
|
delete[] array;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [Interpolation
|
||||||
|
* search](https://en.wikipedia.org/wiki/Interpolation_search) algorithm
|
||||||
|
*/
|
||||||
#include <iostream>
|
#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 InterpolationSearch(int A[], int n, int x) {
|
||||||
int low = 0;
|
int low = 0;
|
||||||
int high = n - 1;
|
int high = n - 1;
|
||||||
@ -11,18 +24,21 @@ int InterpolationSearch(int A[], int n, int x) {
|
|||||||
else
|
else
|
||||||
low = mid + 1; // x lies after mid
|
low = mid + 1; // x lies after mid
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** main function */
|
||||||
int main() {
|
int main() {
|
||||||
int A[] = {2, 4, 5, 7, 13, 14, 15, 23};
|
int A[] = {2, 4, 5, 7, 13, 14, 15, 23};
|
||||||
int x = 17;
|
int x = 17;
|
||||||
int index = InterpolationSearch(
|
|
||||||
A, 8, x); // passed array A inside the InterpolationSearch function
|
///< passed array A inside the InterpolationSearch function
|
||||||
if (index != -1)
|
int index = InterpolationSearch(A, 8, x);
|
||||||
std::cout << "Number " << x << " is at " << index;
|
if (index < 0)
|
||||||
|
std::cout << "Number " << x << " not found" << std::endl;
|
||||||
else
|
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
|
// 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 <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/** jump search implementation
|
||||||
|
*/
|
||||||
int jumpSearch(int arr[], int x, int n) {
|
int jumpSearch(int arr[], int x, int n) {
|
||||||
// Finding block size to be jumped
|
// Finding block size to be jumped
|
||||||
int step = std::sqrt(n);
|
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) {
|
while (arr[std::min(step, n) - 1] < x) {
|
||||||
prev = step;
|
prev = step;
|
||||||
step += std::sqrt(n);
|
step += std::sqrt(n);
|
||||||
if (prev >= n) return -1;
|
if (prev >= n)
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Doing a linear search for x in block
|
// 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
|
// If we reached next block or end of
|
||||||
// array, element is not present.
|
// 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 element is found
|
||||||
if (arr[prev] == x) return prev;
|
if (arr[prev] == x)
|
||||||
|
return prev;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [Linear search
|
||||||
|
* algorithm](https://en.wikipedia.org/wiki/Linear_search)
|
||||||
|
*/
|
||||||
#include <iostream>
|
#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) {
|
int LinearSearch(int *array, int size, int key) {
|
||||||
for (int i = 0; i < size; ++i) {
|
for (int i = 0; i < size; ++i) {
|
||||||
if (array[i] == key) {
|
if (array[i] == key) {
|
||||||
@ -10,6 +23,7 @@ int LinearSearch(int *array, int size, int key) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** main function */
|
||||||
int main() {
|
int main() {
|
||||||
int size;
|
int size;
|
||||||
std::cout << "\nEnter the size of the Array : ";
|
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 <algorithm>
|
||||||
#include <cmath>
|
|
||||||
#include <deque>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iterator>
|
|
||||||
#include <stack>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
std::vector<int> v;
|
/**
|
||||||
std::vector<int> s1;
|
* @todo add documentation
|
||||||
std::vector<int> s2;
|
*/
|
||||||
std::vector<int> s3;
|
|
||||||
|
|
||||||
template <class X>
|
template <class X>
|
||||||
void comp(X x) {
|
void comp(X x, std::vector<int> *s1, std::vector<int> *s2,
|
||||||
if (s1.size() >= x && s1.size() + s2.size() < x) {
|
std::vector<int> *s3) {
|
||||||
std::cout << s2[0] << " is the " << x + 1 << "th element from front";
|
if (s1->size() >= x && s1->size() + s2->size() < x) {
|
||||||
} else if (s1.size() > x) {
|
std::cout << (*s2)[0] << " is the " << x + 1 << "th element from front";
|
||||||
std::sort(s1.begin(), s1.end());
|
} else if (s1->size() > x) {
|
||||||
std::cout << s1[x] << " is the " << x + 1 << "th element from front";
|
std::sort(s1->begin(), s1->end());
|
||||||
} else if (s1.size() + s2.size() <= x && s3.size() > x) {
|
std::cout << (*s1)[x] << " is the " << x + 1 << "th element from front";
|
||||||
std::sort(s3.begin(), s3.end());
|
} else if (s1->size() + s2->size() <= x && s3->size() > x) {
|
||||||
std::cout << s3[x - s1.size() - s2.size()] << " is the " << x + 1
|
std::sort(s3->begin(), s3->end());
|
||||||
|
std::cout << (*s3)[x - s1->size() - s2->size()] << " is the " << x + 1
|
||||||
<< "th element from front";
|
<< "th element from front";
|
||||||
} else {
|
} else {
|
||||||
std::cout << x + 1 << " is invalid location";
|
std::cout << x + 1 << " is invalid location";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define MAX_NUM 20 ///< maximum number of values to sort from
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main function
|
||||||
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
for (int i = 0; i < 1000; i++) {
|
std::vector<int> v{25, 21, 98, 100, 76, 22, 43, 60, 89, 87};
|
||||||
v.push_back(std::rand() % 1000);
|
std::vector<int> s1;
|
||||||
}
|
std::vector<int> s2;
|
||||||
for (int r : v) {
|
std::vector<int> s3;
|
||||||
std::cout << r << " ";
|
|
||||||
}
|
// creates an array of random numbers
|
||||||
int median = std::rand() % 1000;
|
// 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;
|
std::cout << "\nmedian=" << median << std::endl;
|
||||||
int avg1, avg2, avg3, sum1 = 0, sum2 = 0, sum3 = 0;
|
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]) {
|
if (v.back() == v[median]) {
|
||||||
avg1 = sum1 + v.back();
|
avg1 = sum1 + v.back();
|
||||||
s2.push_back(v.back());
|
s2.push_back(v.back());
|
||||||
@ -49,9 +67,12 @@ int main() {
|
|||||||
}
|
}
|
||||||
v.pop_back();
|
v.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
int x;
|
int x;
|
||||||
std::cout << "enter the no. to be searched form begining:- ";
|
std::cout << "enter the no. to be searched form begining:- ";
|
||||||
std::cin >> x;
|
std::cin >> x;
|
||||||
comp(x - 1);
|
comp(x - 1, &s1, &s2, &s3);
|
||||||
|
|
||||||
return 0;
|
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.
|
* This is a divide and conquer algorithm.
|
||||||
* It does this by dividing the search space by 3 parts and
|
* It does this by dividing the search space by 3 parts and
|
||||||
* using its property (usually monotonic property) to find
|
* using its property (usually monotonic property) to find
|
||||||
* the desired index.
|
* the desired index.
|
||||||
*
|
*
|
||||||
* Time Complexity : O(log3 n)
|
* * Time Complexity : O(log3 n)
|
||||||
* Space Complexity : O(1) (without the array)
|
* * Space Complexity : O(1) (without the array)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* 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
|
* it is recommended to not go lower than 10 due to errors that
|
||||||
* may occur.
|
* may occur.
|
||||||
*
|
*/
|
||||||
|
#define absolutePrecision 10
|
||||||
|
/**
|
||||||
* The value of _target should be decided or can be decided later
|
* The value of _target should be decided or can be decided later
|
||||||
* by using the variable of the function.
|
* by using the variable of the function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define _target 10
|
#define _target 10
|
||||||
#define absolutePrecision 10
|
|
||||||
#define MAX 10000000
|
|
||||||
|
|
||||||
int N = 21;
|
#define MAX 10000000 ///< Maximum length of array
|
||||||
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
|
* 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() {
|
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
|
* This is the iterative method of the ternary search which returns the index of
|
||||||
* the element.
|
* 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) {
|
int it_ternary_search(int left, int right, int A[], int target) {
|
||||||
while (1) {
|
while (1) {
|
||||||
if (left < right) {
|
if (left < right) {
|
||||||
if (right - left < absolutePrecision) {
|
if (right - left < absolutePrecision) {
|
||||||
for (int i = left; i <= right; i++)
|
for (int i = left; i <= right; i++)
|
||||||
if (A[i] == target) return i;
|
if (A[i] == target)
|
||||||
|
return i;
|
||||||
|
|
||||||
return -1;
|
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
|
* This is the recursive method of the ternary search which returns the index of
|
||||||
* the element.
|
* 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) {
|
int rec_ternary_search(int left, int right, int A[], int target) {
|
||||||
if (left < right) {
|
if (left < right) {
|
||||||
if (right - left < absolutePrecision) {
|
if (right - left < absolutePrecision) {
|
||||||
for (int i = left; i <= right; i++)
|
for (int i = left; i <= right; i++)
|
||||||
if (A[i] == target) return i;
|
if (A[i] == target)
|
||||||
|
return i;
|
||||||
|
|
||||||
return -1;
|
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 oneThird = (left + right) / 3 + 1;
|
||||||
int twoThird = (left + right) * 2 / 3 + 1;
|
int twoThird = (left + right) * 2 / 3 + 1;
|
||||||
|
|
||||||
if (A[oneThird] == target) return oneThird;
|
if (A[oneThird] == target)
|
||||||
if (A[twoThird] == target) return twoThird;
|
return oneThird;
|
||||||
|
if (A[twoThird] == target)
|
||||||
|
return twoThird;
|
||||||
|
|
||||||
if (target < A[oneThird])
|
if (target < A[oneThird])
|
||||||
return rec_ternary_search(left, oneThird - 1, A, target);
|
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
|
* ternary_search is a template function
|
||||||
* You could either use it_ternary_search or rec_ternary_search according to
|
* You could either use it_ternary_search or rec_ternary_search according to
|
||||||
* preference.
|
* 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) {
|
void ternary_search(int N, int A[], int target) {
|
||||||
std::cout << it_ternary_search(0, N - 1, A, target) << '\t';
|
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;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
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();
|
get_input();
|
||||||
ternary_search(N, A, _target);
|
ternary_search(N, A, _target);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief Search for words in a long textual paragraph.
|
||||||
|
*/
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#ifdef _MSC_VER
|
||||||
|
#include <string> // required for MS Visual C++
|
||||||
char paragraph;
|
#else
|
||||||
|
#include <cstring>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** Main function
|
||||||
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
std::string paragraph;
|
std::string paragraph;
|
||||||
std::cout << "Please enter your paragraph: \n";
|
std::cout << "Please enter your paragraph: \n";
|
||||||
@ -28,7 +36,7 @@ int main() {
|
|||||||
<< paragraph.find(word) << std::endl
|
<< paragraph.find(word) << std::endl
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
system("pause");
|
std::cin.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user