0% found this document useful (0 votes)
47 views

2 - Searching Algorithms

The document discusses linear and binary search algorithms. It explains that linear search examines each item in a list sequentially until the target item is found, while binary search divides the search space in half at each step by comparing the target to the middle item. Binary search is faster but can only be used on sorted lists, while linear search works on any list but is slower. The document provides pseudocode examples and discusses the advantages and disadvantages of each type of search algorithm.

Uploaded by

Prakash Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

2 - Searching Algorithms

The document discusses linear and binary search algorithms. It explains that linear search examines each item in a list sequentially until the target item is found, while binary search divides the search space in half at each step by comparing the target to the middle item. Binary search is faster but can only be used on sorted lists, while linear search works on any list but is slower. The document provides pseudocode examples and discusses the advantages and disadvantages of each type of search algorithm.

Uploaded by

Prakash Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

GCSE COMPUTER SCIENCE

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

● Imagine that you have a database 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.
CRITERIA: Find the address for client 150
Linear search
1. Found <- False
2. Start at the first name
●When data is unsorted, the 3. REPEAT
only sensible option when 4. Examine the current name in the list
5. IF it’s the one you are looking for THEN
searching for a particular 6. found <- True
item is to start at the 7. ENDIF
beginning and look at every 8. UNTIL found = True OR reach end of list
item until you find the one 9. IF found = True THEN
10. OUTPUT name
you want. 11.ELSE
●You could be lucky and find 12. OUTPUT “Not found”
13.ENDIF
the item quite quickly if it’s
near the beginning of the list, Question:
Look at the following list of integers:
or you could be unlucky and
find it right at the end of the 10 22 3 6 7 1 5 15 11 16

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.

It will keep dividing the records in that way until it reaches


two records left, one of which is ‘John Smith’. It will then
throw away the other record and output John Smith’s
details. Binary search effectively divides the data in half
and throws away, or ‘bins’ the half that does not contain
the search term.
Binary search

●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

• Works well when searching small • Inefficient method of searching


numbers of items. large databases.
• Can search through items in an

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

Your overall score [max 7 marks]:


WWW?:
EBI?:

You might also like