What is the time complexity of the following code used to search an element in an array? [code] #include<stdio.h> int search_num(int *arr,

Nguyễn Hào

New member
What is the time complexity of the following code used to search an element in an array?
Code:
#include<stdio.h>
int search_num(int *arr, int num, int len)
{
     int i;
     for(i = 0; i < len; i++)
     if(arr[i] == num)
        return i;
     return -1;
}
int main()
{
      int arr[5] ={1,3,3,3,5},num=3,len = 5;
      int indx = search_num(arr,num,len);
      printf("Index of %d is %d",num,indx);
      return 0;
}
A. O(1)
B. O(n)
C. O(n$^{2}$)
D. O(n$^{3}$)
 

Tìm kiếm

Back
Top