TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
fibonacci_search.cpp
Go to the documentation of this file.
1
8#include <iostream>
9#include <vector> // for std::vector class
10#include <cassert> // for assert
11#include <cstdlib> // for random numbers
12#include <algorithm> // for sorting
13
14
15
23int fibonacci_search(const std::vector<int> &arr, int value){
24 // initialize last and current members of Fibonacci sequence
25 int last = 0, current = 1;
26 int length = arr.size(); // array size
27 // next member of Fibonacci sequence which is "last" + "current"
28 int next = last + current;
29
30 // "next" will store the smallest Fibonacci number greater or equal to "length"
31 while(next < length){
32 last = current;
33 current = next;
34 next = last + current;
35 }
36
37 // "offset" is the end of eliminated range from front
38 int offset = -1, index;
39 // while loop until there are elements left to consider.
40 // when "next" becomes 1, last is equal to 0, so search is done,
41 // because arr[offset] will already be eliminated
42 while(next > 1){
43 // check if "last" is valid location
44 index = std::min(offset + last, length-1);
45 // if value is greater than the value at "index", eliminate the subarray from offset to index
46 if(arr[index] < value){
47 next = current;
48 current = last;
49 last = next - current;
50 offset = index;
51 // if value is less than the value at "index", eliminate the subarray after index+1
52 } else if(arr[index] > value){
53 next = last;
54 current = current - last;
55 last = next - current;
56 // element is found
57 } else {
58 return index;
59 }
60 }
61 // comparing the last element
62 if(current && !arr.empty() && arr[offset+1] == value){
63 return offset+1;
64 }
65 // value was not found, return -1
66 return -1;
67}
68
73 bool passed = true;
74 int rand_num, rand_value, index, num_tests = 1000;
75 std::vector<int> arr;
76 while(num_tests--){
77 arr.clear();
78 for(int i = 0; i < 100; i++){
79 rand_num = std::rand() % 1000;
80 arr.push_back(rand_num);
81 }
82 rand_value = std::rand() % 1000;
83 while(std::find(arr.begin(), arr.end(), rand_value) != arr.end()){
84 std::remove(arr.begin(), arr.end(), rand_value);
85 }
86 sort(arr.begin(), arr.end());
87 index = fibonacci_search(arr, rand_value);
88 passed = passed && (index == -1);
89 }
90 return passed;
91}
92
97 bool passed = true;
98 int rand_num, rand_value, index, real_value, num_tests = 10000;
99 std::vector<int> arr;
100 while(num_tests--){
101 arr.clear();
102 for(int i = 0; i < 100; i++){
103 rand_num = std::rand() % 1000;
104 arr.push_back(rand_num);
105 }
106 rand_value = std::rand() % 1000;
107 std::sort(arr.begin(), arr.end());
108 index = fibonacci_search(arr, rand_value);
109 if(index != -1){
110 real_value = arr[index];
111 passed = passed && (real_value == rand_value);
112 } else {
113 passed = passed && (std::find(arr.begin(), arr.end(), rand_value) == arr.end());
114 }
115 }
116 return passed;
117}
118
123int main() {
124 assert(no_occurence_tests());
125 assert(random_tests());
126 return 0;
127}
int fibonacci_search(const std::vector< int > &arr, int value)
using fibonacci search algorithm finds an index of a given element in a sorted array
bool random_tests()
random tests which cover cases when we have one, multiple or zero occurences of the value we're looki...
bool no_occurence_tests()
random tests for checking performance when an array doesn't contain an element
int main()