★★★★★
"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
ICSE Class 10 · Computer Applications (Java) · Sorting and searching 2027
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
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.

The batch / ICSE CA
Class 9 and 10 Computer Applications in a mini batch, with sorts and searches traced by hand before each one is run in BlueJ.
Open the course →
Next step / ISC
For students moving to ISC Computer Science, where more sorting methods and larger array problems follow.
Open the course →
Beyond the syllabus / DSA
For the student who wants to measure algorithms, compare sorts properly and meet faster ones.
Open the course →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 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.
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.
| Variable | Type | Purpose |
|---|---|---|
| n | int | number of elements |
| a | int[] | the numbers being sorted |
| p | int | pass counter, from 0 |
| j | int | position being compared |
| t | int | temporary store for the swap |
| swaps | int | swaps made in this pass |
| i | int | loop counter for input and printing |
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
| j | Compare | Swap? | Array after the step |
|---|---|---|---|
| 0 | 47 > 12 | yes | 12 47 38 5 26 |
| 1 | 47 > 38 | yes | 12 38 47 5 26 |
| 2 | 47 > 5 | yes | 12 38 5 47 26 |
| 3 | 47 > 26 | yes | 12 38 5 26 47 |
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.
Change one sign: swap when a[j] < a[j + 1]. Now the smallest value sinks to the end each pass.
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 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.
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.
| Variable | Type | Purpose |
|---|---|---|
| a | int[] | six scores |
| i | int | the place being filled |
| j | int | scans the unsorted part |
| big | int | index of the largest found |
| t | int | temporary store for the swap |
| k | int | loop counter for printing |
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
| Pass | Searched | Largest | Swap | Array after |
|---|---|---|---|---|
| 1 | index 0 to 5 | 92 at 3 | a[0] with a[3] | 92 88 45 61 70 88 |
| 2 | index 1 to 5 | 88 at 1 | with itself | 92 88 45 61 70 88 |
| 3 | index 2 to 5 | 88 at 5 | a[2] with a[5] | 92 88 88 61 70 45 |
| 4 | index 3 to 5 | 70 at 4 | a[3] with a[4] | 92 88 88 70 61 45 |
| 5 | index 4 to 5 | 61 at 4 | with itself | 92 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.
| Point | Bubble sort | Selection sort |
|---|---|---|
| What is compared | Neighbours, a[j] and a[j + 1] | Each element with the best found so far |
| Swaps in one pass | Any number | At most one |
| After pass 1 (ascending) | Largest value at the end | Smallest value at the start |
| Comparisons for n elements | n(n - 1) / 2 | n(n - 1) / 2 |
| Can stop early | Yes, with a no-swap flag | No |
Q3. Linear search or binary search?
Linear search checks the elements one by one and works on any array, sorted or not. Binary search needs the array sorted; it checks the middle element and throws away half of what is left at every step, so ten elements need at most four checks.
import java.util.Scanner;
class LinearNames {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String city[] = new String[5];
int pin[] = new int[5];
System.out.println("Enter 5 cities, each followed by its PIN code:");
for (int i = 0; i < 5; i++) {
city[i] = sc.next();
pin[i] = sc.nextInt();
}
System.out.print("City to search: ");
String key = sc.next();
int pos = -1;
for (int i = 0; i < 5; i++) {
if (city[i].equalsIgnoreCase(key)) {
pos = i;
break; // stop at the first match
}
}
if (pos == -1)
System.out.println(key + " is not in the list");
else
System.out.println(city[pos] + " found at position " + (pos + 1) + ", PIN " + pin[pos]);
}
}
pos starts at -1, a value no real index can have, so after the loop it tells you plainly whether the search succeeded.
| Variable | Type | Purpose |
|---|---|---|
| city | String[] | five city names |
| pin | int[] | PIN code of each city, same index |
| key | String | the city searched for |
| pos | int | index found, or -1 |
| i | int | loop counter |
Enter 5 cities, each followed by its PIN code: Pune 411001 Kochi 682001 Agra 282001 Shimla 171001 Patna 800001 City to search: shimla Shimla found at position 4, PIN 171001 Enter 5 cities, each followed by its PIN code: Pune 411001 Kochi 682001 Agra 282001 Shimla 171001 Patna 800001 City to search: Goa Goa is not in the list
import java.util.Scanner;
class BinaryTrace {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a[] = {6, 14, 19, 27, 33, 41, 58, 62, 75, 90}; // already in ascending order
System.out.print("Enter the number to search: ");
int key = sc.nextInt();
int low = 0, high = a.length - 1, pos = -1;
while (low <= high) {
int mid = (low + high) / 2;
System.out.println("low=" + low + " high=" + high + " mid=" + mid + " a[mid]=" + a[mid]);
if (a[mid] == key) {
pos = mid;
break;
} else if (key > a[mid])
low = mid + 1; // throw away the left half
else
high = mid - 1; // throw away the right half
}
if (pos == -1)
System.out.println(key + " not found");
else
System.out.println(key + " found at index " + pos);
}
}
mid = (low + high) / 2 is integer division, so it rounds down: low 2 and high 3 give mid 2.
| Variable | Type | Purpose |
|---|---|---|
| a | int[] | ten values in ascending order |
| key | int | value searched for |
| low, high | int | ends of the part still in play |
| mid | int | middle index checked |
| pos | int | index found, or -1 |
Enter the number to search: 62 low=0 high=9 mid=4 a[mid]=33 low=5 high=9 mid=7 a[mid]=62 62 found at index 7 Enter the number to search: 20 low=0 high=9 mid=4 a[mid]=33 low=0 high=3 mid=1 a[mid]=14 low=2 high=3 mid=2 a[mid]=19 low=3 high=3 mid=3 a[mid]=27 20 not found
| Step | low | high | mid | a[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 9 | 4 | 33 | 20 < 33, so high = 3 |
| 2 | 0 | 3 | 1 | 14 | 20 > 14, so low = 2 |
| 3 | 2 | 3 | 2 | 19 | 20 > 19, so low = 3 |
| 4 | 3 | 3 | 3 | 27 | 20 < 27, so high = 2 |
| end | 3 | 2 | low > high, the loop stops: not found | ||
| Point | Linear search | Binary search |
|---|---|---|
| Array must be sorted | No | Yes |
| Checks for 10 elements, worst case | 10 | 4 |
| Works on Strings | Yes, with equals | Yes, with compareTo, if sorted |
| Finds every occurrence | Yes, if you do not break | No, it finds one |
Q4. What array programs does Section B set?
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.
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.
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.
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.
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.
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.
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.
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.
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?
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.
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.
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.
Full Section B answers are marked on screen: input loop, sort or search logic, output format, comments and the variable description table.
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.
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
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
₹2,999
per month, two live classes a week
Option 2 · Choose your own timing
₹4,999
per month, one private class a weektwo private classes a week, at a time set with you
Outside India: the ICSE Computer Applications course runs as personalised one to one, two private classes a week at a time set in your own time zone. The mini batch is taught for families in India.
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?
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 live | Time difference | Usual class window, local time | Format |
|---|---|---|---|
| India (IST) every Indian city | IST | Tue 5:30 PM, Wed 8:00 PM | Mini batch or 1-on-1 |
| United Arab Emirates Dubai, Sharjah | 1 h 30 min behind India | 3:00 PM to 9:00 PM | Personalised 1-on-1 |
| Indonesia and Thailand West Java, Bangkok area | 1 h 30 min ahead of India | 3:30 PM to 9:00 PM | Personalised 1-on-1 |
| Singapore Singapore | 2 h 30 min ahead of India | 3:30 PM to 9:00 PM | Personalised 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
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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