# Recursive sequential search

Printable View

• 10-10-2002
supaben34
Recursive sequential search
Hi there all,
I have to implement a recursive sequential that searches for an element in an UNSORTED array and displays the position of that element. This is my code so far.
Code:

```//'listSize' is a parameter that inputs size of the array into function //'item' is a parameter that inputs the current value of the function //list is the array itself and therefore is also a pointer //returns the position of the array that we are searching for   int sequentialSearch(int item, int* list, int listSize){     if (listSize == 0){       return -1;     }     if (*list == item){//if first element is equal to listSize       return 0;     }         else sequentialSearch(int item, int* list, int listSize){       list = list + 1;     }   }```
If there is no matching elements we are supposed to return a -1. Would someone please help me with this? Thank you.
• 10-10-2002
bonkey
You need to reduce the list by one item.

If( item == *list) // ot sure if this is what you want, but some comparison to see if you have found your item
{
// found code
return(0);
}
else
{
sequentialSearch(item, list[1], listSize);
}

You have to think of each call to your function independently. Don't think about the recursion. Just think my function will check if the item that is passed equals the first item in my list. If so I found it. Otherwise I will call the function but without the item that I know is not it.
• 10-10-2002
Davros
Don't follow the section of your code which goes:

else sequentialSearch(int item, int* list, int listSize){
list = list + 1;

However, I think I get what you're trying to do.

So who say you have to implement a sequential search in this way? This is terrible. If your list is big, then stack overflow errors will ensue.

Why not just run down a for loop checking for list[n] == item?