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?
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.
Related Questions
A Student executes the given program segment and it results in 1.0, irrespective of the value of n. State the type of the error, write the correct statement:
void solve(int n) { double power=Math.pow(n, 2/3); System.out.println(power); }Consider the following program segment and answer the questions given:
for(int k=1;k<=5;k++) System.out.println(k); System.out.println(k);(a) Will the program segment get executed successfully?
(b) If not, state the type of error?
(c) How do you correct the program if it has any error?Evaluate the expression:
3 * 6 % 5 * 4 / 2 * 7 % 5
Consider the following program which calculates the Norm of a matrix. Norm is square root of sum of squares of all elements.
Fill in the blanks (a) and (b) with appropriate java statements:
double norm() { int x[][]={{1,5,6},{4,2,9},{6,1,3}}; int r, c, sum=0; for(r=0;r<3;r++) { for(c=0;c<3;c++) sum=sum+_____(a)_____; } ____(b)________; }