0% found this document useful (0 votes)
4 views12 pages

Searching

The document outlines searching algorithms, specifically binary and linear search methods, used to find information in large datasets. It details the steps involved in both algorithms, including examples for clarity, and emphasizes the efficiency of binary search for ordered lists compared to the simpler but less efficient linear search for unordered lists. Additionally, it includes tasks for applying both search algorithms and questions for further understanding of their application.

Uploaded by

waniasaleem12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Searching

The document outlines searching algorithms, specifically binary and linear search methods, used to find information in large datasets. It details the steps involved in both algorithms, including examples for clarity, and emphasizes the efficiency of binary search for ordered lists compared to the simpler but less efficient linear search for unordered lists. Additionally, it includes tasks for applying both search algorithms and questions for further understanding of their application.

Uploaded by

waniasaleem12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Algorithm

s
Searching Algorithms

Learning Objective: To be able to demonstrate an


understanding of the algorithms that are used to search for
information in large amounts of data.

Success Criteria:
1. I can list the steps in a binary search algorithm.
2. I can use the binary search algorithm to search for
items in an ordered list.
3. I can list the steps in a linear search algorithm.
4. I can use the linear search algorithm to search for items
in an unordered list.
Search Algorithms
When searching for information within large amounts of data, a
computer uses algorithms in order to do this. There are 2 simple
methods that you need to be aware of:

• Binary Search Algorithm (ordered lists)


• Linear Search Algorithm (unordered lists)
Binary Search
A binary search looks for items in an ordered list.

Steps in a binary search are: To find the middle item


in a list of n items do
1. Put the list into order (if it is not already) (n+1) ÷2 and round up
if necessary.
2. Find the middle item in the ordered list.
3. If this is the item you’re looking for, then stop the search – you’ve
found it.
4. If not, compare the item you’re looking for to the middle item. If
it comes before the middle item, get rid of the second half of the
list. If it comes after the middle item, get rid of the first half of the
list.
5. You’ll be left with a list that is half the size of the original list.
Repeat steps 1-3 on this smaller list to get an even smaller one.
Keep going until you find the item you’re looking for.
Binary Search Example:
We are going to use the binary search algorithm to find the number 99
in the following list:

7 21 52 59 68 92 94 99 133

There are 9 items in the list so the middle item is the (9+1) ÷ 2 = 5th item.

The 5th item is 68 and 68 <99 so get rid of the first half of the list to leave:
92 94 99 133

There are 4 items left so the middle item is the (4+1) ÷ 2 = 2.5 = 3rd item.

The 3rd item is 99. You’ve found the item you’re looking for so the search is
complete.
Task: Binary Search
Use the binary search algorithm to:

1. Find the number 23 from the list.

2 15 20 23 30 34 45 50 51
2. Find the number 145 from the list.

25 45 60 85 100 115 135 145 160


Linear Search
A linear search can be used on an Unordered list.
A linear search checks each item of the list in turn to see if it’s the
correct one, it stops when it either finds the item it’s looking for or has
checked every item.

A linear search is much simpler than a binary search but less efficient.
A linear search can be used on any type of list, it doesn’t have to be
ordered. Due to it being inefficient, it is often only used on small lists.
Linear Search: The Steps
1. Look at the first item in the unordered list.
2. If this is the item you’re looking for, then stop the search – you’ve
found it!
3. If not, then look at the next item in the list.
4. Repeat steps 2 and 3 until you find the item that you’re looking
for or you’ve checked every item.

Lets look at an example….


Linear Search: Example
Use the linear search to find the number 99 from the previous list…

7 21 52 59 68 92 94 99 133

Check the first item: 7 ≠ 99

Look at the next item: 21 ≠ 99

Look at the next item: 52 ≠ 99

Look at the next item: 59 ≠ 99



Look at the next item: 68 ≠ 99 means
Look at the next item: 92 ≠ 99 NOT
Look at the next item: 94 ≠ 99

Look at the next item: 99 = 99

You’ve found the item you’re looking for so the search is complete.
Task: Linear Search
Use the linear search algorithm to:

1. Find the number 23 from the list.

2 15 20 23 30 34 45 50 51
2. Find the number 145 from the list.

25 45 60 85 100 115 135 145 160


Question: Linear Search
Describe an algorithm expressed in pseudocode to ask a user for a
search item and carry out a linear search on data stored in an array to
find that item.
Question: Binary Search
Describe the stages in applying a binary search to the following list to
find the number 17.

3, 5, 9, 14, 17, 21, 27, 31, 35, 37, 39, 40, 42.

You might also like