Linear Search

#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);

int main(){
    int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
    int key = 0;
    
    print_array(a, current_size);
    key = 10;
    //key = 15;
    
    for(i=0; i<current_size ; i++){
        if(a[i] == key){
            printf("Found\n");
        }
    }    
    
    return 0;
}

void print_array(int *array, int current_size){
    int i;
    if(current_size==0){
        printf("Array is Empty\n");
    }else{
        printf("Array Elements are: ");
        for(i=0; i<current_size; i++){
            printf("%d ", array[i]);
        }
        printf("\n");
    }
}

Break Once Element Found

Test Element Not Found

Search Using Flag

Search Using Index

Search Using Function

Return Truth Value from Search Function

Recursive Linear Search (Alternative Approach)

Last Element Index for Multiple Occurrences of Element

Last updated