Linear Search in C Language

Source Code

#include <stdio.h>

int main()
{
    int n = 9;
    int arr[] = {10, 8, 1, 21, 7, 32, 5, 11, 0};
    int key = 56;

    // linear search
    int i = 0;
    for (; i < n; i++)
    {
        if (arr[i] == key)
        {
            printf("%d is found in the array!\n", key);
            break;
        }
    }
    if (i == n)
        printf("%d is not found in the array!\n", key);
    return 0;
}

Time Complexity

O(n) where 'n' is the size of the array

Other videos in this series

Back To Home