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:

• Setup the boolean flag to stop the loop if the sorting of the array list is done. And continue looping if some of the elements still need to be checked;
``````//Flag stops loop when array sorted
boolean flag = false;``````
• Start the “while” loop. It works until the boolean flag is equal to false;
``````//Looping again and again until array flag sorted becomes true
while (!flag)``````
• Start the “while” loop. It works until the boolean flag is equal to false. This is a critical moment. The reason for that is, if the flag remains true, we will stop the loop. If not, after switching the elements, the flag becomes false, and we repeat the iteration ;
• Set boolean flag to true. This flag will stop the “while” looping;
• Start a new loop through the array and compare the current element with the next element;
`````` //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 the current element is more than the next element, we need to swap them. Here is an essential trick: after switching, we need to set the boolean flag to false. It will trigger one more iteration;
``````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;
}``````
• And these steps will rerun again and again until there will be no more conditions to swap the elements, set a boolean flag to false, and rerun the while loop;

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.