Sequential Search in Java: Algorithm, Implementation & Analysis

Lesson Transcript
Instructor: Martin Gibbs

Martin has 20 years experience in Information Systems and Information Technology, has a PhD in Information Technology Management, and a master's degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.

In this lesson, you will learn how to write Java code to perform a sequential search. We will also look at the limitations and performance implications of sequential searches. Updated: 09/22/2022

Sequential Search

Imagine that you have an Excel sheet with 75 rows, and you need to find the value $7.33 within the text. That shouldn't be too hard, right? All you need to do is start scanning down your rows and columns until you find the value. If you find it, you move on. If you don't, you announce that the value is not there.

This type of search is called a sequential search (also called a linear search), in which the search starts at the first record and moves through each record until a match is made, or isn't made.

If your worksheet had 500 rows, however, you might want to make use of a search function instead of manually scanning rows and columns. In Java, we can write code to perform a sequential search of our data.

Sequential Search Algorithm

As stated previously, a sequential search cycles through every element in a data set until a match is found. It's important to re-iterate that every item is checked as the system evaluates the condition.

For a sample sequential search, we're going to use the following assumptions:

  • The list or array is Q
  • We are looking for value z
  • Our counter is i

The steps of an example search algorithm are listed here, using the previously outlined assumptions:

  1. Set starting position to first element (i = 0)
  2. If i is greater than the number of elements, go to Step 7
  3. If Q[i] = z, then go to Step 6
  4. Update i to i + 1
  5. Return to Step 2
  6. Element z has been found at i: Exit (Step 8)
  7. Element Not In List
  8. Exit Routine

Now that we've walked through how the algorithm works, we can create a Java program that performs a sequential or linear search.

Implementing the Search in Java

First, let's take a look at an array of data that we will search that's appearing here. Note that sequential searches don't require that the data be sorted.

 public class Searches {
   public static void main(String[] args) {
   //create an array
   double[] payments = {3.35, 12.07, 122.76, 7.33, 6.04, 681.78, -0.05, 45.25, 107.33, 6.25, 3.45, 3.45, 0.52};

Next, we're going to need a method that performs the sequential search. This method accepts two parameters:

  1. The name of the array
  2. The key value we are searching for
  public static double sequentialSearch(double[] arr, double key) {
   int arraySize = arr.length;
   for(int i = 0; i < arraySize; i++) {
    if(arr[i] == key) {
     return i;
   return -1;

If the value is not found, the function returns -1. You could return 500 if the array were made up of integers, or ''Fred'' if the array were made of up strings. What is certain is that the code would have to search through every element if the desired value is not in the list.

We're now going to add the searchKey variable and the method call within the main function (right after the array declaration), like you can see appearing here:

   //what are we looking for?
   double searchKey = 7.33;
   System.out.println("Search Key found at: " + sequentialSearch(payments, searchKey));

When the program runs, the output displays:

Search Key found at: 3.0

It's returned as a double since our key value was 7.33.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back

Resources created by teachers for teachers

Over 30,000 video lessons & teaching resources‐all in one place.
Video lessons
Quizzes & Worksheets
Classroom Integration
Lesson Plans

I would definitely recommend to my colleagues. It’s like a teacher waved a magic wand and did the work for me. I feel like it’s a lifeline.

Jennifer B.
Jennifer B.
Create an account to start this course today
Used by over 30 million students worldwide
Create an account