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.
Write a Java program to perform the bubble sort.
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
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.
//This is a java program to sort numbers using bubble sort
import java.util.Random;
public class Bubble_Sort
{
static int[] sort(int[] sequence)
{
// Bubble Sort
for (int i = 0; i < sequence.length; i++)
for (int j = 0; j < sequence.length - 1; j++)
if (sequence[j] > sequence[j + 1])
{
sequence[j] = sequence[j] + sequence[j + 1];
sequence[j + 1] = sequence[j] - sequence[j + 1];
sequence[j] = sequence[j] - sequence[j + 1];
}
return sequence;
}
static void printSequence(int[] sorted_sequence)
{
for (int i = 0; i < sorted_sequence.length; i++)
System.out.print(sorted_sequence[i] + " ");
}
public static void main(String args[])
{
System.out
.println("Sorting of randomly generated numbers using BUBBLE SORT");
Random random = new Random();
int N = 20;
int[] sequence = new int[N];
for (int i = 0; i < N; i++)
sequence[i] = Math.abs(random.nextInt(1000));
System.out.println("\nOriginal Sequence: ");
printSequence(sequence);
System.out.println("\nSorted Sequence: ");
printSequence(sort(sequence));
}
}
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.
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”.
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Information Technology Internship
- Buy Programming Books
- Practice Information Technology MCQs
- Practice Programming MCQs
- Apply for Java Internship