SearchingLinear search

Searching for data can be very difficult. Searching algorithms, such as linear search (also known as serial search) and binary search, make the process of searching for data much easier.

Part ofComputer ScienceAlgorithms

Linear search

Data is often stored in lists. Computers are highly efficient at searching through lists. The simplest type of search is a linear search, also known as a serial search. In a linear search, you first specify the item to look for. The search starts at the first item in the list and checks each one in turn until it either finds a match or there is no more data in the list. A linear search is easy to program but not very efficient at finding data.

Example

Imagine that you have a of sales made to customers. You need to deliver the goods that customer number 150 has bought, so need to find their address in the database.

  1. The criterion is set first: Find the address for customer 150.
  2. A linear search will begin at customer 1 and will search through each customer in turn until it reaches customer 150.
  3. It will then output the address for this customer.
  4. If it does not find customer 150, a message will be output to say that the customer is not found.

In this would look like:

OUTPUT "Which customer number would you like to look up?"
INPUT user inputs customer number
STORE the user's input in the customer_number variable
counter = 0 found = False
WHILE found = False and counter ≤ number_of_records: IF counter = customer_number THEN OUTPUT customer address found = True ELSE add 1 to counter ENDIF
ENDWHILE
IF found = False THEN OUTPUT "Customer not found"
ENDIF

As a flow diagram (also known as a flowchart) this would look like:

A flowchart showing a serial search.
Figure caption,
A flow diagram representing a linear search