ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2023 ICSE Computer Science Paper



Share with a Friend

Solved 2023 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Bubble Sort Program - ICSE 2023 Computer Science

Question 4
Define a class to accept 10 characters from a user. Using bubble sort technique to arrange them in ascending order. Display the sorted array and original array.

import java.util.Scanner; class Sort{ public static void main(String[] args){ Scanner in = new Scanner(System.in); char a[] = new char[10]; System.out.println("Enter 10 characters:"); for(int i = 0; i < a.length; i++) a[i] = in.nextLine().charAt(0); System.out.println("Original array:"); for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a.length - 1 - i; j++){ if(a[j] > a[j + 1]){ char temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } System.out.println("Sorted array:"); for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } }

Output

 
 OUTPUT : 
Enter 10 characters:
F
E
H
G
A
D
B
I
C
J
Original array:
F E H G A D B I C J 
Sorted array:
A B C D E F G H I J