ICSE Class 10 · Computer Applications (Java) · Sorting and searching 2027

Bubble sort and selection sort for ICSE Class 10, traced pass by pass.

Bubble sort and selection sort are the two sorting methods ICSE Class 10 expects, and linear and binary search are the two searches. Examiners ask for the program, and just as often for the state of the array after a given pass. This page shows each algorithm as a full Scanner program that prints its own passes, with a trace table you can check your dry run against, then four Section B array programs. Every run is from Java 21. In our live mini batch the traces are written by hand before BlueJ confirms them.

Live online · pass-by-pass traces · bubble, selection, linear, binary · four Section B array programs · first class free

Start here

Courses where sorting and searching are taught

The ICSE Java course where the mini batch traces these passes, the Class 11 and 12 Computer Science course where ISC adds insertion sort and two-dimensional work, and the data structures course for the student who wants to know why binary search is so much faster.

Ans. The short version

For ICSE Class 10, bubble sort compares neighbouring elements and swaps them when they are out of order, so after each pass the largest remaining value settles at the end. Selection sort finds the smallest (or largest) remaining value and swaps it into place, making at most one swap per pass. Linear search checks every element in turn and works on any array; binary search halves a sorted array each step using low, high and mid. Questions ask for the program and for the array after a given pass. Modern Age Coders teaches these live in a Tuesday and Wednesday mini batch in India, or personalised one to one.

Q1. How does bubble sort work, pass by pass?

Bubble sort ICSE Class 10: the program and its trace

Bubble sort walks along the array comparing a[j] with a[j + 1] and swaps them if the left one is bigger. One walk is a pass. After pass 1 the largest value is at the end, after pass 2 the second largest is next to it, and an array of n elements needs at most n minus 1 passes.

BubbleTrace.javaascending, prints each pass
import java.util.Scanner;
class BubbleTrace {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("How many numbers? ");
        int n = sc.nextInt();
        int a[] = new int[n];
        System.out.println("Enter " + n + " numbers:");
        for (int i = 0; i < n; i++)
            a[i] = sc.nextInt();
        for (int p = 0; p < n - 1; p++) {            // pass number p + 1
            int swaps = 0;
            for (int j = 0; j < n - 1 - p; j++) {     // compare neighbours
                if (a[j] > a[j + 1]) {
                    int t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                    swaps++;
                }
            }
            System.out.print("After pass " + (p + 1) + ":");
            for (int i = 0; i < n; i++)
                System.out.print(" " + a[i]);
            System.out.println("   swaps = " + swaps);
        }
    }
}

The inner limit n - 1 - p shrinks each pass, because the values already settled at the end need no more comparing.

VariableTypePurpose
nintnumber of elements
aint[]the numbers being sorted
pintpass counter, from 0
jintposition being compared
tinttemporary store for the swap
swapsintswaps made in this pass
iintloop counter for input and printing
Sample runJava 21
How many numbers? 5
Enter 5 numbers:
47 12 38 5 26
After pass 1: 12 38 5 26 47   swaps = 4
After pass 2: 12 5 26 38 47   swaps = 2
After pass 3: 5 12 26 38 47   swaps = 1
After pass 4: 5 12 26 38 47   swaps = 0

Inside pass 1: every comparison

jCompareSwap?Array after the step
047 > 12yes12 47 38 5 26
147 > 38yes12 38 47 5 26
247 > 5yes12 38 5 47 26
347 > 26yes12 38 5 26 47

Count the comparisons

Passes make 4, 3, 2 and 1 comparisons: 10 in all for five elements, whatever the data. That total, n(n - 1) / 2, is a favourite two-mark question.

Descending order

Change one sign: swap when a[j] < a[j + 1]. Now the smallest value sinks to the end each pass.

Stopping early

A pass with no swaps means the array is sorted. Some answers add a boolean flag and break out; it is an improvement, not a requirement.

Q2. How is selection sort different?

Selection sort ICSE Class 10, in descending order, traced

Selection sort looks through the unsorted part for the largest (or smallest) value, remembers only its position, and makes a single swap at the end of the pass. Bubble sort may swap many times in a pass; selection sort swaps at most once.

SelectDesc.javadescending, prints each pass
import java.util.Scanner;
class SelectDesc {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int a[] = new int[6];
        System.out.println("Enter 6 scores:");
        for (int i = 0; i < 6; i++)
            a[i] = sc.nextInt();
        for (int i = 0; i < 5; i++) {
            int big = i;                           // position of the largest so far
            for (int j = i + 1; j < 6; j++)
                if (a[j] > a[big])
                    big = j;
            int t = a[i];                          // one swap per pass
            a[i] = a[big];
            a[big] = t;
            System.out.print("Pass " + (i + 1) + " (largest at index " + big + "):");
            for (int k = 0; k < 6; k++)
                System.out.print(" " + a[k]);
            System.out.println();
        }
    }
}

For ascending order, look for the smallest instead: change > to < in the comparison and rename big to small.

VariableTypePurpose
aint[]six scores
iintthe place being filled
jintscans the unsorted part
bigintindex of the largest found
tinttemporary store for the swap
kintloop counter for printing
Sample runJava 21
Enter 6 scores:
61 88 45 92 70 88
Pass 1 (largest at index 3): 92 88 45 61 70 88
Pass 2 (largest at index 1): 92 88 45 61 70 88
Pass 3 (largest at index 5): 92 88 88 61 70 45
Pass 4 (largest at index 4): 92 88 88 70 61 45
Pass 5 (largest at index 4): 92 88 88 70 61 45

The trace, one row per pass

PassSearchedLargestSwapArray after
1index 0 to 592 at 3a[0] with a[3]92 88 45 61 70 88
2index 1 to 588 at 1with itself92 88 45 61 70 88
3index 2 to 588 at 5a[2] with a[5]92 88 88 61 70 45
4index 3 to 570 at 4a[3] with a[4]92 88 88 70 61 45
5index 4 to 561 at 4with itself92 88 88 70 61 45

In pass 2 the 88 at index 5 is not chosen, because a[j] > a[big] is strict: an equal value does not replace the one already found. Examiners like duplicate values for exactly this reason.

PointBubble sortSelection sort
What is comparedNeighbours, a[j] and a[j + 1]Each element with the best found so far
Swaps in one passAny numberAt most one
After pass 1 (ascending)Largest value at the endSmallest value at the start
Comparisons for n elementsn(n - 1) / 2n(n - 1) / 2
Can stop earlyYes, with a no-swap flagNo

Q4. What array programs does Section B set?

Array programs ICSE Class 10: the four standard Section B shapes

Most Section B array questions are one of four shapes: sort words alphabetically, sort one array and carry a partner array with it, search a sorted list, or work out the highest, lowest and average. Each shape below is a full program with Scanner input and a real run.

Shape 1: sort names in alphabetical order

SortNames.javabubble sort on Strings
import java.util.Scanner;
class SortNames {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String nm[] = new String[6];
        System.out.println("Enter 6 names:");
        for (int i = 0; i < 6; i++)
            nm[i] = sc.next();
        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 5 - i; j++)
                if (nm[j].compareToIgnoreCase(nm[j + 1]) > 0) {
                    String t = nm[j];
                    nm[j] = nm[j + 1];
                    nm[j + 1] = t;
                }
        System.out.println("In alphabetical order:");
        for (int i = 0; i < 6; i++)
            System.out.println(nm[i]);
    }
}

compareToIgnoreCase returns a positive number when the left word comes later in the alphabet, so > 0 plays the part of > in a number sort.

Sample runJava 21
Enter 6 names:
Zoya aarav Meera Kabir neel Ishaan
In alphabetical order:
aarav
Ishaan
Kabir
Meera
neel
Zoya

Variables: nm String[] names; i, j int pass and position; t String temporary. With plain compareTo the small-letter names would sort after Zoya, because small letters have higher codes.

Shape 2: a merit list from two parallel arrays

MeritList.javasort marks, move names too
import java.util.Scanner;
class MeritList {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String name[] = new String[5];
        int marks[] = new int[5];
        System.out.println("Enter name and marks of 5 students:");
        for (int i = 0; i < 5; i++) {
            name[i] = sc.next();
            marks[i] = sc.nextInt();
        }
        for (int i = 0; i < 4; i++) {
            int big = i;
            for (int j = i + 1; j < 5; j++)
                if (marks[j] > marks[big])
                    big = j;
            int t = marks[i]; marks[i] = marks[big]; marks[big] = t;
            String s = name[i]; name[i] = name[big]; name[big] = s;   // move the name with its marks
        }
        System.out.println("Rank\tName\tMarks");
        for (int i = 0; i < 5; i++)
            System.out.println((i + 1) + "\t" + name[i] + "\t" + marks[i]);
    }
}

Whatever swap is made in marks must be made in name at the same two indices, or names and marks drift apart.

Sample runJava 21
Enter name and marks of 5 students:
Tara 78 Dev 91 Anya 66 Rohan 84 Sana 91
Rank	Name	Marks
1	Dev	91
2	Sana	91
3	Rohan	84
4	Tara	78
5	Anya	66

Variables: name String[], marks int[]; i, j, big int; t int and s String temporaries. Dev stays ahead of Sana because the strict > does not move an equal mark.

Shape 3: binary search with an order check

RollSearch.javacheck sorted, then search
import java.util.Scanner;
class RollSearch {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int roll[] = new int[8];
        System.out.println("Enter 8 roll numbers in ascending order:");
        for (int i = 0; i < 8; i++)
            roll[i] = sc.nextInt();
        // check the order first: binary search is only valid on sorted data
        boolean sorted = true;
        for (int i = 0; i < 7; i++)
            if (roll[i] > roll[i + 1])
                sorted = false;
        if (!sorted) {
            System.out.println("Roll numbers are not in ascending order");
            return;
        }
        System.out.print("Roll number to find: ");
        int key = sc.nextInt();
        int low = 0, high = 7, steps = 0, pos = -1;
        while (low <= high) {
            int mid = (low + high) / 2;
            steps++;
            if (roll[mid] == key) { pos = mid; break; }
            if (roll[mid] < key) low = mid + 1;
            else high = mid - 1;
        }
        if (pos >= 0)
            System.out.println("Present at position " + (pos + 1) + " after " + steps + " comparisons");
        else
            System.out.println("Absent, decided after " + steps + " comparisons");
    }
}

The order check costs seven comparisons and saves a wrong answer: binary search on unsorted data can miss a value that is there.

Sample runsJava 21
Enter 8 roll numbers in ascending order:
3 7 12 18 21 25 30 34
Roll number to find: 25
Present at position 6 after 2 comparisons

Enter 8 roll numbers in ascending order:
3 7 12 18 21 25 30 34
Roll number to find: 4
Absent, decided after 3 comparisons

Enter 8 roll numbers in ascending order:
3 7 12 9 21 25 30 34
Roll numbers are not in ascending order

Variables: roll int[]; sorted boolean; key, low, high, mid, steps, pos int.

Shape 4: highest, lowest, average and above-average count

MaxMinAvg.javaone pass, then a second
import java.util.Scanner;
class MaxMinAvg {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        double t[] = new double[7];
        System.out.println("Enter the maximum temperature for 7 days:");
        for (int i = 0; i < 7; i++)
            t[i] = sc.nextDouble();
        double max = t[0], min = t[0], sum = 0;
        int hot = 0;                               // day number of the hottest day
        for (int i = 0; i < 7; i++) {
            sum = sum + t[i];
            if (t[i] > max) { max = t[i]; hot = i; }
            if (t[i] < min) min = t[i];
        }
        double avg = sum / 7;
        int above = 0;
        for (int i = 0; i < 7; i++)
            if (t[i] > avg) above++;
        System.out.println("Highest = " + max + " on day " + (hot + 1));
        System.out.println("Lowest = " + min);
        System.out.println("Average = " + Math.round(avg * 100) / 100.0);
        System.out.println("Days above average = " + above);
    }
}

max and min start at t[0], not at 0: a week of temperatures all below 0 would otherwise report a maximum of 0.

Sample runJava 21
Enter the maximum temperature for 7 days:
31.5 33 29.5 35.5 34 30 32
Highest = 35.5 on day 4
Lowest = 29.5
Average = 32.21
Days above average = 3

Variables: t double[]; max, min, sum, avg double; hot, above, i int. The average needs the whole first pass, so counting days above it needs a second loop.

Rows and columns are the next step: 2D array programs for ICSE Class 10. Sorting a sentence's letters or words also draws on ICSE Class 10 string programs.

Q5. How are sorting and searching taught in the live classes?

Sorting and searching ICSE: trace on paper, then prove it in BlueJ

The marks in this topic go to students who can produce the array after pass 2 without running anything. So the live classes put the paper trace first and the computer second.

  1. Cards before code

    In the first session students sort a handful of numbered cards on camera, once by bubble and once by selection, saying each comparison aloud. The algorithm is understood before a line of Java is written.

  2. Timed trace sets on Tuesday

    At 5:30 PM IST each student fills pass tables for a fresh array, including duplicates and a value that is not in the list, against the clock.

  3. Programs marked against the scheme

    Full Section B answers are marked on screen: input loop, sort or search logic, output format, comments and the variable description table.

  4. Run with the printing turned on

    At 8:00 PM IST on Wednesday the programs are run in BlueJ with a print inside each pass, exactly as on this page, so every student can compare the machine's trace with their own.

  5. Worksheets, doubts and full mocks

    A worksheet of variations follows each shape, the next class starts with doubts, and from December arrays appear inside complete timed papers.

Searching and sorting also turn up in the one-mark section as short traces; practise those on ICSE Computer Applications output questions. Every other program family is listed on ICSE Class 10 Java programs for practice.

Batch or personalised: you choose

Trace sorts and searches in the mini batch, or one to one

The traces, the four array shapes and the BlueJ runs are the same in both formats. In India, families pick the fixed evening mini batch or a private slot; ICSE students abroad are taught one to one. Monthly billing, no admission fee.

Option 1 · Fixed batch timings

Mini batch, Tuesday 5:30 PM and Wednesday 8 PM

₹2,999

per month, two live classes a week

  • Tuesday 5:30 PM and Wednesday 8:00 PM IST, live
  • A maximum of six ICSE Class 10 students
  • Pass tables filled by hand against a timer
  • Section B answers marked against the scheme on screen
  • Every trace confirmed in BlueJ the next day
Book a free demo in this batch

Option 2 · Choose your own timing

Personalised one to one

₹4,999

per month, one private class a week, at a time set with you

  • A time chosen by the family, in India or abroad
  • Extra traces on whichever sort your child mixes up
  • Parallel arrays and String sorting at their own pace
  • Harder arrays once the basic passes are secure
  • The same mentors and the same 2027 syllabus
Ask for a personalised plan

A student who writes the sort correctly but cannot say what the array looks like after pass 2 usually needs only a few one to one sessions of tracing. A student who wants steady practice across all of Section B is better served by the two batch classes each week. Fees are shown in your own currency.

Studying outside India?

Sorting and searching practice for ICSE students abroad

ICSE schools overseas take the same Computer Applications paper, so the same pass traces and array shapes carry the same Section B marks. Abroad, these classes run one to one in an evening slot in the student's own time zone.

Where you liveTime differenceUsual class window, local timeFormat
India (IST)
every Indian city
ISTTue 5:30 PM, Wed 8:00 PMMini batch or 1-on-1
United Arab Emirates
Dubai, Sharjah
1 h 30 min behind India3:00 PM to 9:00 PMPersonalised 1-on-1
Indonesia and Thailand
West Java, Bangkok area
1 h 30 min ahead of India3:30 PM to 9:00 PMPersonalised 1-on-1
Singapore
Singapore
2 h 30 min ahead of India3:30 PM to 9:00 PMPersonalised 1-on-1

The slot is set after the free demo and can shift around school tests. More for overseas families is on ICSE Computer Applications classes in the Gulf and abroad.

What families say

Rated 4.9 across 547 Google reviews

Real reviews from real families. We neither write nor commission them.

★★★★★

"The one step solution for my son. Modern Age Coders make learning coding so simple that kids love it. The teachers explain complex concepts clearly with practical exercises and interactive content."

Ria Mukherjee

Parent

★★★★★

"Modern Age Coders has been a game-changer for me. I struggled to grasp IT concepts and coding before joining, but their classes transformed everything. I can now confidently write complex programs with ease."

Samriddha Mondal

Student

★★★★★

"One of the most wonderful education centres out there. Education is not limited to school syllabus but focuses on skill development."

Vansh Agarwal

Student

★★★★★

"My child Dhairya is really enjoying the Modern Age Coders classes. This is his first online class and he eagerly looks forward to it. I can already see his improvement, and the teachers are very cooperative."

Sonam Oswal

Parent of Dhairya

★★★★★

"Modern Age Coders have wonderful teachers who teach in a clear, easy and practical way. The teacher boosts students' confidence and inspires them to learn without hesitation."

Sonu Goyal

Parent

★★★★★

"I highly recommend this computer coding class! The teachers are incredibly knowledgeable and passionate about coding."

Ritu Kedia

Parent

The rest of the Class 10 series

More pages for Class 10 board students

Every page in the ICSE Class 10 Computer Applications series, grouped by what you need: classes, each chapter, Section B programs, question practice, notes and revision, and the exam itself.

CBSE Class 10 AI (417): The Mon and Wed 9 PM AI batch CBSE AI 417 syllabus explained CBSE AI important questions Sample paper and mock tests

Questions about sorting and searching

What students ask about sorting and searching in ICSE Class 10

What is the difference between bubble sort and selection sort?

Bubble sort compares neighbouring elements and swaps whenever they are out of order, so it can swap many times in one pass. Selection sort scans the unsorted part for the smallest or largest value and makes one swap per pass. Both make n(n - 1) / 2 comparisons on n elements.

How many passes does bubble sort need?

An array of n elements needs at most n minus 1 passes, because each pass settles one more value at the end and the last value is then automatically in place. Five elements need at most four passes. With a no-swap flag the sort can stop sooner if the array becomes sorted early.

How do you write the array after a given pass?

Trace it by hand. For bubble sort, walk left to right comparing neighbours and swapping, and write the whole array after each pass. For selection sort, find the largest or smallest value in the unsorted part, swap it into place, and write the array. Never skip a pass, even one with no changes.

What is binary search in Java ICSE?

Binary search finds a value in a sorted array by checking the middle element. If the key is larger, it searches the right half by setting low to mid plus 1; if smaller, the left half by setting high to mid minus 1. It stops when the key is found or when low passes high.

Why must the array be sorted for binary search?

Binary search throws away half the array after each check, on the assumption that every value to one side of mid is smaller and every value to the other side is larger. If the array is not sorted that assumption is false, and the search can report a value as absent when it is there.

What is linear search in Java?

Linear search compares the key with each element in turn, from index 0 onwards, until it finds a match or reaches the end. It works on any array, sorted or not, and on Strings with equals. For n elements it needs up to n comparisons, which is slow on large arrays but fine for Class 10 sizes.

How do you sort names alphabetically in Java ICSE?

Use bubble or selection sort exactly as for numbers, but compare with compareTo or compareToIgnoreCase instead of greater than. A positive result means the left String comes later in the alphabet, so the two should be swapped. compareToIgnoreCase stops small and capital letters sorting into separate groups.

Can I use Arrays.sort in the ICSE exam?

When a question asks for bubble sort or selection sort, it is testing whether you can write the algorithm, so a library call would not earn the marks for the logic. Write the nested loops and the swap yourself, with a variable description table, and use the library only where a question clearly allows it.

How many comparisons does binary search need?

Each comparison roughly halves what is left, so an array of 10 elements needs at most 4 comparisons and one of 100 elements needs at most 7. Linear search on the same arrays could need 10 and 100. That gap is the reason binary search is worth the extra condition of sorted data.

Do you teach sorting and searching in live classes?

Yes. In the mini batch on Tuesday and Wednesday evenings in India, students trace passes by hand against a timer, have full array programs marked against the scheme on screen, and check every trace in BlueJ. Families abroad, or who need another time, can take personalised one to one classes.

Start

Book a free live sorting and searching class

In the free demo your child traces bubble sort and binary search on a fresh array by hand, then runs the same program in BlueJ with the mentor and compares the printed passes with the paper ones. Afterwards you get a straight answer on where the gaps are, and on whether the mini batch or one to one suits better.

Prefer to see the whole syllabus first? Go to ICSE Computer Applications important questions or the ICSE Class 10 Java classes.

WhatsApp us · +91 91233 66161 · connect@modernagecoders.com

Every class is live video. The form requests a callback and reserves nothing.

We call within a few hours. No card, no enrolment fee.

Want it sooner? Book a priority demo

A full live class of about 45 to 60 minutes at a slot you pick, with a written skill report and learning roadmap on WhatsApp afterwards. The fee is adjusted against your first month if you enrol.

Book a priority demo

Keep exploring Modern Age Coders

By class and age

Learn more

Free resources

From the blog

Start here