Monday, January 10, 2011

Linear Search:

What is Linear Search?
                If u read your reference book u will notice that there is a large explanation on LINEAR SEARCH. But Let Me tell u my answer to this question:-
                Linear search means for e.g. that we have to search an element A in array B which contains N no of elements, So what a linear Search do is That it traverse all N element of array B & while traversing it also check whether each element of array B == A & if it is equal then the location is Prompted else we say nothing is found

Program >>>
Program:
#include<stdio.h>
#include<conio.h>

void main(){
    int arr[50],i,SIZE,ele;

    clrscr();
    printf("Enter Size of Array: ");
    scanf("%d",&SIZE);

    clrscr();
    printf("ARRAY INITIALIZATION...\n\n");

    for(i = 0 ; i < SIZE; i++){
        printf("Enter Element at Position %d of %d: ",i+1,SIZE);
        scanf("%d",&arr[i]);
    }

        printf("\nYour Array now contains: { ");
    for(i = 0 ; i < SIZE; i++){
        printf("%d,",arr[i]);
    }
    printf("}");
    getch();
    clrscr();
    printf("Enter the Element to search in array: ");
    scanf("%d",&ele);

    printf("\nDoing Linear Search...\n");

    for(i = 0 ; i <= SIZE;i++){
        if(arr[i] == ele){
        printf("\nElement found at Location %d",i);
            break;
        }
        if(i == SIZE){
            printf("Element Not Found");
        }
    }
    getch();
}


OUTPUT:




No comments:

Post a Comment