UMBC CS 201, Fall 06
 UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help Search Notes:

# search

#### What is it ?

• Given an item X and a collection of items, determine whether or not X is in the collection.
• The collection can be an array or some other type of collection
• The items can be numbers or strings or any kind of data structure
• For simplicity, we'll assume that our collection is held in an array

#### Linear Search

• Check each array element in turn until you find it.
• We say that linear search runs in linear time.
If we have N items in our collection, it could take us as many as N tries to find the item and it would definitely take us N tries to find out that it was not among the items in the collection.
• Would it help if the collection were sorted first ?

#### Binary Search

• Assume the elements are sorted
• See the phone-book demolition in lecture for a demonstration of the algorithm.
• We say that binary search runs in "log N" time.
If there are N items in our sorted collection, it can take as long as log2N to find it or rule out its presence.
• Wow! That's fast !!!

#### Comparing the two

• Linear search requires between 1 and n comparisons/array accesses.
• Binary search requires between 1 and log2(n) comparisons/array accesses.

CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:14:03 EDT