Computer Science, asked by ridsharma0405, 3 months ago

Write a program to perform linear search on the given list:

[10,52,45,6,98,76,5,33]

Answers

Answered by chintamanbhamre000
0

Answer:

Linear Search

Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].

Examples :

Input : arr[] = {10, 20, 80, 30, 60, 50,

110, 100, 130, 170}

x = 110;

Output : 6

Element x is present at index 6

Input : arr[] = {10, 20, 80, 30, 60, 50,

110, 100, 130, 170}

x = 175;

Output : -1

Element x is not present in arr[].

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

A simple approach is to do a linear search, i.e

Start from the leftmost element of arr[] and one by one compare x with each element of arr[]

If x matches with an element, return the index.

If x doesn’t match with any of elements, return -1.

Similar questions