The sequential search algorithm given in this chapter is nonrecursive. Here is a
ID: 3626453 • Letter: T
Question
The sequential search algorithm given in this chapter is nonrecursive. Here is an example of the nonrecursive algorithm:template <class elemType>
int seqSearch(const elemType list[ ], int length,
const elemType& item)
{
int loc;
bool found = false;
loc = 0;
while (loc < length && !found)
if (list[loc] == item)
found = true;
else
loc++;
if (found)
return loc;
else
return -1;
} //end seqSearch
I need to write and implement a recursive version of the sequential search algorithm.
Explanation / Answer
template <class elemType>
int seqSearch(const elemType list[], int length,const elemType& item)
{
if(length>=0)
{
if (list[length-1] == item)
return length-1;
else
return seqSearch(list,length-1,item);
}
return -1;
} //end seqSearch
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.