Searching



Searching

Searching is a operation in which any particular data is search in the list.The search is said to be successful or unsuccessful depending on whether searching is found or not. there are two  method to search element- linear search and Binary search.



Linear search

This is simple method of searching. In this method,the element is searched in sequence order as one by one.This method can applied on sorted or an unsorted list
10
30
50
60
20
40
80
70
100
90


The array shown in figure consist 10 number.Suppose  the element 60 is searched, so 60 is compared with starting with 0th position and searching process is ends either when 60 is found or the list ends.
The number of comparisons in linear searching is 0(n).


void linear_searching( int a[], int size,int svalue)
{
    int i, flag=0;
    for(i=0;i<size;i++)
    {
        if(a[i]==svalue)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
        printf("seaeching is found");
    else
        printf("searching is not found");

}


Binary search

Binary search method is very fast and efficient. It is the best and fast searching technique which is applied for only sorted data.It is more suitable for large amount of data.
In this searching we compare search element with middle element as n/2 of the list if it is equal to searching  element then searching is successful otherwise list is divided into two parts.One starting from 0 to n/2-1 and second from n/2+1 to n-1. As result  first partition contains element less than mid element and second partition contains greater than mid element.
10
20
30
40
50
60
70
80
90
100


suppose an array consist 10 sorted number and searching number is 70, now binary search method works as follows-
70 is compared with mid element 50,since 70 is greater than 50 and search value is not found so process is continue.
70 is compared  with mid element of second partition, since 70 is less then 80 and search is not found so searching is proceed in the element present between 60 and 80 .
This process is repeated till 70 is found or no further division of sub-array is possible.

void binary1_searching( int a[], int size,int svalue)
{
    int beg,end,mid, flag=0,i;
    beg=0;
    end=size-1;
    mid=end/2;
    for(i=0;i<size;i++)
    {
        if(a[mid]==svalue)
        {
            flag=1;
            break;
        }

    else{
        if(svalue>a[mid])
            beg=mid+1;
        else
            end=mid-1;
    }
    }
    if(flag==1)
        printf("searching is found");
    else
        printf("searching is not found");


}

Thank you..........................................
plz suscribe and share.....................

Comments

Popular posts from this blog

python pattern programming

Decision making statement in python

Looping statement in python