Bubble Sort vs Selection Sort in Python: Key Differences and Code Comparison
Bubble sort and Selection sort are simple sorting algorithms used in Python to order lists. Bubble sort repeatedly swaps adjacent elements if they are in the wrong order, while Selection sort selects the smallest element and swaps it with the current position. Bubble sort can be slightly slower due to more swaps, but both have similar time complexity of O(n²).Quick Comparison
Here is a quick side-by-side comparison of Bubble sort and Selection sort based on key factors.
| Factor | Bubble Sort | Selection Sort |
|---|---|---|
| Algorithm Type | Comparison-based, swapping adjacent elements | Comparison-based, selecting minimum element |
| Number of Swaps | Many swaps (can be up to O(n²)) | Fewer swaps (at most n swaps) |
| Time Complexity | O(n²) average and worst case | O(n²) average and worst case |
| Best Case Performance | O(n) if already sorted (optimized version) | O(n²) regardless of input |
| Stability | Stable (does not change order of equal elements) | Not stable (may change order) |
| Use Case | Good for nearly sorted data | Simple but less efficient for large data |
Key Differences
Bubble sort works by repeatedly stepping through the list, comparing adjacent pairs, and swapping them if they are in the wrong order. This process repeats until no swaps are needed, which means the list is sorted. It can stop early if the list becomes sorted before completing all passes, making it efficient for nearly sorted data.
In contrast, Selection sort divides the list into a sorted and unsorted part. It repeatedly finds the smallest element from the unsorted part and swaps it with the first unsorted element. This means it does fewer swaps overall but always performs the same number of comparisons, regardless of the initial order.
Bubble sort is stable because it only swaps adjacent elements, preserving the order of equal elements. Selection sort is not stable because it can swap non-adjacent elements, potentially changing the order of equal items. Both have a time complexity of O(n²), but bubble sort can be faster on nearly sorted lists due to early stopping.
Code Comparison
Here is how you implement Bubble sort in Python to sort a list of numbers.
def bubble_sort(arr): n = len(arr) for i in range(n): swapped = False for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] swapped = True if not swapped: break sample_list = [64, 34, 25, 12, 22, 11, 90] bubble_sort(sample_list) print(sample_list)
Selection Sort Equivalent
Here is the equivalent Selection sort implementation in Python for the same task.
def selection_sort(arr): n = len(arr) for i in range(n): min_idx = i for j in range(i + 1, n): if arr[j] < arr[min_idx]: min_idx = j arr[i], arr[min_idx] = arr[min_idx], arr[i] sample_list = [64, 34, 25, 12, 22, 11, 90] selection_sort(sample_list) print(sample_list)
When to Use Which
Choose Bubble sort when you expect the list to be nearly sorted already, as it can finish early and save time. It is also preferable when stability is important, meaning you want to keep the original order of equal elements.
Choose Selection sort when you want a simple algorithm with fewer swaps, which can be useful if swapping is costly. However, it is less efficient on large or nearly sorted data and is not stable, so use it only when these factors are acceptable.