From 8e53f4da833c4e5410a74e26dce8b15cb46b41a0 Mon Sep 17 00:00:00 2001 From: sprintyaf Date: Tue, 7 Jul 2020 17:54:55 +0400 Subject: [PATCH] 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;