# Linear Search/ Sequential Search

### Linear/Sequential Search

In computer science, linear search, also known as sequential search, is a method for <mark style="color:red;">locating</mark> a specific value in a list that involves verifying each of the list's members one by one until the desired one is discovered.&#x20;

The <mark style="color:red;">simplest</mark> search algorithm is linear search. It's a type of <mark style="color:red;">brute-force</mark> search with a twist. Its worst-case cost is proportional to the list's amount of items.

### The complexity of Linear Search Technique

* **Time Complexity:** <mark style="color:red;">O(n)</mark>
* **Space Complexity:** <mark style="color:red;">O(1)</mark>

### Linear/ Sequential Search - Algorithm

<div align="center"><img src="https://3717782766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F60gGuq3OlHvCAPS1ajFy%2Fuploads%2FwbJYutUJuCABLEoLkcrK%2Fimage.png?alt=media&#x26;token=bbff91ae-c67c-41ad-b67c-8db2a26ae477" alt=""></div>

### Linear/ Sequential Search - Example

Search for <mark style="color:red;">**1**</mark> in the given array  ![](https://3717782766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F60gGuq3OlHvCAPS1ajFy%2Fuploads%2FiCLCeQL20HDZP2uDWHTm%2Fimage.png?alt=media\&token=0abeee8d-0d6c-41ba-a8ab-3406b96abbd9)

Comparing the value of <mark style="color:red;">ith</mark> index with element to be search one by one until we get searched element or end of the array.

![Element found at ith index, i=3](https://3717782766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F60gGuq3OlHvCAPS1ajFy%2Fuploads%2FbvB0KwA4LOt7gWIlKgoS%2Fimage.png?alt=media\&token=8b933b64-4d1f-440f-8afc-3f35372009a5)

### Example

```
#include<iostream>
using namespace std;

int linSearch(int a[], int size, int key) {
   for(int i = 0; i<size; i++) {
      if(a[i] == key) 
         return i; 
   }
   return -1; 
}

int main() {
   int n, skey, loc;
   cout << "Enter number of items: ";
   cin >> n;
   int arr[n]; 
   cout << "Enter items: " << endl;

   for(int i = 0; i< n; i++) {
      cin >> arr[i];
   }

   cout << "Enter which number to search in the list: ";
   cin >> skey;

   if((loc = linSearch(arr, n, skey)) >= 0)
      cout << "Item found at location: " << loc << endl;
   else
      cout << "Item is not found in the list." << endl;
}
```

### Output

```
Enter number of items: 5
Enter items:
2 9 3 1 8
Enter which number/ to search in the list: 1
Item found at location: 3
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fsm.gitbook.io/algo/linear-search-sequential-search.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
