Searching Algorithms

Determining whether an element is included within a collection is a common task. Indeed, most collection-type classes offer a contains() method for this very reason. There are two common strategies (algorithms) for searching a collection for a given element: the sequential search and the binary search.

The Sequential (Linear) Search.

In the sequential search, inspection begins with the first element and proceeds until the element is found or the end of the collection is encountered. If the element if found, typically the algorithm returns the position of index of the location in the collection. If the element is not found, a value of -1 is returned.

  1. Create the Java project SearchingAndSorting and drop in this driver.
  2. Implement the body of the createRandomIntArray(int length, int n) method and add statements in the main method to confirm its functionality.
  3. Implement the body of the sequentialSearch(int[] array, int key) method and add statements in the main method to confirm its functionality.
  4. Call the sequentialSearch(int[] array, int key) method for HOWMANY × `i` for `i in [1,5]`.
  5. Add timing statements that will record the time taken in ms for each search and display these results to the Console Window.

The Binary Search.