Linear Search in Data Structure

Linear Search also called sequential search in data structure which works by scanning one by one element in the list until the searched element is found.

For Example:

If we want to search 30 in the following Array:

Step-1 : Firstly, 30 is compared to first element that is 10

Step-2: When not matched then 30 is compared to second element that is 20

Step-3 When not matched then 30 is compared to third element that is 30

Step -4: Finally element is found

Advantage of Linear Search over Binary Search

  • Applicable for Small data
  • Can be used for unsorted array
  • Easy to understand
  • No special data structure required

Disadvantage

  • Time Consuming
  • Not applicable for Large data

Applications of linear Search in real life

  • Phonebook
  • Books like novel
  • movie

Linear Search Program in C++

Program of Searching in C++ is as follows:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main() {
	int list[100];
	int i,n,pos,x;
	char ch;
	do {
		cout<<"\n Enter the size of the list(no. of elements): ";
		cin>>n;
		cout<<" \n Enter the elements: ";
		for(i=0;i<n;i++) {
			cin>>list[i];
			cout<<" ";
		}
		pos=-1;
		cout<<"\n Enter the element to be searched: ";
		cin>>x;
		//searching the list sequentially from here.
		for(i=0;i<n;i++) {
			if(list[i]==x)
			pos=i;
		}
		if(pos>-1) {
			cout<<"\n Element Found";
			cout<<"\n The Position of element is: "<<pos<<" \n\n";
		}
		else
			cout<<"\n Sorry!!!Search is Unsuccessful!!!\n ";
		cout<<"\n Want to try again(y/n): ";
		cin>>ch;
	} while(ch=='y'||ch=='Y');
	return 0;
}

Output of the program to find the number in the list using c++ :

Linear Search Program in C++ Using Array

Group of element in any form of data structure is an array.

Program of searching in C++ using array as follows:

#include<iostream>
#include<conio.h>

using namespace std;

int main(){
	int i,data,found=-1,a[10];
	cout<<"enter 10 numbers in array\n";
	for(int i=0;i<10;i++)
			cin>>a[i];
	cout<<"\nenter number to be search\n";
	cin>>data;
	for(i=0;i<10;i++) {
		if(a[i]==data) {
			found=i;
			break;
		}
	}
	if (found > -1 )
	   cout<<"data is present at position\n"<<found;
    else
	   cout<<"data is not present";
	getch();
}

Output of the program to find the number in the array using c++ :

Linear Search

Linear Search Program in C using Recursion

The program in which one function works repeatedly to do similar work until it complete is called as recursion.

Program of linear search in C as follows:

#include <stdio.h>

int RecursiveLS(int arr[], int value, int index, int n)
{
    int pos = 0;

    if(index >= n)

        return 0;
    else if (arr[index] == value)
 {
        pos = index + 1;
        return pos;
    }
 
    else
        return RecursiveLS(arr, value, index+1, n);
    return pos;
}
 
int main()
{
    int n, value, pos, m = 0, arr[100];
    printf("Enter the total elements in the array  ");
    scanf("%d", &n);
    printf("Enter the array elements\n");
    for (int i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }
    printf("Enter the element to search  ");
    scanf("%d", &value);
 
   pos =  RecursiveLS(arr, value, 0, n);
    if (pos != 0)
{
        printf("Element found at pos %d ", pos);
    }
    else
{
        printf("Element not found");
    }
    return 0;
}

Output the program in C using recursion which is as follows:

Linear Search Program in Python

In this program the number is search using python language is as follows:

def linear_search(alist, key):
    """Return index of key in alist. Return -1 if key not present."""
    for i in range(len(alist)):
        if alist[i] == key:
            return i
    return -1
 
 
alist = input('Enter the list of numbers: ')
alist = alist.split()
alist = [int(x) for x in alist]
key = int(input('The number to search for: '))
 
index = linear_search(alist, key)
if index < 0:
    print('{} was not found.'.format(key))
else:
    print('{} was found at index {}.'.format(key, index))

Output of the program to search a number using python is as follows:

Linear Search

Related Link:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments