From 2c3d72b102c48b6c4329f5b89b45a546da11eeac Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 14:38:58 +0400 Subject: [PATCH 01/10] Added fibonacci search --- search/fibonacci_search.cpp | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 search/fibonacci_search.cpp diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp new file mode 100644 index 000000000..29f10c370 --- /dev/null +++ b/search/fibonacci_search.cpp @@ -0,0 +1,61 @@ +#include +#include + +/* +Input: a sorted array and a value +Output: if the array contains the value, returns its index (index of its first occurence) + else returns -1 +*/ + +int fibonacchi_search(std::vector arr, int value){ + int last = 0, current = 1, offset = -1, index; + int length = arr.size(); + int next = last + current; + + while(next < length){ + last = current; + current = next; + next = last + current; + } + + while(next > 1){ + index = std::min(offset + last, length-1); + if(arr[index] < value){ + next = current; + current = last; + last = next - current; + offset = index; + } else if(arr[index] > value){ + next = last; + current = current - last; + last = next - current; + } else { + return index; + } + } + if(current && !arr.empty() && arr[offset+1] == value){ + return offset+1; + } + return -1; +} + +int main() { + int size, value; + std::cout << "Enter size of a sorted array: " << std::endl; + std::cin >> size; + std::cout << "Enter array elements: " << std::endl; + + std::vector arr(size); + for(int i = 0; i < size; i++){ + std::cin >> arr[i]; + } + std::cout << "Enter the value you're looking for: " << std::endl; + std::cin >> value; + int index = fibonacchi_search(arr, value); + if(index != -1){ + std::cout << "Index of the given value is " << index << std::endl; + } else { + std::cout << "Array does not contain the value" << std::endl; + } + return 0; +} From 363f43c942879a2ee92253588896fb3c81c8780a Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 14:57:57 +0400 Subject: [PATCH 02/10] min function implemented --- search/fibonacci_search.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index 29f10c370..af3858e74 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -7,7 +7,15 @@ Output: if the array contains the value, returns its index (index of its first o else returns -1 */ -int fibonacchi_search(std::vector arr, int value){ +int min(int first, int second){ + if(first > second){ + return first; + } else { + return second; + } +} + +int fibonacci_search(std::vector arr, int value){ int last = 0, current = 1, offset = -1, index; int length = arr.size(); int next = last + current; @@ -19,7 +27,7 @@ int fibonacchi_search(std::vector arr, int value){ } while(next > 1){ - index = std::min(offset + last, length-1); + index = min(offset + last, length-1); if(arr[index] < value){ next = current; current = last; @@ -51,7 +59,7 @@ int main() { } std::cout << "Enter the value you're looking for: " << std::endl; std::cin >> value; - int index = fibonacchi_search(arr, value); + int index = fibonacci_search(arr, value); if(index != -1){ std::cout << "Index of the given value is " << index << std::endl; } else { From 1002126449a0599630df669397aa49a1586e3e90 Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 15:05:07 +0400 Subject: [PATCH 03/10] min function corrected --- search/fibonacci_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index af3858e74..0bf33c494 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -8,7 +8,7 @@ Output: if the array contains the value, returns its index (index of its first o */ int min(int first, int second){ - if(first > second){ + if(first < second){ return first; } else { return second; From 86c6af46e865d78e9a41256414690ded7916efb3 Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 17:27:45 +0400 Subject: [PATCH 04/10] doxygen documentation and tests added --- search/fibonacci_search.cpp | 140 +++++++++++++++++++++++++++++------- 1 file changed, 116 insertions(+), 24 deletions(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index 0bf33c494..2a3ba7207 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -1,12 +1,19 @@ +/** + * Copyright 2020 @author sprintyaf + * @file + * @brief [Fibonacci search + * algorithm](https://en.wikipedia.org/wiki/Fibonacci_search_technique) + */ + #include -#include +#include // for std::vector class +#include // for assert -/* -Input: a sorted array and a value -Output: if the array contains the value, returns its index (index of its first occurence) - else returns -1 -*/ + +/** + * Returns minimum of the two numbers + */ int min(int first, int second){ if(first < second){ return first; @@ -15,7 +22,12 @@ int min(int first, int second){ } } -int fibonacci_search(std::vector arr, int value){ +/** + * Input: a sorted array and a value + * Output: if the array contains the value, returns its index + * else returns -1 + */ +int fibonacci_search(std::vector &arr, int value){ int last = 0, current = 1, offset = -1, index; int length = arr.size(); int next = last + current; @@ -47,23 +59,103 @@ int fibonacci_search(std::vector arr, int value){ return -1; } -int main() { - int size, value; - std::cout << "Enter size of a sorted array: " << std::endl; - std::cin >> size; - std::cout << "Enter array elements: " << std::endl; +/** + * Tests where given value occurs only once + */ +bool one_occurence_test(){ + // declarations + int value, index; + bool passed = true; + std::vector arr; + // last element + arr = {1, 2, 3}; + value = 3; + index = fibonacci_search(arr, value); + passed = passed && (index == 2); + // first element + value = 1; + index = fibonacci_search(arr, value); + passed = passed && (index == 0); + // somewhere in the middle element + arr = {1, 3, 5, 7}; + value = 3; + index = fibonacci_search(arr, value); + passed = passed && (index == 1); + // arr size is 1 + arr = {10}; + value = 10; + index = fibonacci_search(arr, value); + passed = passed && (index == 0); - std::vector arr(size); - for(int i = 0; i < size; i++){ - std::cin >> arr[i]; - } - std::cout << "Enter the value you're looking for: " << std::endl; - std::cin >> value; - int index = fibonacci_search(arr, value); - if(index != -1){ - std::cout << "Index of the given value is " << index << std::endl; - } else { - std::cout << "Array does not contain the value" << std::endl; - } + return passed; +} + +/** + * Tests where given value occurs more than once + */ +bool many_occurence_test(){ + // declarations + int value, index; + bool passed = true; + std::vector arr; + // last element + arr = {1, 1, 10, 10}; + value = 10; + index = fibonacci_search(arr, value); + passed = passed && (index == 2 || index == 3); + // first element + value = 1; + index = fibonacci_search(arr, value); + passed = passed && (index == 0 || index == 1); + // somewhere in the middle element + arr = {1, 3, 3, 7}; + value = 3; + index = fibonacci_search(arr, value); + passed = passed && (index == 1 || index == 2); + // all elements are the same + arr = {10, 10, 10}; + value = 10; + index = fibonacci_search(arr, value); + passed = passed && (index >= 0 && index <= 2); + + return passed; +} + +/** + * Tests where the array doesn't contain given value + */ +bool no_occurence_test(){ + // declarations + int value, index; + bool passed = true; + std::vector arr; + // many elements + arr = {1, 2, 4, 5}; + value = 3; + index = fibonacci_search(arr, value); + passed = passed && (index == -1); + // one element + arr = {1}; + value = 2; + index = fibonacci_search(arr, value); + passed = passed && (index == -1); + // empty array + arr.clear(); + value = 10; + index = fibonacci_search(arr, value); + passed = passed && (index == -1); + + return passed; +} + + +/** + * Main Function + * testing the algorithm + */ +int main() { + assert(one_occurence_test()); + assert(many_occurence_test()); + assert(no_occurence_test()); return 0; } From 8e53f4da833c4e5410a74e26dce8b15cb46b41a0 Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 17:54:55 +0400 Subject: [PATCH 05/10] converted from vector to array --- search/fibonacci_search.cpp | 65 ++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index 2a3ba7207..5cdc93ae9 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -23,13 +23,12 @@ int min(int first, int second){ } /** - * Input: a sorted array and a value + * Input: a sorted array, a value we're looking for and size of the array * Output: if the array contains the value, returns its index * else returns -1 */ -int fibonacci_search(std::vector &arr, int value){ +int fibonacci_search(int *arr, int value, int length){ int last = 0, current = 1, offset = -1, index; - int length = arr.size(); int next = last + current; while(next < length){ @@ -53,7 +52,7 @@ int fibonacci_search(std::vector &arr, int value){ return index; } } - if(current && !arr.empty() && arr[offset+1] == value){ + if(current && (length > 0) && arr[offset+1] == value){ return offset+1; } return -1; @@ -64,27 +63,29 @@ int fibonacci_search(std::vector &arr, int value){ */ bool one_occurence_test(){ // declarations - int value, index; + int value, index, length; bool passed = true; - std::vector arr; // last element - arr = {1, 2, 3}; + length = 3; + int arr1[length] = {1, 2, 3}; value = 3; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr1, value, length); passed = passed && (index == 2); // first element value = 1; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr1, value, length); passed = passed && (index == 0); // somewhere in the middle element - arr = {1, 3, 5, 7}; + length = 4; + int arr2[length] = {1, 3, 5, 7}; value = 3; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr2, value, length); passed = passed && (index == 1); // arr size is 1 - arr = {10}; + length = 1; + int arr3[length] = {10}; value = 10; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr3, value, length); passed = passed && (index == 0); return passed; @@ -95,27 +96,29 @@ bool one_occurence_test(){ */ bool many_occurence_test(){ // declarations - int value, index; + int value, index, length; bool passed = true; - std::vector arr; // last element - arr = {1, 1, 10, 10}; + length = 4; + int arr1[length] = {1, 1, 10, 10}; value = 10; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr1, value, length); passed = passed && (index == 2 || index == 3); // first element value = 1; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr1, value, length); passed = passed && (index == 0 || index == 1); // somewhere in the middle element - arr = {1, 3, 3, 7}; + length = 4; + int arr2[length] = {1, 3, 3, 7}; value = 3; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr2, value, length); passed = passed && (index == 1 || index == 2); // all elements are the same - arr = {10, 10, 10}; + length = 3; + int arr3[length] = {10, 10, 10}; value = 10; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr3, value, length); passed = passed && (index >= 0 && index <= 2); return passed; @@ -126,23 +129,25 @@ bool many_occurence_test(){ */ bool no_occurence_test(){ // declarations - int value, index; + int value, index, length; bool passed = true; - std::vector arr; // many elements - arr = {1, 2, 4, 5}; + length = 4; + int arr1[length] = {1, 2, 4, 5}; value = 3; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr1, value, length); passed = passed && (index == -1); // one element - arr = {1}; + length = 1; + int arr2[length] = {1}; value = 2; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr2, value, length); passed = passed && (index == -1); // empty array - arr.clear(); + length = 0; + int arr3[length]; value = 10; - index = fibonacci_search(arr, value); + index = fibonacci_search(arr3, value, length); passed = passed && (index == -1); return passed; From 4b3749ed49d863bbfd2090c56151586962c7b78d Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 17:59:49 +0400 Subject: [PATCH 06/10] variable-length arrays fixed --- search/fibonacci_search.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index 5cdc93ae9..f8525a6b4 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -67,7 +67,7 @@ bool one_occurence_test(){ bool passed = true; // last element length = 3; - int arr1[length] = {1, 2, 3}; + int arr1[3] = {1, 2, 3}; value = 3; index = fibonacci_search(arr1, value, length); passed = passed && (index == 2); @@ -77,13 +77,13 @@ bool one_occurence_test(){ passed = passed && (index == 0); // somewhere in the middle element length = 4; - int arr2[length] = {1, 3, 5, 7}; + int arr2[4] = {1, 3, 5, 7}; value = 3; index = fibonacci_search(arr2, value, length); passed = passed && (index == 1); // arr size is 1 length = 1; - int arr3[length] = {10}; + int arr3[1] = {10}; value = 10; index = fibonacci_search(arr3, value, length); passed = passed && (index == 0); @@ -100,7 +100,7 @@ bool many_occurence_test(){ bool passed = true; // last element length = 4; - int arr1[length] = {1, 1, 10, 10}; + int arr1[4] = {1, 1, 10, 10}; value = 10; index = fibonacci_search(arr1, value, length); passed = passed && (index == 2 || index == 3); @@ -110,13 +110,13 @@ bool many_occurence_test(){ passed = passed && (index == 0 || index == 1); // somewhere in the middle element length = 4; - int arr2[length] = {1, 3, 3, 7}; + int arr2[4] = {1, 3, 3, 7}; value = 3; index = fibonacci_search(arr2, value, length); passed = passed && (index == 1 || index == 2); // all elements are the same length = 3; - int arr3[length] = {10, 10, 10}; + int arr3[3] = {10, 10, 10}; value = 10; index = fibonacci_search(arr3, value, length); passed = passed && (index >= 0 && index <= 2); @@ -133,19 +133,19 @@ bool no_occurence_test(){ bool passed = true; // many elements length = 4; - int arr1[length] = {1, 2, 4, 5}; + int arr1[4] = {1, 2, 4, 5}; value = 3; index = fibonacci_search(arr1, value, length); passed = passed && (index == -1); // one element length = 1; - int arr2[length] = {1}; + int arr2[1] = {1}; value = 2; index = fibonacci_search(arr2, value, length); passed = passed && (index == -1); // empty array length = 0; - int arr3[length]; + int arr3[10] = {}; value = 10; index = fibonacci_search(arr3, value, length); passed = passed && (index == -1); From 8216a3267fb35d471b38b657c9e4aca7a819f5e6 Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 20:38:40 +0400 Subject: [PATCH 07/10] randomized tests, documentation, back to vector --- search/fibonacci_search.cpp | 161 ++++++++++++------------------------ 1 file changed, 54 insertions(+), 107 deletions(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index f8525a6b4..e2796b0ba 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -1,6 +1,6 @@ /** * Copyright 2020 @author sprintyaf - * @file + * @file fibonacci_search.cpp * @brief [Fibonacci search * algorithm](https://en.wikipedia.org/wiki/Fibonacci_search_technique) */ @@ -8,27 +8,21 @@ #include #include // for std::vector class #include // for assert +#include // for random numbers +#include // for sorting /** - * Returns minimum of the two numbers + * @brief using fibonacci search algorithm finds an index of a given element in a sorted array + * + * @param arr sorted array + * @param value value that we're looking for + * @returns if the array contains the value, returns an index of the element. otherwise -1. */ -int min(int first, int second){ - if(first < second){ - return first; - } else { - return second; - } -} - -/** - * Input: a sorted array, a value we're looking for and size of the array - * Output: if the array contains the value, returns its index - * else returns -1 - */ -int fibonacci_search(int *arr, int value, int length){ +int fibonacci_search(const std::vector &arr, int value){ int last = 0, current = 1, offset = -1, index; + int length = arr.size(); int next = last + current; while(next < length){ @@ -38,7 +32,7 @@ int fibonacci_search(int *arr, int value, int length){ } while(next > 1){ - index = min(offset + last, length-1); + index = std::min(offset + last, length-1); if(arr[index] < value){ next = current; current = last; @@ -52,115 +46,68 @@ int fibonacci_search(int *arr, int value, int length){ return index; } } - if(current && (length > 0) && arr[offset+1] == value){ + if(current && !arr.empty() && arr[offset+1] == value){ return offset+1; } return -1; } /** - * Tests where given value occurs only once - */ -bool one_occurence_test(){ - // declarations - int value, index, length; + * @brief random tests for checking performance when an array doesn't contain an element +*/ +bool no_occurence_tests(){ bool passed = true; - // last element - length = 3; - int arr1[3] = {1, 2, 3}; - value = 3; - index = fibonacci_search(arr1, value, length); - passed = passed && (index == 2); - // first element - value = 1; - index = fibonacci_search(arr1, value, length); - passed = passed && (index == 0); - // somewhere in the middle element - length = 4; - int arr2[4] = {1, 3, 5, 7}; - value = 3; - index = fibonacci_search(arr2, value, length); - passed = passed && (index == 1); - // arr size is 1 - length = 1; - int arr3[1] = {10}; - value = 10; - index = fibonacci_search(arr3, value, length); - passed = passed && (index == 0); - + int rand_num, rand_value, index, num_tests = 1000; + std::vector arr; + while(num_tests--){ + arr.clear(); + for(int i = 0; i < 100; i++){ + rand_num = rand() % 1000; + arr.push_back(rand_num); + } + rand_value = rand() % 1000; + while(std::find(arr.begin(), arr.end(), rand_value) != arr.end()){ + std::remove(arr.begin(), arr.end(), rand_value); + } + sort(arr.begin(), arr.end()); + index = fibonacci_search(arr, rand_value); + passed = passed && (index == -1); + } return passed; } /** - * Tests where given value occurs more than once - */ -bool many_occurence_test(){ - // declarations - int value, index, length; + * @brief random tests which cover cases when we have one, multiple or zero occurences of the value we're looking for +*/ +bool random_tests(){ bool passed = true; - // last element - length = 4; - int arr1[4] = {1, 1, 10, 10}; - value = 10; - index = fibonacci_search(arr1, value, length); - passed = passed && (index == 2 || index == 3); - // first element - value = 1; - index = fibonacci_search(arr1, value, length); - passed = passed && (index == 0 || index == 1); - // somewhere in the middle element - length = 4; - int arr2[4] = {1, 3, 3, 7}; - value = 3; - index = fibonacci_search(arr2, value, length); - passed = passed && (index == 1 || index == 2); - // all elements are the same - length = 3; - int arr3[3] = {10, 10, 10}; - value = 10; - index = fibonacci_search(arr3, value, length); - passed = passed && (index >= 0 && index <= 2); - + int rand_num, rand_value, index, real_value, num_tests = 10000; + std::vector arr; + while(num_tests--){ + arr.clear(); + for(int i = 0; i < 100; i++){ + rand_num = rand() % 1000; + arr.push_back(rand_num); + } + rand_value = rand() % 1000; + sort(arr.begin(), arr.end()); + index = fibonacci_search(arr, rand_value); + if(index != -1){ + real_value = arr[index]; + passed = passed && (real_value == rand_value); + } else { + passed = passed && (std::find(arr.begin(), arr.end(), rand_value) == arr.end()); + } + } return passed; } -/** - * Tests where the array doesn't contain given value - */ -bool no_occurence_test(){ - // declarations - int value, index, length; - bool passed = true; - // many elements - length = 4; - int arr1[4] = {1, 2, 4, 5}; - value = 3; - index = fibonacci_search(arr1, value, length); - passed = passed && (index == -1); - // one element - length = 1; - int arr2[1] = {1}; - value = 2; - index = fibonacci_search(arr2, value, length); - passed = passed && (index == -1); - // empty array - length = 0; - int arr3[10] = {}; - value = 10; - index = fibonacci_search(arr3, value, length); - passed = passed && (index == -1); - - return passed; -} - - /** * Main Function * testing the algorithm */ int main() { - assert(one_occurence_test()); - assert(many_occurence_test()); - assert(no_occurence_test()); + assert(no_occurence_tests()); + assert(random_tests()); return 0; } From cf6b77a7dce59afb35cb62a8bb6bc780ad958ec5 Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 20:45:03 +0400 Subject: [PATCH 08/10] rand fixed --- search/fibonacci_search.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index e2796b0ba..067d8e636 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -62,10 +62,10 @@ bool no_occurence_tests(){ while(num_tests--){ arr.clear(); for(int i = 0; i < 100; i++){ - rand_num = rand() % 1000; + rand_num = std::rand() % 1000; arr.push_back(rand_num); } - rand_value = rand() % 1000; + rand_value = std::rand() % 1000; while(std::find(arr.begin(), arr.end(), rand_value) != arr.end()){ std::remove(arr.begin(), arr.end(), rand_value); } @@ -86,11 +86,11 @@ bool random_tests(){ while(num_tests--){ arr.clear(); for(int i = 0; i < 100; i++){ - rand_num = rand() % 1000; + rand_num = std::rand() % 1000; arr.push_back(rand_num); } - rand_value = rand() % 1000; - sort(arr.begin(), arr.end()); + rand_value = std::rand() % 1000; + std::sort(arr.begin(), arr.end()); index = fibonacci_search(arr, rand_value); if(index != -1){ real_value = arr[index]; From 644524337a9112491b59d87df3890d276078007d Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 22:53:12 +0400 Subject: [PATCH 09/10] copyright removed --- search/fibonacci_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index 067d8e636..dd1b06f11 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -1,5 +1,5 @@ /** - * Copyright 2020 @author sprintyaf + * @author sprintyaf * @file fibonacci_search.cpp * @brief [Fibonacci search * algorithm](https://en.wikipedia.org/wiki/Fibonacci_search_technique) From 58099884dd626ba94602fa00351d8e6c5f49c9e9 Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Fri, 10 Jul 2020 19:13:16 +0400 Subject: [PATCH 10/10] comments added --- search/fibonacci_search.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/search/fibonacci_search.cpp b/search/fibonacci_search.cpp index dd1b06f11..be181a47d 100644 --- a/search/fibonacci_search.cpp +++ b/search/fibonacci_search.cpp @@ -21,34 +21,48 @@ * @returns if the array contains the value, returns an index of the element. otherwise -1. */ int fibonacci_search(const std::vector &arr, int value){ - int last = 0, current = 1, offset = -1, index; - int length = arr.size(); - int next = last + current; + // initialize last and current members of Fibonacci sequence + int last = 0, current = 1; + int length = arr.size(); // array size + // next member of Fibonacci sequence which is "last" + "current" + int next = last + current; + // "next" will store the smallest Fibonacci number greater or equal to "length" while(next < length){ last = current; current = next; next = last + current; } + // "offset" is the end of eliminated range from front + int offset = -1, index; + // while loop until there are elements left to consider. + // when "next" becomes 1, last is equal to 0, so search is done, + // because arr[offset] will already be eliminated while(next > 1){ + // check if "last" is valid location index = std::min(offset + last, length-1); + // if value is greater than the value at "index", eliminate the subarray from offset to index if(arr[index] < value){ next = current; current = last; last = next - current; offset = index; + // if value is less than the value at "index", eliminate the subarray after index+1 } else if(arr[index] > value){ next = last; current = current - last; last = next - current; + // element is found } else { return index; } } + // comparing the last element if(current && !arr.empty() && arr[offset+1] == value){ return offset+1; } + // value was not found, return -1 return -1; }