Last Updated:

Bubble sorting Theory -  Example of a Java program

In this article, we'll look at an example of a program for sorting an array or list of items using a bubble sorting algorithm.

Bubble sorting.

 

The bubble sorting algorithm is one of the easiest to implement and slowest in execution time. Its essence is reduced to alternate comparison of neighboring elements of the array.

Bubble sorting

Alternate comparison of neighboring array elements

Example of implementing bubble sorting in Java

Below is a small Program in Java that demonstrates the "full power" of bubble sorting.

Note that bubble sorting will continue as long as the elements are rearranged in places, that is, until the array is fully sorted.

The result of performing bubble sorting in Java in descending order:

It is also worth noting that our program sorts the elements in descending order, that is, from larger to smaller. In order for the bubble algorithm to sort in ascending order (from smaller to larger), you need to change the character in the sort condition to :<>

on Java

The result of performing bubble sorting in Java in ascending order:

This is the implementation of the bubble sorting algorithm in Java. Let me remind you that this article is part of the Java section for beginners. Stay tuned.