mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
document exponential search
This commit is contained in:
parent
605fa140cc
commit
969e916871
@ -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};
|
||||||
|
Loading…
Reference in New Issue
Block a user