Buzz Code
Published in

Buzz Code

Java 8 | Sorting Algorithm — Bubble Sort

Hi y’all! Today We’re going to take a look at one of the sorting algorithms — Bubble Sort.

[Bubble Sort]

Now let’s see how this bubble sort works!

Let’s say there’s an array like above, and we have to sort this array in ascending order.

This is what the process looks like when we sort the array with the bubble sort. It check the array from the first index and the next index and see which one’s bigger. If the value of the index is bigger than the next one, it changes the value of two of them. So after the first for loop finished, the array would look like below.

Biggest number in the array will be moved to the end of the array. So at the second loop you don’t need to check the last index, because that’s already the biggest number in the array.

Bubble sort is the easiest sorting algorithm to code, but it’s very inefficient. There is a mathematical notation that describes algorithm efficiency named Big-O. This notation describes the growth rate of algorithms and check the time and space complexity.
We can describe bubble sort into Big-O as O(n²). Which means there are nested for loop, and both of the for loop will repeated from 0 to n.

This is the complexity chart of the Big-O and you can see the O(n²) has huge increase on its operation time when its data(elements) increases.

Well that’s all for today guys! Let’s practice on sorting array with the bubble sort next time. Thank you for reading my post, See ya!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store