Bubble Sort
Bubble sort is the simplest sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. Larger elements "bubble up" to the end. Time complexity: O(n²) average and worst, O(n) best (already sorted).
Java implementation:
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
boolean swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
swapped = true;
}
}
if (!swapped) break;
}
}
Two Minute Drill
- Bubble sort repeatedly swaps adjacent out-of-order elements.
- Worst & average O(n²), best O(n) (already sorted).
- Stable, in-place.
- Educational but inefficient for large data.
Need more clarification?
Drop us an email at career@quipoinfotech.com
