ICSE Class 10 Computer Applications Question 51 of 59

Arrays — Question 53

Back to all questions
53
Question

Question 47

Write a program to search for 66 and 71 in the following array :

304172113184295456717878899931096119912\underset{0}{\boxed{3}}\underset{1}{\boxed{4}}\underset{2}{\boxed{7}}\underset{3}{\boxed{11}}\underset{4}{\boxed{18}}\underset{5}{\boxed{29}}\underset{6}{\boxed{45}}\underset{7}{\boxed{71}}\underset{8}{\boxed{87}}\underset{9}{\boxed{89}}\underset{10}{\boxed{93}}\underset{11}{\boxed{96}}\underset{12}{\boxed{99}}

Make use of binary search technique. Also give the intermediate results while executing this algorithm.

public class KboatBinarySearch
{
    public int binSearch(int arr[], int item)    {
        int l = 0, index = -1;
        int h = arr.length - 1;

        System.out.println("Searching for " + item);
        while (l <= h) {
            int m = (l + h) / 2;
            System.out.println("Searching in sub-array :");
            for(int i = l; i < h; i++)
                System.out.print(arr[i] + " ");
            
            System.out.println();
            
            System.out.println("Comparing " + item + " with "
                                + arr[m] + " at index " + m);
            
            if (arr[m] < item)
                l = m + 1;
            else if (arr[m] > item)
                h = m - 1;
            else {
                index = m;
                break;
            }
        }
        return index;
    }
    
    public static void main(String args[]) {
        KboatBinarySearch obj = new KboatBinarySearch();
        
        int X[] = {3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93, 96, 99};
        
        int index = obj.binSearch(X, 66);
        if (index == -1) {
            System.out.println("66 not found");
        }
        else {
            System.out.println("66 found at index " + index);
        }
        
        index = obj.binSearch(X, 71);
        if (index == -1) {
            System.out.println("71 not found");
        }
        else {
            System.out.println("71 found at index " + index);
        }
        
    }
}
Output
BlueJ output of KboatBinarySearch.java
Answer

Source: This question is from Arrays, Computer Applications — Class 10, ICSE Board.

Key Concepts Covered

This question tests your understanding of the following concepts from the chapter Arrays: Question, Program, Search, Array, Underset, Boxed. These are fundamental topics in Computer Applications that students are expected to master as part of the ICSE Class 10 curriculum.

A thorough understanding of these concepts will help you answer similar questions confidently in your ICSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Applications papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.

How to Approach This Question

Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.

Key Points to Remember

  • Write programs with proper indentation and comments.
  • Trace through your code with sample inputs to verify correctness.
  • Explain the logic behind each step of your solution.
  • Familiarise yourself with common library functions and methods.

Practice more questions from Arrays — Computer Applications, Class 10 ICSE