In this **Bubble Sort In Java** tutorial, you will learn a lot about bubble sort algorithms and how they are implemented. This tutorial could be one of your fundamental learning guides to your journey as a Java developer.

## Table of contents

- What Is Bubble Sort In Java?
- How Bubble Sort Works?
- Algorithm Of Bubble Sort
- Bubble Sort Sample Code
- Sample Java Code Using Scanner.
- Bubble Sort Example Output Using Scanner
- Sample Java Code Using Array.
- Bubble Sort Example Output Using Array
- Test Your Bubble Sort Code
- Conclusion
- Related Sorting Algorithm in Java

**What Is Bubble Sort In Java?**

The **Java Program To Bubble Sort** is a basic sorting algorithm that compares neighboring array elements and swaps them if the rightmost element is smaller than the leftmost. It’s an in-place sorting method, which means it doesn’t take up any extra space and just modifies the array itself.

Bubble sort is a simple algorithm that sorts a list by letting the lowest or highest values rise to the top, just like how bubbles rise from the bottom of a glass. The algorithm goes through a list and compares the values next to each other. If they are not in the right order, they are switched.

**How Bubble Sort Works?**

This is how Bubble Sort simple works:

**First Iteration (Compare and Swap)**

- Compare the first and second elements starting with the first index.
- The first and second elements are swapped if the first is bigger than the second.
- The first and second elements are swapped if the first is bigger than the second.
- The method is repeated until the last element.

**Iteration Continues**

For the remaining iterations, the same procedure is followed.

The largest element among the unsorted items is placed at the conclusion of each iteration.

As you can see in the image, the iteration is completely finished. From the left side of all the elements, the number of elements is from lowest to highest (4, 6, 10, 12, 45, 89), which means that the bubble sorted completely and sorted perfectly.

**Algorithm Of Bubble Sort**

bubbleSort(array) for i <- 1 to indexOfLastUnsortedElement – 1 if leftElement > rightElement swap leftElement and rightElement end bubbleSort

**Bubble Sort Sample Code**

You can run this code in your code editor if you already downloaded it to your laptop or PC, but if you don’t, you can download it. Here’s the link. NetBeans IDE, but if you wish to run this code online, we also have an online compiler in Java. Here’s the link. Java Online Compiler

We also put the Java online compiler below. Just scroll down so that you can easily test or run the bubble sort source code in order to learn more about this sort algorithm.

**Sample Java Code Using Scanner.**

A **Scanner** is a class in the Java Utilities package that is used to get the input of primitive types like int, double, etc., and strings. It is the easiest way to read input into a Java program.

import java.util.Scanner; public class Bubble_Sort_Java { public static void Sort(int a[]) { int n = a.length, i, j, p, temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (a[j + 1] < a[j]) { temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; } } } } public static void printarray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } /** * @param args the command line arguments */ public static void main(String[] args) { int n, res, i; Scanner s = new Scanner(System.in); System.out.print("Enter number of elements in the array:"); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter " + n + " elements "); for (i = 0; i < n; i++) { a[i] = s.nextInt(); } System.out.println("elements in array "); printarray(a); Sort(a); System.out.println("\nelements after sorting"); printarray(a); } }

If you wish to use our online compiler to run this Bubble Sort source code, simply copy all of the code and paste it there. Here’s the URL to the page. The Java Online Compiler is a tool that allows you to compile Java code online

**Bubble Sort Example Output Using Scanner**

Enter number of elements in the array:Enter 6 elements

elements in array

4 10 45 12 6 89

elements after sorting

4 6 10 12 45 89

**Test Your Bubble Sort Code**

**Sample Java Code Using Array.**

An **Array** is a container object that holds a fixed number of values of the same type. The length of an array is set when the array is made. After it is made, its length is fixed.

public class Bubble_Sort_Java { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } } public static void main(String[] args) { int arr[] ={4,10,45,12,6,89}; System.out.println("Array Before Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println(); bubbleSort(arr);//sorting array elements using bubble sort System.out.println("Array After Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } } }

If you wish to use our online compiler to run this Bubble Sort source code, simply copy all of the code and paste it there. Here’s the URL to the page. The Java Online Compiler is a tool that allows you to compile Java code online

**Bubble Sort Example Output Using Array**

Array Before Bubble Sort

4 10 45 12 6 89

Array After Bubble Sort

4 6 10 12 45 89

**Test Your Bubble Sort Code**

Anyway, if you want to level up your programming knowledge, especially Java, try this new article I’ve made for you Best Java Projects With Source Code For Beginners Free Download 2021.

**Conclusion**

So far, we’ve already discussed the Bubble Sort algorithm for sorting in Java. We also looked at the algorithm and a detailed example of how to use the Bubble Sort Technique to sort an array. Then we put the Java program to work on the Bubble Sort. I hope this Bubble Sort tutorial helps you a lot in learning Java development.

**Related Sorting Algorithm in Java**

**Quick Sort**

**Selection Sort**