Document Here - Harry Mills Portfolio

advertisement
Search Algorithms
Searching is the basics of most of the worlds computing applications from googles web searching to
searching for information in a database. So it is a very important part of computer science, below is
my research into two main search algorithms, the linear search and the binary search.
Linear Search
Linear search is the method of searching a list of data items one by one from the start until the
correct item is found. This method of searching does not require the list of data items to be in any
order at all. This means that programming this search algorithm is very easy compared to others.
However the search time can be very long if the list of data items is big and the desired item is right
at the end, this searching method is only advised for short lists as time could be an issue.
Below is the pseudo code for a linear search of a list:
FOR all items in list
IF current item = search item
RETURN position
ELSE IF current item = last item
RETURN not in list
ELSE
NEXT item
END IF
END FOR
Figure 1. Linear Search (C# Corner 2012)
Binary Search
Binary search is another method of searching which is usually more efficient than linear search. This
method however requires the list of data items to be in order. It chooses a random place within the
list (usually the midpoint) and then decides to search left or right of that by comparing the data item
against the search item. By doing this it cuts the amount of data items in the list by half with each
comparison, this means it does not need to compare every item only a select few. It will repeat this
process until the item is found or there are no longer any items in the list, at which point it can
confirm it is not in the original list. Binary search normally requires recursion and the data items
must be sorted so programming it is a lot more complex than a linear search. The list must be sorted
before searching and any items added must be added smartly or else this algorithm will not work.
This method is a lot more efficient than linear search on large list although linear search can be more
efficient if the required item is at the start of the list.
Below is the pseudo code for a binary search of a list:
FUNCTION BinarySearch(list)
GET midpoint of list
IF midpoint > search item
CREATE newlist of every item to left
REPEAT BinarySearch(newlist)
ELSE IF midpoint < search item
CREATE newlist of every item to right
REPEAT BinarySearch(newlist)
ELSE IF midpoint = search item
RETURN item
ELSE IF list length = 1 AND midpoint <> search item
RETURN not in list
END IF
END FUNCTION
Figure 2. Binary Search (Peter Drake, 2005)
List of References
Karl Seguin (n.d.) Linear Search/Binary Search [online] available from <http://algorithms.
openmymind.net/search/linear.html> [20 January 2015]
Cprogramming (n.d.) Linear Search, Binary Search and other Searching Techniques [online] available
from <http://www.cprogramming.com/discussionarticles/sorting_and_searching.html> [20
January 2015]
C# Corner (25 March 2012) Linear Search in Java [online] available from <http://www.csharpcorner.com/UploadFile/433c33/linear-search-in-java/> [20 January 2015]
Peter Drake (2005) Data Structures and Algorithms in Java.
United States: Prentice Hall
Download