Hello all, I need help with my pseucode recursion for a search program for my class. This type of search is a sequential type of search. You give the program an array of n elements and a value to search for, and it will return the location of the element (only one match needed). If it is not found then it will return a -1. Here is the non-recursive one below followed by the recursive one I made (if anyone can tell me my mistakes): Here it runs from the end of the array instead of the start.
Non-Recursive:
Algorithm SS(A,n,X,B)
Input: A[n] integers and integer X (X is the one we want to find in the
array)
Output: Integer B such that B is largest integer such that A[B] = X or B =
-1 if A contains no X
For B=n-1 to 0 do
If A[B] = X then
Return I
Return I
Recursive:
Algorithm recursiveSS(A,n,X,B)
Input: A[n] integers and integer X (X is the one we want to find in the
array)
Output: Integer B such that B is largest integer such that A[B] = X or B =
-1 if A contains no X
B <-- n-1
If B = -1 //Test and see if B == -1
Return B //Return -1 if B == -1 is true
If A[B] = X then //compare
Return B //Return the index of where the first match occurs in the
array as its looked at from n-1 to 0
Return recursiveSS( A, n-1, X, B)
I don't see any code, butidonot.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks