Bubble Sort Algorithm in Java

Lorenzo Orlando
4 min readJan 31, 2024
Brief visual explanation of Bubble Sort algorithm

Sorting is a fundamental operation in Computer Science, and various algorithms have been developed to efficiently organise data. One of the simplest sorting algorithms is Bubble Sort, which even though it is not the most efficient, serves as a good introduction to sorting concepts.

Before we begin, I want to tell you that you can find all the shown code in my Github Repository about sorting algorithms.

You can find it by clicking HERE

What is Bubble Sort?

Bubble Sort is a straightforward sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

How Bubble Sort Works Step by Step

Let’s understand the Bubble Sort algorithm through a step-by-step example:

Consider an array of integers: [5, 9, 1, 2, 8, 7, 3]

  1. First Pass:
  • Compare 5 and 9. No swap needed.
  • Compare 9 and 1. Swap them: [5, 1, 9, 2, 8, 7, 3]
  • Compare 9 and 2. Swap them: [5, 1, 2, 9, 8, 7, 3]
  • … Continue until the end of the array.

--

--

Lorenzo Orlando

Experienced Java Engineer. Love to talk about Java & Spring Boot.