mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
converted from vector to array
This commit is contained in:
parent
86c6af46e8
commit
8e53f4da83
@ -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<int> &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<int> &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<int> &arr, int value){
|
||||
*/
|
||||
bool one_occurence_test(){
|
||||
// declarations
|
||||
int value, index;
|
||||
int value, index, length;
|
||||
bool passed = true;
|
||||
std::vector<int> 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<int> 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<int> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user