Loading, please wait...

SEARCHING TECHNIQUES

We often spend time in searching for the desired elements. If the data is stored properly in sorted order, then searching becomes very easy and efficient.

Searching is an operation which finds the place of a given element in the list. The search is said to be successful or unsuccessful depending upon whether the element is being searched is found or not.

 

Some of the standards searching methods are as:


Linear Search:

This is the simplest method for searching. In this method, the element to be found is searched sequentially in the list. This method can be used on a sorted or an unsorted list. In case of a sorted list searching starts from 0th element and continues until the element is found or the element whose value is greater than (Considering list is in ascending order) the value being is searched or reached. As against this, searching in case of an unsorted list starts from the 0thelement and continues until the element is found or the end of list is reached.

 

Example: Assume the element 45 is searched from a sequence of sorted elements 12, 18, 25, 36, 45, 48, 50. The Linear search starts from the first element 12, since the value to be searched is not 12 (value 45), the next element 18 is compared and is also not 45, by this way all the elements before 45 are compared and when the index is 5, the element 45 is compared with the search value and is equal, hence the element is found and the element position is 5.

 

List i Result of comparison

12 18 25 36 45 48 50 1 12 <>45 : false

12 18 25 36 45 48 50 1 18 <>45 : false

12 18 25 36 45 48 50 1 25 <>45 : false

12 18 25 36 45 48 50 1 36 <>45 : false

12 18 25 36 45 48 50 1 45 = 45 : true



Listing: Following program is showing the linear search on given array.

/* 
* C program to input N numbers and store them in an array. 
* Do a linear search for a given key and report success 
* or failure. 
*/ 
#include <stdio.h> 

void main() 
{ 
    int array[10]; 
    int i, num, keynum, found = 0; 

    printf("Enter the value of num \n"); 
    scanf("%d", &num); 
    printf("Enter the elements one by one \n"); 
    for (i = 0; i < num; i++) 
    { 
        scanf("%d", &array[i]); 
    } 
    printf("Input array is \n"); 
    for (i = 0; i < num; i++) 
    { 
        printf("%dn", array[i]); 
    } 
    printf("Enter the element to be searched \n"); 
    scanf("%d", &keynum); 
    /*  Linear search begins */ 
    for (i = 0; i < num ; i++) 
    { 
        if (keynum == array[i] ) 
        { 
            found = 1; 
            break; 
        } 
    } 
    if (found == 1) 
        printf("Element is present in the array\n"); 
    else 
        printf("Element is not present in the array\n"); 
} 
Output:

Enter the value of num 
    5 
    Enter the elements one by one 
    456 
    78 
    90 
    40 
    100 
    Input array is 
    456 
    78 
    90 
    40 
    100 
    Enter the element to be searched 
    70 
    Element is not present in the array 
     
    
    Enter the value of num 
    7 
    Enter the elements one by one 
    45 
    56 
    89 
    56 
    90 
    23 
    10 
    Input array is 
    45 
    56 
    89 
    56 
    90 
    23 
    10 
    Enter the element to be searched 
    45 
    Element is present in the array