converted from vector to array

This commit is contained in:
sprintyaf 2020-07-07 17:54:55 +04:00
parent 86c6af46e8
commit 8e53f4da83

View File

@ -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 * Output: if the array contains the value, returns its index
* else returns -1 * else returns -1
*/ */
int fibonacci_search(std::vector<int> &arr, int value){ int fibonacci_search(int *arr, int value, int length){
int last = 0, current = 1, offset = -1, index; int last = 0, current = 1, offset = -1, index;
int length = arr.size();
int next = last + current; int next = last + current;
while(next < length){ while(next < length){
@ -53,7 +52,7 @@ int fibonacci_search(std::vector<int> &arr, int value){
return index; return index;
} }
} }
if(current && !arr.empty() && arr[offset+1] == value){ if(current && (length > 0) && arr[offset+1] == value){
return offset+1; return offset+1;
} }
return -1; return -1;
@ -64,27 +63,29 @@ int fibonacci_search(std::vector<int> &arr, int value){
*/ */
bool one_occurence_test(){ bool one_occurence_test(){
// declarations // declarations
int value, index; int value, index, length;
bool passed = true; bool passed = true;
std::vector<int> arr;
// last element // last element
arr = {1, 2, 3}; length = 3;
int arr1[length] = {1, 2, 3};
value = 3; value = 3;
index = fibonacci_search(arr, value); index = fibonacci_search(arr1, value, length);
passed = passed && (index == 2); passed = passed && (index == 2);
// first element // first element
value = 1; value = 1;
index = fibonacci_search(arr, value); index = fibonacci_search(arr1, value, length);
passed = passed && (index == 0); passed = passed && (index == 0);
// somewhere in the middle element // somewhere in the middle element
arr = {1, 3, 5, 7}; length = 4;
int arr2[length] = {1, 3, 5, 7};
value = 3; value = 3;
index = fibonacci_search(arr, value); index = fibonacci_search(arr2, value, length);
passed = passed && (index == 1); passed = passed && (index == 1);
// arr size is 1 // arr size is 1
arr = {10}; length = 1;
int arr3[length] = {10};
value = 10; value = 10;
index = fibonacci_search(arr, value); index = fibonacci_search(arr3, value, length);
passed = passed && (index == 0); passed = passed && (index == 0);
return passed; return passed;
@ -95,27 +96,29 @@ bool one_occurence_test(){
*/ */
bool many_occurence_test(){ bool many_occurence_test(){
// declarations // declarations
int value, index; int value, index, length;
bool passed = true; bool passed = true;
std::vector<int> arr;
// last element // last element
arr = {1, 1, 10, 10}; length = 4;
int arr1[length] = {1, 1, 10, 10};
value = 10; value = 10;
index = fibonacci_search(arr, value); index = fibonacci_search(arr1, value, length);
passed = passed && (index == 2 || index == 3); passed = passed && (index == 2 || index == 3);
// first element // first element
value = 1; value = 1;
index = fibonacci_search(arr, value); index = fibonacci_search(arr1, value, length);
passed = passed && (index == 0 || index == 1); passed = passed && (index == 0 || index == 1);
// somewhere in the middle element // somewhere in the middle element
arr = {1, 3, 3, 7}; length = 4;
int arr2[length] = {1, 3, 3, 7};
value = 3; value = 3;
index = fibonacci_search(arr, value); index = fibonacci_search(arr2, value, length);
passed = passed && (index == 1 || index == 2); passed = passed && (index == 1 || index == 2);
// all elements are the same // all elements are the same
arr = {10, 10, 10}; length = 3;
int arr3[length] = {10, 10, 10};
value = 10; value = 10;
index = fibonacci_search(arr, value); index = fibonacci_search(arr3, value, length);
passed = passed && (index >= 0 && index <= 2); passed = passed && (index >= 0 && index <= 2);
return passed; return passed;
@ -126,23 +129,25 @@ bool many_occurence_test(){
*/ */
bool no_occurence_test(){ bool no_occurence_test(){
// declarations // declarations
int value, index; int value, index, length;
bool passed = true; bool passed = true;
std::vector<int> arr;
// many elements // many elements
arr = {1, 2, 4, 5}; length = 4;
int arr1[length] = {1, 2, 4, 5};
value = 3; value = 3;
index = fibonacci_search(arr, value); index = fibonacci_search(arr1, value, length);
passed = passed && (index == -1); passed = passed && (index == -1);
// one element // one element
arr = {1}; length = 1;
int arr2[length] = {1};
value = 2; value = 2;
index = fibonacci_search(arr, value); index = fibonacci_search(arr2, value, length);
passed = passed && (index == -1); passed = passed && (index == -1);
// empty array // empty array
arr.clear(); length = 0;
int arr3[length];
value = 10; value = 10;
index = fibonacci_search(arr, value); index = fibonacci_search(arr3, value, length);
passed = passed && (index == -1); passed = passed && (index == -1);
return passed; return passed;