Serial and binary search algorithm


Searching starts with the first item and then moves to each item in turn until either a match is found or the search reaches the end of the data set with no match found. A criteria is set up before the search begins. If no match is found, then the process will return the appropriate message.

Serial search is fairly simple to code. For example the pseudo-code below shows the algorithm in action. Each item is checked until either a match is found or the loop ends and the 'return 'no match found' is reached. Good performance over small to medium lists. Computers are now very powerful and so checking potentially every element in the list for a match may not be an issue with lists of moderate length.

The list does not need to be in any order. Other algorithms only work because they assume that the list is ordered in a certain way. Serial searching makes no assumption at all about the list so it will work just as well with a randomly arranged list as an ordered list. Not affected by insertions and deletions. Some algorithms assume the list is ordered in a certain way.

So if an item is inserted or deleted, the computer will need to re-order the list before that algorithm can be applied. The overhead of doing this may actually mean that serial searching performs better than other methods. So what you do is make a guess - J is about halfway down the alphabet and so you open the telephone book around half way. The page you see has names starting with N. So you know J will be in the first half of the book.

Next you open a page about halfway down the first half - the page has 'H'. So now Jones must be in the upper half of this section. You are carrying out a 'Binary search' algorithm. Notice that after only two guesses you are getting much closer to the answer. If you were carrying out a serial search, you would still be at page 2. If the list is large and changing often, with items constantly being added or deleted, then the time it takes to constantly re-order the list to allow for a binary search might be longer than a simple serial search in the first place.

If the list is large and static e. If the list is small then it might be simpler to just use a linear search 4. If the list is random, then linear is the only way 5.