2 - Searching Algorithms
2 - Searching Algorithms
Problem Solving
What is an Algorithm?
Searching Algorithms
Unit 1: Fundamentals of Algorithms
Learning Objectives
● To understand linear search algorithms
● To understand binary search algorithms
Why do we need searching
algorithms?
●Thousands of software applications, including databases
or commercial search engines such as Google, depend on
the ability to quickly search through huge amounts of
data to find a particular item.
●There are many different types of searching algorithms.
Two of them are linear search and binary search.
Types of search algorithm
There are many different types of searching algorithms. Two of
them are serial search and binary search.
Linear Search
● Criteria are set up before the search begins. The search then
starts with the first item and then moves to each item in
turn, until either a match is found or it reaches the end of
the data with no match found
Example
list. Using the pseudocode above, how many items would need
to be examined to find the number 5?
Binary Search
● Binary searches use ordered lists
● An ordered list is one where the sequence of items in
the list is important. An ordered list does not necessarily
contain a sequence of numbers (eg 1, 2, 3, 4) or
characters (eg A, B, C, D). It might also contain, eg a list
of names in alphabetical order, a list of files from
smallest to largest or a list of records from earliest to
most recent.
A binary search will split the database in half, and compare
the midpoint (the middle name) to the name ‘John
Smith’. It will see whether the midpoint comes before or
after ‘Smith’ and will throw away the set of records that
doesn’t contain ‘Smith’ as a surname.
●If the list you are searching is sorted (numerically or alphabetically), you can use a much
more efficient algorithm called a binary search.
●It works by repeatedly dividing in half the portion of the data list that could contain the
required data item.
●This is continued until there is only one item in the list you are examining.
1. found <- False
2. REPEAT
3. Examine the middle data item in the list
4. IF this is the required item THEN
5. found <- True
6. ELSE
7. IF required item > middle item THEN
8. discard the first half of the list and middle item
9. ELSE
10. discard the second half of the list and the middle item
11. ENDIF
12. ENDIF
13. UNTIL found = True OR there are no more items in the list
Linear and Binary:
Advantages and Disadvantages
One of the main advantages of a linear One of the main advantages of a
search is that it is a very simple binary search is that it is much quicker
algorithm, which makes it very easy to than a serial search because the data
write a computer program to carry it out. that needs to be searched halves with
It can also be used on any set of data each step. For example, it is possible to
regardless of type and whether or not it search through 1024 values and find the
is sorted. one you want within 10 steps, every
The biggest problem with a linear search time.
is that it is very slow. For example, when The biggest problem with a binary search
searching through a database of is that you can only use this if the data is
everyone in the UK to find a particular sorted into an order.
name, it might be necessary to search
through 60 million names before you
found the one you wanted.
Linear vs Binary searches
Search type Positives Negatives
Linear •
unsorted list – items don’t have to
be in sequence.
Can add items quickly to a
database and conduct a linear
search without sorting them.
• Very efficient method of searching. • Can only be applied to a sorted list.
Binary
• Can search through massive • If new data is constantly added to a
databases quickly e.g. in a list of database, it will need to be
10 million items, only 24 items resorted before conducting a
would need to be examined. binary search.
Exam questions
Answer
Exam questions
Answer