Bubble Sort Java

Hi guys!! Welcome to flower brackets blog. In this post we will learn bubble sort java.

bubble sort java

Bubble sort algorithm in java program is considered as the simplest sorting algorithm for the fact that array is traversed from first number to last number.

Also Read – Insertion sort in java

Java bubble sorting is considered as not so efficient sorting algorithm because bubble sort in java uses nested loops. Hence useful for small data sets.

Basically in the below bubble sort java example numbers are swapped if the first number is greater than the next number.

So, let’s see how to write a bubble sort program in java,

Example: bubble sort java

Let’s see bubble sort java algorithm using random numbers,

import java.util.Random;

public class BubbleSort
{
   static int[] sort(int[] inOrder)
   {
      for(int a = 0;a < inOrder.length;a++)
         for(int b = 0;b < inOrder.length - 1;b++)

            if(inOrder[b] > inOrder[b + 1])
            {
               inOrder[b] = inOrder[b] + inOrder[b + 1];
               inOrder[b + 1] = inOrder[b] - inOrder[b + 1];
               inOrder[b] = inOrder[b] - inOrder[b + 1];
            }
            return inOrder;
   }

   static void printInOrder(int[] properOrder)
   {
      for(int x = 0;x < properOrder.length;x++)
         System.out.println(properOrder[x] + " ");
   }

   public static void main(String[] args)
   {
      System.out.println("Randomly sorting numbers using Bubble Sort ");

      Random rd = new Random();
      int r = 10;
      int[] order = new int[r];
      for(int x = 0;x < r;x++)
         order[x] = Math.abs(rd.nextInt(1000));
      System.out.println("Original Sequence: ");
      printInOrder(order);
      System.out.println("Sorted Sequence: ");
      printInOrder(sort(order));
   }
}

Output:

bubble sort program in java


Bubble sort algorithm

Here’s the bubble sort implementation java,

import java.util.Scanner;

public class BubbleSortDemo
{
   public static void main(String[] args)
   {
      int numbers, a, b, swap;
      Scanner sc = new Scanner(System.in);
          
      System.out.println("Please enter number of integers to sort : ");
      numbers = sc.nextInt();

      int array[] = new int[numbers];        
      System.out.println("Please enter " + numbers + " integers.");

      for(a = 0; a < numbers; a++)
         array[a] = sc.nextInt();

      for(a = 0; a < ( numbers - 1 ); a++)
      {
         for(b = 0; b < numbers - a - 1; b++)
         {
            if(array[b] > array[b + 1])
            { 
               swap = array[b];
               array[b] = array[b + 1];
               array[b + 1] = swap;
            }
         }
      }
    
      System.out.println("After Bubble Sort - ");
      for(a = 0; a < numbers; a++)          
         System.out.println(array[a]);
      sc.close();
   }
}

Output:

Please enter number of integers to sort : 5
Please enter 5 integers.
30
50
10
40
20
After Bubble Sort –
10
20
30
40
50


Using method

Let’s see bubble sort java implementation using method,

public class BubbleSortUsingMethod
{
   static void bubbleSortMethod(int[] sortArray)
   {
      int y = sortArray.length;
      int temp = 0;
      for(int a = 0; a < y; a++)
      {
         for(int b = 1; b < (y - a); b++)
         {
            if(sortArray[b - 1] > sortArray[b])
            {
               temp = sortArray[b-1];                            
               sortArray[b - 1] = sortArray[b];                                sortArray[b] = temp;
            }
         }
      }
   }
  
   public static void main(String[] args)
   {
      int number[] ={25,255,77,5,26,54,6};          
      System.out.println("Before Bubble Sort - ");           
      for(int a = 0; a < number.length; a++)
      {
         System.out.print(number[a] + " ");
      }
      System.out.println();

      bubbleSortMethod(number);

      System.out.println("After Bubble Sort - ");             
      for(int a = 0; a < number.length; a++)
      {
         System.out.print(number[a] + " ");
      }
   }
}

Output:

Before Bubble Sort – 25 255 77 5 26 54 6
After Bubble Sort – 5 6 25 26 54 77 255


conclusion

So this is all about bubble sort program in java. I hope you have understood the concept on how to bubble sort in java.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts