(DOCX, Unknown)

advertisement
Algorithms: Linear and
Binary searching
Notes
What is an array?
An array is a form of list, it is a commonly used data structure, and it can hold several elements and can
be one, two or three dimensions.
What is an algorithm?
It is the steps that need to be carried out to get from the start to the end of a task or problem, like a
recipe. They can be used before coding in any language and there are lots that have been created already
and are often found in books of algorithms.
Serial searching:




It is also known as a linear search or a sequential search.
The ‘old’ way of searching, it is the most simple.
It starts at the first item and asks if it what it is looking for, if it is then it stops, if not it moves
onto the next item.
Example of a serial searching algorithm:
1. Set up the search criteria.
2. Examine first item in the data set.
3. If there is a match, end the procedure and return the result with 'match found'.
4. If no match is found repeat with the next item.
5. If the last item is reached and no match is found return 'match not found'.
Advantages




It is very easy to code, it simply can loop through a list checking each item.
It is good for small or medium length lists.
It does not need an ordered list unlike other algorithms, the outcome does not have any
difference depending on the order.
It is not affected by a change in the list, if something is added or deleted.
Disadvantages


Can be slow over long lists, if the item is near the end for example or if no match was found.
Other methods have been developed because of the slow speed, meaning it is now scarcely
used.
Binary searching:



A fast method of searching.
Relies on having an ordered list.
For example when you use the dictionary, you open it half way then see which way you need to
go, then go half way into that and so on.

Example of an algorithm:
1. Set the highest location in the list to be searched as N
2. Set the lowest location in the list to be searched as L
3. Examine the item at location (N - L) /2 (i.e. halfway)
4. Is it a match? If Yes End search.
5. No
6. Is item less than criteria?
7. If Yes, Set lower limit L to item + 1 (Force the next search to use the upper half)
8. If No, Set upper limit N to item - 1 (Force the next search to use the lower half)
9. Is lower limit = upper limit, if yes end search (no match found)
10. Repeat from step 3 with the new upper and lower bounds.
Advantages



Faster over large data sets.
Good for static lists (no added or deleted data).
If the list is ordered, it is very reliable.
Disadvantages




Doesn’t work on unordered data.
Not worth using over small lists.
Not useful if the list changes too much.
If the list orders the items so that the most searched are at the beginning, it would be easier to
use a linear search.
Tasks
Perform a linear and binary search for Peru in the following data:
Argentina, Bolivia, Brazil, Chile, Colombia, Ecuador, French Guiana, Guyana, Paraguay, Peru, Suriname,
Uruguay, Venezuela.
Linear


Took 10 processes.
Uruguay took 12.
Binary




Process 1: half = French Guiana, P = further down alphabet.
Process 2: upper half = Peru.
2 processes.
Uruguay took 3.
Describe and explain the circumstances in which you might choose to use a
linear search over a binary search:
When searching through a list of names that is short and changing because then you don’t have to order
the names but it is still fairly quick as there aren’t many. Also when you are coding a search but you need
the coding to be quick and simple and the actual search does not have to be done quickly.
Download