TutorialsICSEClass 10Computer Applications

ICSE Class 10 Arrays: Linear Search, Binary Search, Bubble Sort and Selection Sort

Complete ICSE Class 10 Computer Applications notes on arrays: declaring and initialising arrays, linear and binary search, bubble sort and selection sort, with dry runs, solved Java programs and exam tips.

14 min read
ICSE Class 10 Arrays: Linear Search, Binary Search, Bubble Sort and Selection Sort

Arrays are one of the most important chapters in ICSE Class 10 Computer Applications. Almost every board paper has at least one array program in Section B, usually a search or a sort, plus short output questions in Section A. These notes cover everything you need, with programs you can run in BlueJ.

What is an array?

An array is a collection of values of the same data type stored under one name. Each value is called an element, and its position is its index. Indices start at 0.

int marks[] = new int[5];             // declaration with size 5
int primes[] = {2, 3, 5, 7, 11};      // declaration with initialisation
System.out.println(primes[0]);        // 2  (first element)
System.out.println(primes.length);    // 5  (number of elements)
  • An array of size n has indices 0 to n − 1.
  • Using an index outside this range causes an ArrayIndexOutOfBoundsException.
  • length is a property, not a method, so there are no brackets: arr.length.

Reading values into an array

Scanner sc = new Scanner(System.in);
int a[] = new int[10];
for (int i = 0; i < a.length; i++)
    a[i] = sc.nextInt();

Linear search

Linear search compares the value you are looking for with each element in turn, from the first to the last. It works on any array, sorted or not.

int pos = -1;
for (int i = 0; i < a.length; i++) {
    if (a[i] == key) {
        pos = i;
        break;            // stop at the first match
    }
}
if (pos == -1)
    System.out.println("Not found");
else
    System.out.println("Found at index " + pos);

Binary search

Binary search works only on a sorted array. It compares the value with the middle element and throws away the half that cannot contain it, repeating until the value is found or the range is empty.

int low = 0, high = a.length - 1, pos = -1;
while (low <= high) {
    int mid = (low + high) / 2;
    if (a[mid] == key) { pos = mid; break; }
    else if (key > a[mid]) low = mid + 1;
    else high = mid - 1;
}

Dry run: search for 23 in {4, 8, 15, 16, 23, 42}.

low  high  mid  a[mid]  action
0    5     2    15      23 > 15  → low = 3
3    5     4    23      found at index 4

Linear search vs binary search

  • Array order: linear search works on any array; binary search needs a sorted array.
  • Method: linear search checks the elements one by one; binary search halves the search range each time.
  • Speed: linear search is slow for large arrays; binary search is much faster.

Bubble sort

Bubble sort compares adjacent elements and swaps them if they are in the wrong order. After each pass, the largest remaining element "bubbles" to the end.

int n = a.length;
for (int i = 0; i < n - 1; i++) {            // passes
    for (int j = 0; j < n - 1 - i; j++) {    // comparisons
        if (a[j] > a[j + 1]) {               // ascending order
            int t = a[j];
            a[j] = a[j + 1];
            a[j + 1] = t;
        }
    }
}

Passes for {5, 1, 4, 2}:

Pass 1: 1 4 2 5
Pass 2: 1 2 4 5
Pass 3: 1 2 4 5

Selection sort

Selection sort finds the smallest element in the unsorted part and swaps it into the next position.

for (int i = 0; i < n - 1; i++) {
    int min = i;
    for (int j = i + 1; j < n; j++)
        if (a[j] < a[min])
            min = j;
    int t = a[i];
    a[i] = a[min];
    a[min] = t;
}

Passes for {5, 1, 4, 2}:

Pass 1: 1 5 4 2   (1 swapped to position 0)
Pass 2: 1 2 4 5   (2 swapped to position 1)
Pass 3: 1 2 4 5   (4 is already in place)

Bubble sort vs selection sort

  • Bubble sort compares neighbouring pairs and may swap many times in each pass.
  • Selection sort finds the minimum first and makes at most one swap per pass.
  • Both take n − 1 passes for n elements.

Sorting in descending order

Reverse the comparison: use a[j] < a[j + 1] in bubble sort, or search for the maximum in selection sort. Board questions often ask for descending order, so read the question carefully.

Exam tips

  1. Write the complete program, including the input loop and the output loop. Examiners give marks for both.
  2. For binary search, always mention, or make sure, that the array is sorted.
  3. Common mistake: the bubble sort inner loop must stop at n − 1 − i, otherwise a[j + 1] goes out of bounds.
  4. Practise sorting String arrays too. Compare with compareTo(), not >.

For practice, try the array programs in our ICSE Class 10 Computer Applications Sample Paper 2027 and the 10 most important String programs.