Wednesday 3 February 2016

Linear Search Alogritham | Flow Chart | Program | Time complexity

The linear search is a sequential search, which uses a loop to step through an array, starting with the first element. It compares each element with the value being searched for, and stops when either the value is found or the end of the array is encountered. If the value being searched is not in the array, the algorithm will unsuccessfully search to the end of the array.

                 Since the array elements are stored in linear order searching the element in the linear order make it easy and efficient. The search may be successful or unsuccessfully. That is, if the required element is found them the search is successful other wise it is unsuccessfully.

Advantages and disadvantages of the linear search

Advantages:

  •     The linear search is simple - It is very easy to understand and implement;
  •     It does not require the data in the array to be stored in any particular order.

Disadvantages:

  •     Not efficient 
  •     In average case, n/2 comparisons will be made
  •     In worst case,  n comparisons will be made
  •     The time complexity of linear search is O(n)

Flow Char for Linear Search 




Linear Search Alogritham

Here an algorithm of a function that performs the linear search:

1.Set found to false
2.Set position to -1
3.Set index to 0
While found is false and index < number of elements
   If list[index] is equal to search value
      found = true
      position = index
   End if
   Add 1 to index
End while
Return position

 1. Write a simple code for linear search in c programming language
2. Wap a c program to search an element in an array using linear search

int main(){

    int a[10],i,n,m,c=0;
    clrscr();

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements of the array: ");
    for(i=0;i<=n-1;i++){
         scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);
    for(i=0;i<=n-1;i++){
         if(a[i]==m){
             c=1;
             break;
         }
    }
    if(c==0)
         printf("The number is not in the list");
    else
     printf("The number is found at index %d",i);
    getche();
    return 0;

}

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top