Search Algorithms


Comparision of the efficiancy of Binary Search (top), versus the efficiancy of sequential
search (bottom). The cell that is being checked for the value is being marked with 'X'.

For the purposes of ICS AP two searching algorithms need to be learned. Sequential search, and Binary Search. Both these algorithms are used to find the index of a specified value in an array. They find the needle in the haystack, if you will.

Each of those two sorting algorithms have their own drawbacks, and should be used in specific situations. There is no truly optimal search algorithm for every situation. Both of these searching algorithms have their advantages and their drawbacks.

For the purposes of this tutorial, if a loop fails to find the element in the array, the value returned will be -1. This value is chosen as -1 can never be an index in Java, yet it can still be stored within the int data type.


Sequential Search