1. Begin iterating through the list
2. Compare each element to n
3. If an element is equal to n, return true
4. If none of the elements in the list is equal to n, return false
Here is a simple example function written in Python
def hasN(n, a):
for x in a: # for each element x in list a
if x is n: # if the element is equal to n, return true
return True
return False # since none of the elements returned true, n is not in list a
This algorithm will always determine if n is in list a since it checks every element in a to see if it is n. Since this algorithm at most makes one comparison per element, this algorithm is finite and will take O(n) time.
For those of you who were wondering what my title means, U+0043 U+0051 are unicode notations for C and Q
perfect.
ReplyDelete