Bubble Sort
Code
C language code:
#include <stdio.h>
void bubble_sort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Code Explanation
- Initialization: The array
arris the array to be sorted, andnis the length of the array. - Main sorting loop:
- The outer
forloop controls the number of passes, from pass 0 to passn-2. (The last element is automatically sorted.) - The inner
forloop controls the number of comparisons per pass, from element 0 to elementn-i-2. - Compares adjacent elements; if the preceding element is greater than the following element, the two are swapped.
- The outer
- Result output: After sorting is complete, the sorted array is printed.
Example Run
Assuming the input array is {64, 34, 25, 12, 22, 11, 90}:
- Initial state:
{64, 34, 25, 12, 22, 11, 90} - First outer loop (i=0):
- Compare and swap:
{34, 64, 25, 12, 22, 11, 90} - Compare and swap:
{34, 25, 64, 12, 22, 11, 90} - Compare and swap:
{34, 25, 12, 64, 22, 11, 90} - Compare and swap:
{34, 25, 12, 22, 64, 11, 90} - Compare and swap:
{34, 25, 12, 22, 11, 64, 90} - Last pass:
{34, 25, 12, 22, 11, 64, 90}
- Compare and swap:
- Second outer loop (i=1):
- Compare and swap:
{25, 34, 12, 22, 11, 64, 90} - Compare and swap:
{25, 12, 34, 22, 11, 64, 90} - Compare and swap:
{25, 12, 22, 34, 11, 64, 90} - Compare and swap:
{25, 12, 22, 11, 34, 64, 90} - Last pass:
{25, 12, 22, 11, 34, 64, 90}
- Compare and swap:
- This repeats until the array is fully sorted.
Final result: {11, 12, 22, 25, 34, 64, 90}
Time Complexity Analysis
The time complexity of bubble sort depends on the initial order of the array.
- Best-case time complexity: When the input array is already sorted, only comparisons are needed with no swaps, giving a time complexity of .
- Worst-case time complexity: When the input array is in reverse order, bubble sort requires the maximum number of comparisons and swaps, giving a time complexity of .
- Average time complexity: In general cases, the time complexity of bubble sort is .
Space Complexity Analysis
Bubble sort is an in-place sorting algorithm that requires no additional auxiliary space, so the space complexity is . In this implementation, we only use a few extra variables (i, j, temp), so the space complexity remains at a constant level.
Advantages and Disadvantages
Advantages:
- Simple and easy to implement.
- Stable sorting algorithm; does not change the relative order of equal elements.
- In-place sorting; low space complexity.
Disadvantages:
- High time complexity, especially inefficient for large-scale data.
- Even when the array is partially sorted, complete comparison and swap operations are still required.
Applicable Scenarios
Bubble sort is suitable for the following scenarios:
- Sorting small datasets.
- When a simple and stable sorting algorithm is needed.
- Learning and teaching purposes, to understand basic sorting principles.
Summary
Bubble sort is a simple and intuitive sorting algorithm that works by repeatedly comparing and swapping adjacent elements, causing larger elements to gradually "bubble" to the end of the array. Although its worst-case time complexity is high, in specific scenarios, especially with small datasets, bubble sort remains an effective choice.