Skip to content
Computer ScienceISC Class 11 · Arrays and Strings

Java Arrays: Searching, Sorting and 2D Arrays (ISC Class 11 Computer Science)

Updated 17 October 2026 · Fact-checked

An array is a fixed-size collection of elements of the same type stored under one name and accessed by an index from 0 to length - 1. Operations include finding the maximum and minimum, linear search (scan every element), binary search (halve a sorted array) and sorting by bubble, selection or insertion sort. A two-dimensional array is an array of arrays accessed by a row and a column index.

Each step checks the orange middle cell, crosses out the half that cannot hold the key, and the green cell shows the key found.

Illustration drawn for QuestionBuddy.

Understand Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays

Declaration and creation.

int a[] = new int[5];          // five elements, indices 0 to 4
int b[] = {12, 45, 7, 33, 21}; // initialised

Default values: 0 for numeric types, false for boolean, '\u0000' for char, null for objects. a.length gives the size. An index outside 0 to length - 1 throws ArrayIndexOutOfBoundsException at run time.

Maximum and minimum. Assume the first element is both, then compare with each element.

Linear search. Compare the key with each element in turn; works on any array; up to n comparisons.
Binary search. Works only on a sorted array. Compare the key with the middle element; discard half; repeat. About log2(n) comparisons.

int lo = 0, hi = n - 1;
while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (a[mid] == key) return mid;
    else if (a[mid] < key) lo = mid + 1;
    else hi = mid - 1;
}

Sorting (ascending).

  • *Bubble sort:* compare adjacent elements and swap if out of order; after each pass the largest element reaches the end; n - 1 passes, n(n-1)/2 comparisons in all.
  • *Selection sort:* in pass i find the smallest of a[i..n-1] and swap it into position i.
  • *Insertion sort:* take a[i] as key, shift larger elements right, insert the key in place.

Two-dimensional arrays. int m[][] = new int[3][3]; m[i][j] is row i, column j. Row sums use a nested loop; the main diagonal has i == j and the other diagonal i + j == n - 1.

Passing arrays. An array passed to a method is a reference, so changes made inside the method affect the original array.

Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays: key facts

Array facts
  • Indices 0 to length - 1
  • length = number of elements
  • Out of range gives ArrayIndexOutOfBoundsException
Fixed size.
Bubble sort count
Passes = n - 1 | Comparisons = (n - 1) + (n - 2) + ... + 1 = n(n - 1)/2
Without a flag.
Binary search
  • mid = (low + high)/2
  • Halve the range each time
  • Needs a sorted array
About log2 n steps.

How to answer Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays questions

Trace the array after each pass or each comparison.

  1. 1Write the array and the loop variables.
  2. 2For sorting, record the array after each pass.
  3. 3For binary search, record low, high, mid and the element at mid.
  4. 4Count comparisons or swaps if asked.

Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Binary search

Suppose a library keeps 1,000 books sorted by number and you look up a number by opening the shelf in the middle, then halving the part that can still contain it.

About how many looks are needed at most?

Try it first, then show the solutionSolution
  1. Each look halves the number of books left: 1,000 to 500 to 250 and so on.
  2. 2 to the power 10 = 1,024 >= 1,000, so at most about 10 looks.

Answer: About 10 looks at most.

Memory hook: Halve and halve again.

Tricks to remember Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Index starts at zero

    An array of n elements has indices 0 to n - 1.

    Use it when: Indexing.

  • Bubble: big floats up

    The largest value goes to the end after the first pass.

    Use it when: Bubble sort.

  • Binary needs sorted

    No sorted array, no binary search.

    Use it when: Search.

Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Valid index · 1 mark · MCQBoard level

For int a[] = new int[5]; which statement causes a run-time exception?
(a) a[0] = 1; (b) a[4] = 9; (c) a[5] = 3; (d) a[2] = a[1] + 1;

Try it first, then show the solutionSolution
  1. Valid indices are 0 to 4.
  2. a[5] is outside the range: ArrayIndexOutOfBoundsException.

Answer: (c) a[5] = 3;

Example 2: Output of a program · 3 marks · Short answerBoard level

Write the output of:

int a[] = {12, 45, 7, 33, 21};
int max = a[0], min = a[0], sum = 0;
for (int i = 0; i < a.length; i++) {
    if (a[i] > max) max = a[i];
    if (a[i] < min) min = a[i];
    sum += a[i];
}
System.out.println(max + " " + min + " " + sum + " " + (double) sum / a.length);

(3 marks)

Try it first, then show the solutionSolution
  1. max = 45, min = 7.
  2. sum = 12 + 45 + 7 + 33 + 21 = 118.
  3. (double) sum / 5 = 118.0 / 5 = 23.6.

Answer: 45 7 118 23.6

Example 3: Trace a bubble sort · 6 marks · Long answerHarder board level

Sort 5, 2, 9, 1, 7 in ascending order by bubble sort. Show the array after each pass, and count the swaps and the comparisons (without a flag). (6 marks)

Try it first, then show the solutionSolution
  1. Pass 1: (5,2) swap, (5,9) no, (9,1) swap, (9,7) swap gives 2 5 1 7 9 (3 swaps).
  2. Pass 2: (2,5) no, (5,1) swap, (5,7) no gives 2 1 5 7 9 (1 swap).
  3. Pass 3: (2,1) swap, (2,5) no gives 1 2 5 7 9 (1 swap).
  4. Pass 4: (1,2) no gives 1 2 5 7 9 (0 swaps).
  5. Swaps = 5; comparisons = 4 + 3 + 2 + 1 = 10.

Answer:

PassArraySwaps
12 5 1 7 93
22 1 5 7 91
31 2 5 7 91
41 2 5 7 90

Total swaps 5, comparisons 10.

Example 4: Binary search · 5 marks · Long answerHarder board level

In the sorted array {3, 8, 12, 17, 25, 31, 40} trace the binary search for 31 and then for 10, giving low, high, mid and the number of comparisons. (5 marks)

Try it first, then show the solutionSolution
  1. Key 31: low 0, high 6, mid 3, a[3] = 17 < 31 so low = 4; low 4, high 6, mid 5, a[5] = 31: found at index 5 after 2 comparisons.
  2. Key 10: mid 3 (17 > 10) so high = 2; mid 1 (8 < 10) so low = 2; mid 2 (12 > 10) so high = 1; now low (2) > high (1): not found, after 3 comparisons.

Answer: 31 is found at index 5 in 2 comparisons; 10 is not found after 3 comparisons.

Example 5: Insertion sort and reasoning · 5 marks · Long answerHOTS / competency

Show the array 7, 3, 5, 1 after each pass of an insertion sort. Then explain why binary search cannot be used on 7, 3, 5, 1 directly, and how many comparisons a linear search for 5 makes. (5 marks)

Try it first, then show the solutionSolution
  1. Pass 1 (key 3): 3 7 5 1.
  2. Pass 2 (key 5): 3 5 7 1.
  3. Pass 3 (key 1): 1 3 5 7.
  4. Binary search assumes a sorted order to decide which half to discard; the unsorted array may hide the key in the half that is thrown away.
  5. Linear search for 5 compares with 7, 3, 5: 3 comparisons.

Answer: After passes: 3 7 5 1; 3 5 7 1; 1 3 5 7. Binary search needs a sorted array; a linear search for 5 makes 3 comparisons.

Where students lose marks in Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays

The slips examiners see most often, and what to write instead.

  • Mistake: Using index n

    Counting from 1.

    Wrong working

    for (i = 0; i <= a.length; i++)

    Right working

    for (i = 0; i < a.length; i++)

    Do this: Last index is length - 1.

    Usually costs: 1-2 marks.

  • Mistake: Binary search on an unsorted array

    Ignoring the condition.

    Wrong working

    Binary search on 7 3 5 1

    Right working

    Sort first

    Do this: State the pre-condition.

    Usually costs: 2 marks.

  • Mistake: Integer division in the average

    118 / 5 gives 23.

    Wrong working

    sum / a.length = 23

    Right working

    (double) sum / a.length = 23.6

    Do this: Cast before dividing.

    Usually costs: 1-2 marks.

Exam tips

  • Show the array after each pass in sort questions.
  • State "array must be sorted" for binary search.
  • Write the loop bounds carefully (< length).

Practice questions on Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    The index of the last element of an array of 10 elements is:
    (a) 9 (b) 10 (c) 11 (d) 0

    Hint

    Zero-based.

    Answer

    (a) 9

  2. Q2 · 3 marks · Short answerBoard level

    Write a Java code segment to find and print the sum of the elements of the array int a[] = {4, 9, 2, 7}. State the output. (3 marks)

    Hint

    Accumulate in a loop.

    Answer

    int s = 0; for (int i = 0; i < a.length; i++) s += a[i]; System.out.println(s); Output: 22.

  3. Q3 · 5 marks · Long answerHarder board level

    Trace the selection sort for 64, 25, 12, 22, 11 and write the array after each pass. Count the swaps. (5 marks)

    Hint

    Smallest to the front.

    Answer

    Pass 1: 11 25 12 22 64; pass 2: 11 12 25 22 64; pass 3: 11 12 22 25 64; pass 4: 11 12 22 25 64. Swaps (with a swap each pass) = 4, of which the last swaps an element with itself.

  4. Q4 · 5 marks · Long answerHarder board level

    Given int m[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; write the output of a program that prints the row sums, the main diagonal sum and the other diagonal sum. (5 marks)

    Hint

    i == j and i + j == 2.

    Answer

    Row sums: 6, 15, 24. Main diagonal: 1 + 5 + 9 = 15. Other diagonal: 3 + 5 + 7 = 15.

  5. Q5 · 3 marks · Short answerHOTS / competency

    A programmer wrote: int a[] = {5, 1, 4}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (a[j] > a[j + 1]) swap. Find the error that occurs at run time, the first index at which it happens, and the correction. (3 marks)

    Hint

    j + 1 goes to 3.

    Answer

    When j = 2, a[j + 1] = a[3] is outside 0 to 2: ArrayIndexOutOfBoundsException. The inner loop must run to j < 2 (that is, j < n - 1 - i).

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.First index of an array?Show answer

    0.

  2. 2.Binary search needs?Show answer

    A sorted array.

  3. 3.Comparisons in bubble sort for n=5?Show answer

    10.

  4. 4.Default value of int array?Show answer

    0.

  5. 5.Passing an array to a method passes?Show answer

    A reference.

Arrays in Java: One-dimensional Arrays, Searching, Sorting and 2D Arrays: frequently asked questions

Can array size change?

No, it is fixed once created.

What is a.length?

The number of elements.

Is binary search always faster?

On sorted arrays, yes.

What is a jagged array?

An array of arrays of unequal lengths.