**Implementation of Linear Search algorithms in C language with Online compiler link:***In Linear search algorithm, we tried to find out the search element sequentially from the start to end of the Linear array ( whether sorted or unsorted) by comparing each each element. If at any specific position search element is found then we can say that search is successful other wise search is not successful or element not found in the linear array. In best case the time complexity of the linear search algorithm is O(1) and in worst case the time complexity is O(n), where n is the size of the linear array.*

Below is the step which we follow to implement Linear Search are:

1. First declare all the header fill you want in your program ( according to language in which you want to implement the algorithm).

2. Declare an array of any size ( i.e arr[15]) with different variable you need (like search_element, i and j for loop variable).

3. Enter element into the array arr[15].

4. Enter the search element search_element

5. Start comparing array element with the search element from the start of the array.

6. If at any array index if the array element and search_element is equal, then we say the search is successful.

7. If no element of the array is equal to search_element, then we say search is not successful.

8. End of the program.

#include<stdio.h>

int main()

{

int arr[10], i, num, n, pos;

scanf("%d",&n);

for(i=0; i<n; i++)

{

scanf("%d",&arr[i]);

}

//printf("/n Enter the number to be search : ");

scanf("%d",&num);

int flag=0;

for(i=0; i<n; i++)

{

if(arr[i]==num)

{

flag=1;

pos=i+1;

printf("\n Element is found at position %d ",pos);

break;

}

}

if(flag==0)

printf("\n Element not found");

return 0;

}

Below is the step which we follow to implement Linear Search are:

1. First declare all the header fill you want in your program ( according to language in which you want to implement the algorithm).

2. Declare an array of any size ( i.e arr[15]) with different variable you need (like search_element, i and j for loop variable).

3. Enter element into the array arr[15].

4. Enter the search element search_element

5. Start comparing array element with the search element from the start of the array.

6. If at any array index if the array element and search_element is equal, then we say the search is successful.

7. If no element of the array is equal to search_element, then we say search is not successful.

8. End of the program.

**Implementation of Linear Search Algorithm in C:**#include<stdio.h>

int main()

{

int arr[10], i, num, n, pos;

scanf("%d",&n);

for(i=0; i<n; i++)

{

scanf("%d",&arr[i]);

}

//printf("/n Enter the number to be search : ");

scanf("%d",&num);

int flag=0;

for(i=0; i<n; i++)

{

if(arr[i]==num)

{

flag=1;

pos=i+1;

printf("\n Element is found at position %d ",pos);

break;

}

}

if(flag==0)

printf("\n Element not found");

return 0;

}

**Online Compiler Link for the above code: Run the above program online**

**Thanks**

**Mukesh Rajput**
## No comments:

## Post a Comment

Thanks

Mukesh Rajput