CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Bubble sort technique in Java

JAVA November 19, 2025 5 views 0 likes
Define a class to accept 10 characters from a user. Using bubble sort technique arrange them in ascending order. Display the sorted array and original array.

Answer


Character Bubble Sort Program in Java

Character Bubble Sort Program in Java


import java.util.Scanner;

public class KboatCharBubbleSort
{
    public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        char ch[] = new char[10];
        System.out.println("Enter 10 characters:");
        for (int i = 0;  i < ch.length; i++) {
            ch[i] = in.nextLine().charAt(0);
        }

        System.out.println("Original Array");
        for (int i = 0;  i < ch.length; i++) {
            System.out.print(ch[i] + " ");
        }
        
        //Bubble Sort
        for (int i = 0; i < ch.length - 1; i++) {
            for (int j = 0; j < ch.length - 1 - i; j++) {
                if (ch[j] > (ch[j + 1])) {
                    char t = ch[j];
                    ch[j] = ch[j + 1];
                    ch[j + 1] = t;
                }
            }
        }
        
        System.out.println("\nSorted Array");
        for (int i = 0;  i < ch.length; i++) {
            System.out.print(ch[i] + " ");
        }
    }
}
5 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285