Bubble sort is a straightforward sorting algorithm that operates by comparing neighboring elements and exchanging them if they are out of order.The algorithm gets its name from the way that the larger elements “bubble” to the top of the array as the smaller elements are moved down. Bubble sort is one of the simplest sorting algorithms, but it is also one of the least efficient. It has a time complexity of O(n^2), which means that the running time of the algorithm is proportional to the square of the number of elements in the array. Despite its inefficiency, bubble sort is still a useful algorithm for sorting small arrays. It is also a good algorithm to use for learning about sorting algorithms.
Table of Contents
How Bubble Sort Works
Here’s the basic idea behind the bubble sort algorithm:
- Start with an unsorted array of elements.
- Compare the first element with the second element. Whenever the first element is found to be greater than the second element, they are swapped with each other.
- Move to the next pair of elements (second and third) and repeat the comparison and swap if necessary.
- Continue this process, comparing and swapping adjacent elements, until you reach the end of the array. At this point, the largest element will be in its correct position at the end of the array.
- Repeat steps 2-4 for the remaining unsorted elements, but ignore the already sorted elements at the end of the array.
- Keep repeating this process until the entire array is sorted.
Implementation of Bubble Sort in Java
public class BubbleSort {
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n – 1; i++)
{ for (int j = 0; j < n – i – 1; j++)
{ if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(array);
System.out.println("Sorted array:");
for (int i : array) {
System.out.print(i + " ");
}
}
}
Advantages and Disadvantages of Bubble Sort
Advantages:
- Bubble sort is a simple algorithm to understand and implement.
- Bubble sort is stable, which means that it does not change the relative order of elements that are equal.
Disadvantages:
- Bubble sort is inefficient, with a time complexity of O(n^2).
- Bubble sort is not very effective for sorting large arrays.
Arrange names of 10 students in ascending order using bubble sort
public class BubbleSort {
public static void main(String[] args) {
String[] names = {“John”, “Emma”, “David”, “Sophia”, “Daniel”, “Olivia”, “Michael”, “Emily”, “James”, “Isabella”};
int n = names.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
System.out.println("Sorted names:");
for (String name : names) {
System.out.println(name);
}
}
}
Conclusion
Bubble sort is a simple sorting algorithm that is useful for learning about sorting algorithms. However, it is not very efficient and should not be used for sorting large arrays.