Bubble Sort Char Array Java

Bubble Sort Char Array Java | In this post we will discuss how to apply the bubble sorting technique for the char array in Java. We will take an array of characters and develop bubbleSort() method to sort them using the bubble sorting technique.

What is Bubble Sorting in Java?

The bubble sort is a sorting technique that is used to sort the array. This sorting method works like a dictionary, while searching in a dictionary we first open the middle number and then compare the key to the middle number if the middle number is greater than the key we search for the key to the left of the middle number or else if the middle number is lesser than the key then we search for the key for the right side.

The key is the element which is needed to be searched. To use the bubble sort technique we need three elements high, low and mid. See the below implementation of the bubble sort. The example for the same is as follows.

Program for Bubble Sort Char Array Java

import java.util.Arrays;

public class Main {
   public static void bubbleSort(char[] array) {
      int num = array.length;
      char temp = 0;
      for (int i = 0; i < num; i++) {
         for (int j = 1; j < (num - i); j++) {
            if (array[j - 1] > array[j]) {
               temp = array[j - 1];
               array[j - 1] = array[j];
               array[j] = temp;

   public static void main(String[] args) {
      char array[] = { 'a', 'z', 'g', 'f', 'd', 'c', 'a' };
      System.out.println("Array Before Bubble Sort: " + Arrays.toString(array));
      System.out.println("Array After Bubble Sort: " + Arrays.toString(array));


Array Before Bubble Sort: [a, z, g, f, d, c, a]
Array After Bubble Sort: [a, a, c, d, f, g, z]

To display the array of characters we have used the toString() method of the Arrays class. The Arrays class of java.util package contains many methods for array manipulation like searching (binary search), sorting (Quick Sort), copying, converting to string, and e.t.c.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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