Java Program to Implement Bubble Sort

«
»
What is Bubble Sort?

Bubble Sort in Java is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items, and swapping them if they are in the wrong order. Bubble sort technique is used to sort an array of values in increasing or decreasing order.

In simple words, bubble sort algorithm goes with the name, generally used to sort numbers in the ascending order. The smallest numbers bubbles up at each iteration of the sort.

Problem Description

Write a Java program to perform the bubble sort.

Bubble Sort Algorithm

1. In Bubble sort algorithm we compare the first two elements of an array and swap them if required.
2. If we want to sort the elements of array in ascending order and if the first element is greater than second then, we need to swap the elements.
3. If the first element is smaller than second, we don’t need to swap the elements. This process go on until last and second last element is compared and swapped.

Bubble Sort Example:

If we have the array as {50,20,60,90,40}
and we apply bubble sort to sort the array,
then the resultant array after each iteration will be as follows:

               Original array: {50, 20, 60, 90, 40}

Array after first iteration          20  ->   50   ->   60   ->   40   ->   90
Array after second iteration         20  ->   50   ->   40   ->   60   ->   90
Array after third iteration          20  ->   40   ->   50   ->   60   ->   90
Array after fourth iteration         20  ->   40   ->   50   ->   60   ->   90
           
               Sorted array is  20  40  50  60 90
Program/Source Code

Here is the source code of the Java Program to Implement Bubble Sort. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
  1. //This is a java program to sort numbers using bubble sort
  2. import java.util.Random;
  3.  
  4. public class Bubble_Sort 
  5. {
  6.     static int[] sort(int[] sequence) 
  7.     {
  8.         // Bubble Sort
  9.         for (int i = 0; i < sequence.length; i++)
  10.             for (int j = 0; j < sequence.length - 1; j++)
  11.                 if (sequence[j] > sequence[j + 1]) 
  12.                 {
  13.                     sequence[j] = sequence[j] + sequence[j + 1];
  14.                     sequence[j + 1] = sequence[j] - sequence[j + 1];
  15.                     sequence[j] = sequence[j] - sequence[j + 1];
  16.                 }
  17.         return sequence;
  18.     }
  19.  
  20.     static void printSequence(int[] sorted_sequence) 
  21.     {
  22.         for (int i = 0; i < sorted_sequence.length; i++)
  23.             System.out.print(sorted_sequence[i] + " ");
  24.     }
  25.  
  26.     public static void main(String args[]) 
  27.     {
  28.         System.out
  29.                 .println("Sorting of randomly generated numbers using BUBBLE SORT");
  30.         Random random = new Random();
  31.         int N = 20;
  32.         int[] sequence = new int[N];
  33.  
  34.         for (int i = 0; i < N; i++)
  35.             sequence[i] = Math.abs(random.nextInt(1000));
  36.  
  37.         System.out.println("\nOriginal Sequence: ");
  38.         printSequence(sequence);
  39.  
  40.         System.out.println("\nSorted Sequence: ");
  41.         printSequence(sort(sequence));
  42.     }
  43. }

Time Complexity: O(n2)
The time complexity of the buble sort algorithm is O(n2).

Space Complexity: O(1)
Space completixty of bubble sort program is constant because no extra space is required.

Check this: Java Books | BCA MCQs
Program Output

In this case, we are sorting of randomly generated numbers using bubble sort in ascending order.

$ javac Bubble_Sort.java
$ java Bubble_Sort
 
Sorting of randomly generated numbers using BUBBLE SORT
 
Original Sequence: 
307 677 574 88 325 851 676 357 172 932 166 450 60 538 964 987 706 690 919 518 
Sorted Sequence: 
60 88 166 172 307 325 357 450 518 538 574 676 677 690 706 851 919 932 964 987

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.