My Blog List

Tuesday, December 3, 2019

linear search using cpp.

Linear search using cpp.

In this tutorial we learn how to search element in linear searching technology.Linear search is a technique searching element in linear order.This technique checks each element of the list until a match is found.

Complexity:

worst complexity: O(n)
Average complexity: O(n)
Space complexity: O(1)

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
    int arr[10],i,num,n,c=0,pos;
    clrscr();
    cout<<"\nEnter the size of array:";
    cin>>n;
    cout<<"\nEnter the element:";
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"\nEnter searchable number:";
    cin>>num;
    for(i=0;i<n;i++)
    {
        if(arr[i]==num)
        {
            c=1;
            pos=i+1;
            break;
        }

    }
    if(c==0)
    {
        cout<<"\nNumber is not found;";
    }
    else
    {
        cout<<num<<"\nFound at position"<<pos;
    }
    getch();
}


OUTPUT:



 

No comments:

Post a Comment

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++. On the basis of Inheritance concept the another type of inheritance is the Multiple I...