Java Program to Implement Bubble Sort

«
»
This is a java program to sort the numbers using the Bubble Sort Technique. The 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. The time complexity of the algorithm is O(n^2).

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.

  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. }

Output:

$ javac Binary_Counting_Subsets.java
$ java Binary_Counting_Subsets
 
$ 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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

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.