Sunday, September 19, 2010

Write an algorithm to search for a number in a list of given numbers

Search for a number in a list [a1, a2, a3, an] of given number [k]





1.Compare k with a1(first number in a list)
2.If k and number in a list is equal, do the step 4, else number in a list is not equal as k, do the step 3.
3.If k and a1(number that pick in a list) isn't equal, repeat step 1~2 with next number in a list.
4.If return "YES", display that number.





This algorithm is correct, because if we compare given number with each number in a list(al, a2, a3, an), we can find number that equal with given number. Also, this is finiteness because it should find correct number with repeat least n times(this case, n times is 4 times).









[I am living in US almost 6 years, but I'm still not very well English-speaker and take hard time with grammer, therefore, maybe my algorithm with explain for correctness and finiteness are hard to read and understand. But believe I got this algorithm]

No comments:

Post a Comment