Recursive call that returns the beginning and end of a vector

Asked

Viewed 46 times

-2

Good afternoon! I’m very lost in this matter of a college job because I didn’t understand right recursion. Could someone help me?
I need it here: A function that returns the index in which the searched element is found or -1 if the element is not present. It takes, as an argument, the array of integers, the beginning and end of the interval analyzed at each recursion, and the value to be searched for.
My code is like this so far (basically I took it off the internet because I didn’t know where to start)

#include <stdio.h>
#define TAMANHO 6

int bin_search (int *v, int begin, int end, int value){
    int i = (begin + end)/2; //meio do vetor


    if (begin > end){
        return -1; //se o inicio for maior que o fim > -1 (não é decrescente)
    }
    if (v[i] == value){  //vetor do meio = valor?
        return i;
    }
    if(v[i] < value){ //se o valor for maior que o vetor do meio = fim do vetor
        return bin_search(v, i+1,end,value);
    }
    else if (v[i] > value) { //se o valor de i for maior que valor = inicio do vetor.
        return bin_search(v, begin, i-1,value);
    }
}

int main (){
    int v [TAMANHO] = {1, 3, 5, 10, 20, 30};
    int i,begin,end;

    for(i = 0; i < TAMANHO; i++){
        int value = v[i];            

        printf("Busca binaria recursiva: %d\n",value,bin_search(v,0,TAMANHO -1, value));    
    }    
}

How do I return the start and the end? Data entry is also a little complicated for me.
Thanks since already <3

1 answer

1

To understand recursion we use a repetition loop to achieve the same goal.

#include <stdio.h>
#define TAMANHO 6

int search (int *v, int begin, int end, int value) {
    int i = begin;
    for (i = begin; i < end; i++) {
        if(v[i] == value) return i;
    }
    return -1;
}

int main() {
    int v [TAMANHO] = {1, 3, 5, 10, 20, 30};
    int i;

    for(i = 0; i < TAMANHO; i++) {
        int value = v[i];            

        printf("Busca linear encontrou %d em v[%d]\n", value, search(v, 0, TAMANHO, value));
    }
    return 0;
}

The algorithm checks each number and returns the index of that number or -1 if you have checked all of them. Reimplementing this algorithm recursively is a matter of identifying the base cases if(v[i] == value) return i; and if(!(i < end)) return -1, the last step of for, and identify the next step which is to continue searching on the list with another index.

#include <stdio.h>
#define TAMANHO 6

int search (int *v, int begin, int end, int value) {
   int i = begin;
   // Retorne -1 caso não encontremos em toda a lista
   if (begin > end) {
       return -1; 
   }
   // Retorne o índice caso encontremos o valor 
   if (v[i] == value) { 
       return i;
   }
   // Continue a procura
   return search(v, i + 1, end, value);
}

int main () {
   int v [TAMANHO] = {1, 3, 5, 10, 20, 30};
   int i;

   for(i = 0; i < TAMANHO; i++) {
       int value = v[i];            

       printf("Busca recursiva encontrou %d em v[%d]\n", value, search(v, 0, TAMANHO, value));
   }
   printf("Busca recursiva de %d retornou %d\n", 2, search(v, 0, TAMANHO, 2));
   return 0;
}

As the description of the problem you have does not guarantee the increasing ordering of the items in the list, I have no way to implement the function using the binary search algorithm, which should not be difficult to find over the internet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.