All TalkersCode Topics

Follow TalkersCode On Social Media

Bubble Sort In Java

Last Updated : Jan 1, 2023

Bubble Sort In Java

In this article we will show you the solution of bubble sort in java, as usual you need to imported package, in case you using eclipse software then itself that will imports.

After we defined main class with two methods, BubSort() for sorting block of process and static main method.

There we defining array value and creating object for main class to call the BubSort() method with passed array as parameter. Finally printed result of sorted array values.

Step By Step Guide On Bubble Sort In Java :-

In BubSort() method definition, we iterating array 'arr1' values with two block of for loop.

To compare current value with next value by if statement. If condition true, then we swapping their position each other otherwise checking next two values. Likewise, the array sorted and stored on original array variable arr1.

At main method initialized array arr1 with some values and created object for main class to access sorting method and we displaying sorted result on terminal.

//Bubble Sort Example
package Sortings;
class Prgm {
 void BubSort(int arr[]){
  int l=arr.length;
  int i,j,t;
  for (i = 0; i < l; i++) {
   for (j = 1; j < (l - i); j++) {
    if (arr[j-1] > arr[j])
     t = arr[j-1];
     arr[j-1] = arr[j];
     arr[j] = t;
 public static void main(String []args) {
  int arr1[]={12,9,2,1};
  Prgm ob=new Prgm();
  System.out.println("Sorted Array Values");
  for (int c = 0; c < arr1.length; c++)
  1. The ‘Package Sortings’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. First we defined main class 'Prgm', then we defined BubSort() method with argument array arr. We find out the array length by binding 'lenght' with array variable 'arr' that returns array length stored on avriable 'l'.
  3. To process we defined some integer variables 'i,j,t', these are majorly helps to sort out the array values. In first for loop we initialized variable i with value '0' and provide condition statement that iterates the array values until reaches array length.
  4. The second for loop defined to points next position of current value by specifying 'j=1', which will work until the j value since have to greater than 'l-i'.
  5. After passing two loops specification, we checking condition through if statement and comparing very first with second value. When first value greater than second one doing swaping index position.
  6. Within public static void main() method. There declared integer array can do the swaps. In the main method, declared and initialized array arr1 with values '{12,9,2,1}'.
  7. Then created object 'ob' for main class and called BubSort() method, at last we printed array values with the help of for loop.
  8. The ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  9. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  10. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to do bubble sorting in java. To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Prgm‘line for gets the output.

Then you can see text 'Sorted Array Values' with '1 2 9 12' sorted values of output that might be vary if you change the defined array values.

On eclipse just press of run button then you will get output inside terminal.

I hope this article on bubble sort in java helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials