8.1.4
Search Algorithms
Search Algorithms
Search Algorithms
Searching is a classic computer science problem. It involves finding a certain value in a set of other values.
![Illustrative background for Search algorithms](https://image-v2.cdn.app.senecalearning.com/2018-08/8fe98794-b7d7-4aa2-bcc1-a72ee94520cd/search-bar-,h_400,q_80,w_640.jpg)
![Illustrative background for Search algorithms ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/8fe98794-b7d7-4aa2-bcc1-a72ee94520cd/search-bar-,h_400,q_80,w_640.jpg)
Search algorithms
Search algorithms
- A search algorithm is a set of instructions for finding a specific item of data within a data set.
- Computer systems can store and process billions of pieces of data so it is vital that computers can find the information they need efficiently.
![Illustrative background for Effective searching](https://image-v2.cdn.app.senecalearning.com/2018-04/3b523221-1812-4e8a-b208-ece6f5fd7674/shutterstock_424331113,h_400,q_80,w_640.jpg)
![Illustrative background for Effective searching ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-04/3b523221-1812-4e8a-b208-ece6f5fd7674/shutterstock_424331113,h_400,q_80,w_640.jpg)
Effective searching
Effective searching
- An effective search is one which will always either find the solution or determine that the target data is not present.
![Illustrative background for Efficient searching](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/7.1.2 Cracking and Alkenes/Volatile,h_400,q_80,w_640.jpg)
![Illustrative background for Efficient searching ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/7.1.2 Cracking and Alkenes/Volatile,h_400,q_80,w_640.jpg)
Efficient searching
Efficient searching
- An efficient search will find the solution quickly regardless of its location within the data set.
![Illustrative background for Examples of search algorithms](https://image-v2.cdn.app.senecalearning.com/2018-06/6ede39b1-6f96-4078-ba64-5d07f569a573/depositphotos_5660618-stock-illustration-binary-code-seamless-pattern,h_400,q_80,w_640.jpg)
![Illustrative background for Examples of search algorithms ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-06/6ede39b1-6f96-4078-ba64-5d07f569a573/depositphotos_5660618-stock-illustration-binary-code-seamless-pattern,h_400,q_80,w_640.jpg)
Examples of search algorithms
Examples of search algorithms
- Two common search algorithms are:
- Linear search.
- Binary search.
Linear Search
Linear Search
Linear search is one of the simplest searching algorithms.
![Illustrative background for The concept](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/1.1.1 Measuring length, volume, etc.../paper-1849364_640-min,h_400,q_80,w_640.jpg)
![Illustrative background for The concept ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/1.1.1 Measuring length, volume, etc.../paper-1849364_640-min,h_400,q_80,w_640.jpg)
The concept
The concept
- If you were looking for a specific piece of paper in a stack of papers, then one way to find it would be to work from the top of the stack to the bottom, checking each paper on the way.
- This is exactly how linear search works.
![Illustrative background for In English](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
![Illustrative background for In English ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
In English
In English
- Check the first item in the dataset:
- If it is what we are looking for, return it.
- Check the second item in the dataset:
- If it is what we are looking for, return it.
- Continue for rest of items.
- If we reach the end of the data set, then the item was not in our data set.
![Illustrative background for Pros and cons of linear search](https://image-v2.cdn.app.senecalearning.com/2018-08/33935250-d722-41a3-a9cf-588036929f55/checklist-2077020_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Pros and cons of linear search ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/33935250-d722-41a3-a9cf-588036929f55/checklist-2077020_1920,h_400,q_80,w_640.jpg)
Pros and cons of linear search
Pros and cons of linear search
- Pros:
- Very easy to implement.
- Cons:
- Slow on a long list.
Binary Search
Binary Search
Binary search is an example of a divide-and-conquer algorithm.
![Illustrative background for The concept](https://image-v2.cdn.app.senecalearning.com/2018-05/e96d7e91-613b-4825-9b91-60c33f326c6c/shutterstock_169575122,h_400,q_80,w_640.jpg)
![Illustrative background for The concept ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-05/e96d7e91-613b-4825-9b91-60c33f326c6c/shutterstock_169575122,h_400,q_80,w_640.jpg)
The concept
The concept
- If you try to find a certain page in a book, it's unlikely that you check every page on the way.
- You are more likely to split the book in two, then see if the page you need is before or after the split, and repeat.
- This is the concept behind the binary search.
![Illustrative background for In English](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
![Illustrative background for In English ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
In English
In English
- Find the middle of the dataset.
- If the middle value is greater than the target then:
- Repeat on the first half of dataset.
- If the middle value is lesser than the target then:
- Repeat on the second half of dataset.
- If the middle value is the target:
- We found it!
- Stop repeating once the size of our dataset is zero.
![Illustrative background for Pros and cons of binary search](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/4.3.5 - Digital Circuits/binary-min,h_400,q_80,w_640.jpg)
![Illustrative background for Pros and cons of binary search ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/4.3.5 - Digital Circuits/binary-min,h_400,q_80,w_640.jpg)
Pros and cons of binary search
Pros and cons of binary search
- Pros:
- Faster than linear search on a large dataset.
- Cons:
- Dataset must be sorted before starting.
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Jump to other topics
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered