Linear Search

Contents

Problems

Problem : Define the term linear search.

Linear search, also known as sequential search, is a search algorithm which examines each element in the order it is presented to find the specified data.

Problem : You need a picture frame, so you walk down to the local photo store to examine their collection. They have all of their frames lined up against the wall. Apply the linear search algorithm to this problem, and describe how you would find the frame you wanted.

Starting at the first frame, examine each frame along the wall (without skipping any) until you find the frame you want.

Problem : Your local bookstore just received a new shipment of classic literature, and you're extremely excited to find the rare text you've been looking for. You decide that the best way to search for your book is a linear search algorithm. But when you get to the store, the clerk tells you that the books have yet to be sorted. Does this present a problem for your searching algorithm? Why or why not?

No. Data is not required to be sorted for the linear search algorithm.

Problem : What do think may be a disadvantage to linear search? (ie. why should you even bother to continue on with this guide... isn't linear search good enough)?

Linear search is inefficient. It takes O(n) time. We can do better. In fact, depending on how the data is organized, we can do as well as O(1) .

Problem : When the city planners developed your neighborhood, they accidentally numbered the houses wrong. As such, the addresses of the houses on your street are in a random order. How does the postman find your house using a linear search method?

He starts at the first house at the street and looks at the address. If it is the house he is searching for, then he stops there. Otherwise, he goes on to the next house and repeats the process.