KnowledgeBoat Logo
|

Computer Applications

Consider the array:

int a[]={12, 35, 40, 22, 56, 9, 70};

(a) In the above given array, using linear search, how many iterations are required to check for the existence of the value 56?

(b) If the array is arranged in descending order, how many iterations are required to check for the existence of 56 using linear search?

Java Arrays

2 Likes

Answer

(a) 5

(b) 2

Explanation

(a) In linear search, we check each element of the array one by one starting from the first element. We compare each element with the value we want to find, which is 56 in this case. The search will check 12, then 35, then 40, then 22, and finally 56. Since 56 is the fifth element, it takes 5 iterations to find it.

(b) If the array is sorted in descending order, it will look like this: {70, 56, 40, 35, 22, 12, 9}. Now, when we do linear search, we again start from the first element and check each element one by one. The search will check 70 first and then 56, which is the second element. So, it takes only 2 iterations to find 56 in the sorted array.

Answered By

3 Likes


Related Questions