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 databaseA data store designed in an organised way, making it easier to search for the information you need. 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.
- The criterion is set first: Find the address for customer 150.
- A linear search will begin at customer 1 and will search through each customer in turn until it reaches customer 150.
- It will then output the address for this customer.
- If it does not find customer 150, a message will be output to say that the customer is not found.
In pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding. 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"
ENDIFAs a flow diagram (also known as a flowchart) this would look like: