If the position of the element found is a positive number or zero num_positivo
and if it is not found it is -1
the max(-1,num_positivo)=num_positivo
, max is the maximum between two numbers
rsearch
works calculating the center of the vector if the value sought is the center returns the position of this, if it does not look recursively with rsearch to the left and rsearch to the right as the indices of an array are positive the maximum it will always be the value found if it occurs
template<class T>
int vector_t<T>:: rsearch(int i, int d, const T& x){
//i es izquierda d derecha y x el valor a buscar c de centro
int c=-1;//valor por defecto
if (i<=d){ // si divides el vector puede que no halla una mitad
c=(i+d)/2;//Toma el elemento del centro de la lista
if (v_[c] !=x){ //Si No es el elemento del centro
int c1=rsearch(i,c-1,x); //Buscar en la lista a la izquierda
int c2=rsearch(c+1,d,x); //Buscar en la lista a la derecha
c= max(c1,c2);// si es mayor que -1 es que encontro el Elemento
}
}
return c;//Si c no fue encontrado devuelve -1 q era el valor original
}
This function forces to execute the search to the right even if it has been found on the left, it would be better this way:
template<class T>
int vector_t<T>:: rsearch(int i, int d, const T& x){
int c=-1;//valor por defecto
if (i<=d){ // si divides el vector puede que no halla una mitad
c=(i+d)/2;//Toma el elemento del centro de la lista
if (v_[c] !=x){
c=rsearch(i,c-1,x);
if(c==-1)c=rsearch(c+1,d,x); //Si no lo encontro al la izquierda buscar a la derecha
}
}
return c;
}
And I personally prefer this style of code:
template<class T>
int vector_t<T>:: rsearch(int i, int d, const T& x){
if (i>d)return -1; //El indice de la izquierda no puede ser mayor que el de la derecha
int c=(i+d)/2;//El centro de la lista si i==d es 2i/2 ==i==d
if (v_[c] ==x)return c;
c=rsearch(i,c-1,x);
if(c!=-1)return c;
c=rsearch(c+1,d,x);
return c;
}