Data Structure and Algorithm
Topperworld.in
Bubble Sort
Bubble Sort is another 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. This process is
repeated for the entire list until no more swaps are needed, indicating that
the list is sorted.
Algorithm:
Bubble_Sort(int a[], n)
{
int swapped, i, j;
for (i=0; i<n; i++)
{
swapped = 0;
for (j=0; j<n-i-1; j++)
{
if (a[j] > a[j+1])
{
swap (a[j], a[j+1]);
swapped = 1;
}
}
if (swapped == 0)
break;
}
}
1. Start by comparing the first two elements of the array.
2. If the first element is greater than the second element, swap them.
3. Move to the next pair of elements and repeat step 2.
©TopperWorld
Data Structure and Algorithm
4. Continue this process until you reach the end of the array. By this
point, the largest element will have "bubbled up" to the end of the
array.
5. Repeat steps 1-4 for the remaining unsorted portion of the array.
6. Continue these iterations until no more swaps are needed, indicating
that the array is sorted.
➔Working of Bubble Sort
Suppose we are trying to sort the elements in ascending order.
1. First Iteration (Compare and Swap)
1. Starting from the first index, compare the first and the second
elements.
2. If the first element is greater than the second element, they are
swapped.
3. Now, compare the second and the third elements. Swap them if they
are not in order.
4. The above process goes on until the last element.
©TopperWorld
Data Structure and Algorithm
Compare the Adjacent Elements
2. Remaining Iteration
The same process goes on for the remaining iterations.
After each iteration, the largest element among the unsorted elements is
placed at the end.
©TopperWorld
Data Structure and Algorithm
Put the largest element at the end
In each iteration, the comparison takes place up to the last unsorted
element.
Compare the adjacent elements
©TopperWorld
Data Structure and Algorithm
The array is sorted when all the unsorted elements are placed at their
correct positions.
The array is sorted if all elements are kept in the right order
➔Java Implementation:
Here's the Java code for implementing Bubble Sort:
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
©TopperWorld
Data Structure and Algorithm
swapped = true;
}
}
// If no two elements were swapped in the inner loop,
the array is already sorted
if (!swapped) {
break;
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
OUTPUT
Sorted array:
11 12 22 25 34 64 90
©TopperWorld
Data Structure and Algorithm
➔Complexity Analysis:
• Time Complexity: Bubble Sort has a time complexity of O(n^2),
where n is the number of elements in the array. In each iteration, it
compares and swaps adjacent elements, and it repeats this process
for n iterations in the worst case.
• Space Complexity: Bubble Sort has a space complexity of O(1), as
it only requires a constant amount of additional memory to perform
the swaps and keep track of indices.
Bubble Sort is straightforward to implement, but its efficiency decreases for
larger datasets. Other sorting algorithms like Merge Sort or Quick Sort are
generally preferred for larger arrays due to their better average and worst-
case time complexities.
©TopperWorld