Linear Search

Searching AlgorithmAlgorithmsData Structure Algorithms

Linear searching techniques are the simplest technique. In this technique, the items are searched one by one. This procedure is also applicable for unsorted data set. Linear search is also known as sequential search. It is named as linear because its time complexity is of the order of n O(n).

The complexity of Linear Search Technique

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Input and Output

A list of data:
20 4 89 75 10 23 45 69
the search key 10
Item found at location: 4


linearSearch(array, size, key)

Input − An sorted array, size of the array and the search key

Output − location of the key (if found), otherwise wrong location.

   for i := 0 to size -1 do
      if array[i] = key then
         return i
   return invalid location


using namespace std;

int linSearch(int array[], int size, int key) {
   for(int i = 0; i<size; i++) {
      if(array[i] == key) //search key in each places of the array
         return i; //location where key is found for the first time
   return -1; //when the key is not in the list

int main() {
   int n, searchKey, loc;
   cout << "Enter number of items: ";
   cin >> n;
   int arr[n]; //create an array of size n
   cout << "Enter items: " << endl;

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

   cout << "Enter search key to search in the list: ";
   cin >> searchKey;

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


Enter number of items: 8
Enter items:
20 4 89 75 10 23 45 69
Enter search key to search in the list: 10
Item found at location: 4
Published on 04-Jul-2018 17:28:40