Java Program: Bubble Sort to Sort Characters

This program sorts the characters of a given string using the Bubble Sort algorithm. Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

import java.util.Scanner;

public class BubbleSortCharacters {

    // Method to sort characters in a string using Bubble Sort
    public static String bubbleSort(String input) {
        char[] characters = input.toCharArray(); // Convert string to character array
        int n = characters.length;

        // Bubble Sort algorithm
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                // Swap if the current character is greater than the next
                if (characters[j] > characters[j + 1]) {
                    char temp = characters[j];
                    characters[j] = characters[j + 1];
                    characters[j + 1] = temp;
                }
            }
        }
        return new String(characters); // Convert character array back to string
    }

    // Main method
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input: User enters a string
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Sort the characters in the string
        String sortedString = bubbleSort(input);

        // Output: Display the sorted string
        System.out.println("Sorted characters: " + sortedString);
    }
}

Code Breakdown

1. Import Statement

  • Line: import java.util.Scanner;
  • This imports the Scanner class to read user input.

2. Class Declaration

  • Line: public class BubbleSortCharacters {
  • This declares a public class named BubbleSortCharacters.

3. Bubble Sort Method

  • Line: public static String bubbleSort(String input) {
  • This method sorts the characters of the input string and returns the sorted string.

4. Convert String to Character Array

char[] characters = input.toCharArray();
  • Converts the input string into an array of characters for sorting.

5. Bubble Sort Algorithm

for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - 1 - i; j++) {
        if (characters[j] > characters[j + 1]) {
            char temp = characters[j];
            characters[j] = characters[j + 1];
            characters[j + 1] = temp;
        }
    }
}
  • This nested loop implements the Bubble Sort algorithm. It compares adjacent characters and swaps them if they are in the wrong order.

6. Convert Character Array Back to String

  • Line: return new String(characters);
  • Converts the sorted character array back into a string.

Main Method

7. Create Scanner Object

  • Line: Scanner scanner = new Scanner(System.in);
  • Creates a Scanner object for reading user input.

8. User Input

System.out.print("Enter a string: ");
String input = scanner.nextLine();
  • Prompts the user for input and reads it into the variable input.

9. Sort the Characters

  • Line: String sortedString = bubbleSort(input);
  • Calls the bubbleSort method with the user’s input.

10. Display the Result

System.out.println("Sorted characters: " + sortedString);
  • Displays the sorted characters to the user.

Conclusion

This program uses the Bubble Sort algorithm to sort the characters of a string. It demonstrates fundamental programming concepts such as methods, loops, and user input. If you have any questions or need clarification on any part, feel free to ask!



0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *