Searching Algorithms
Explore the fundamental searching algorithms used in computer science, including sequential and binary search. Understand how to implement these algorithms in Java to efficiently locate values in arrays. This lesson helps you grasp core concepts and coding techniques crucial for acing your AP Computer Science exam.
We'll cover the following...
Overview
One of the main advantages a computer offers is storing large amounts of data. Computers can find things quicker than humans. This capability is called searching. There are two main types of searching techniques:
- Sequential search
- Binary search
Sequential search
Going through all items, one by one, until we find the desired item is called a sequential search. It is also known as a linear search.
For example, if we have the following array, and we need to find , we’ll check all the elements one by one.
It’s very easy to code a sequential search in Java. Let’s begin.
Look at line 3. We create the header of the search() function. It takes the array and a value that is to be searched. As a result, it will return the index of the value. If the value isn’t found, it returns ...