Bubble sort Java example. Simple solution. – IT Blog
IT Blog
Boris IT Expert

Bubble sort Java example. Simple solution.

Boris~November 11, 2020 /Sorting Algorithms

Does bubble sort for an Array List in Java is the best solution? It’s not the best from a complexity estimation point of view. But it is easiest to understand, remember, and implement. It is better to show not the best algorithm that works than the elegant solution that does not work. Since the sorting algorithm is a widespread case on any technical interview, I would recommend keeping a bubble sort in your pocket. The main idea here is pretty straight forward. We are looping through our array until the array becomes sorted. With each iteration, we will compare the current element with the element after it. If needed, we should swap them. And we should repeat this process until all elements will find their place. When it happens, our Boolean flag stops the loop and returns the result.

Bubble sort of the Array List? The step-by-step solution in Java.

Best implementation with the pictures. The most obvious solution below:

//Flag stops loop when array sorted
        boolean flag = false;
//Looping again and again until array flag sorted becomes true
        while (!flag)
bubble sort java
 //Flag suppose to be true because if there are no values to switch means array is sorted
            flag = true;
//Loop goes through the array and swap values if current value more than next
            for (int i = 0; i < array.length - 1; i++) {
//Comparing current element with the next one
                if (array[i] > array[i + 1])
if (array[i] > array[i + 1]) {
//In case current bigger than next one we make switch
//Saving current value in temporary variable
                    int temporary = array[i];
//Setting current element value of the next element
                    array[i] = array[i + 1];
//Setting the next element value of the current element
                    array[i + 1] = temporary;
//Make sorted flag false to run loop one more time
                    flag = false;
                }

Animation:

bubble sort animation

Full schema:

Code example:

private void bubbleSort() {
//Creating array
        int[] array = new int[]{4, 2, 1, 11, 31, 9099, 6, 3};

//Flag stops loop when array sorted
        boolean flag = false;

 //Looping again and again until array flag sorted becomes true
        while (!flag) {
//Flag suppose to be true because if there are no values to switch means array is sorted
            flag = true;

//Loop goes through the array and swap values if current value more than next
            for (int i = 0; i < array.length - 1; i++) {

//Comparing current element with the next one
                if (array[i] > array[i + 1]) {
 //In case current bigger than next one we make switch

 //Saving current value in temporary variable
                    int temporary = array[i];

//Setting current element value of the next element
                    array[i] = array[i + 1];

//Setting the next element value of the current element
                    array[i + 1] = temporary;

//Make sorted flag false to run loop one more time
                    flag = false;
                }
            }
        }

        for (int i : array) {
            Log.d("arrayList", "-> " + i);
        }
    }

In this article, you see an example of the bubble sort in Java.

Full code and more examples in my facebook group.

Useful links:

Leave Any Questions Related To This Article Below!