LINEAR SEARCH

 LINEAR SEARCH:-


ALGORITHM:-    

 STEP 1.  Begin

STEP 2. set a[5] {10,20,30,40,50}

STEP3. set i=0

STEP4. input searching item

STEP5. repeat step 6 & 7 while i<5

STEP6. if a [i]=item then print item found and location=i & exit

STEP 7. i=i+1

STEP8. if i>5  then print item not found

STEP 9. EXIT



CODE :-      


  #include<stdio.h>

#include<conio.h>

void main() {

    int a[5], i = 0, j, item;

    clrscr();

    printf("Enter array elements:\n");

    for (j = 0; j < 5; j++) {

        scanf("%d", &a[j]);

    }

  printf("Enter item to search:\n");

    scanf("%d", &item);

     while (i < 5) {

        if (a[i] == item) {

            printf("Item found at location: %d\n", i);

            break; // Added to exit the loop once the item is found

        }

        i++;

    }

  if (i == 5) {

        printf("Item not found\n");

    }

getch();

}


TIME COMPLEXITY:-     O(|n)


Comments

Popular posts from this blog

AI Lab file 2024

AI Prolog PDF