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