0
0
PythonComparisonBeginner · 4 min read

Bubble Sort vs Selection Sort in Python: Key Differences and Code Comparison

Both 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.

FactorBubble SortSelection Sort
Algorithm TypeComparison-based, swapping adjacent elementsComparison-based, selecting minimum element
Number of SwapsMany swaps (can be up to O(n²))Fewer swaps (at most n swaps)
Time ComplexityO(n²) average and worst caseO(n²) average and worst case
Best Case PerformanceO(n) if already sorted (optimized version)O(n²) regardless of input
StabilityStable (does not change order of equal elements)Not stable (may change order)
Use CaseGood for nearly sorted dataSimple 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.

python
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)
Output
[11, 12, 22, 25, 34, 64, 90]
↔️

Selection Sort Equivalent

Here is the equivalent Selection sort implementation in Python for the same task.

python
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)
Output
[11, 12, 22, 25, 34, 64, 90]
🎯

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.

Key Takeaways

Bubble sort swaps adjacent elements and can stop early if the list is nearly sorted.
Selection sort selects the smallest element and swaps it with the current position, doing fewer swaps overall.
Bubble sort is stable; selection sort is not.
Both have O(n²) time complexity, but bubble sort can be faster on nearly sorted data.
Use bubble sort for stability and nearly sorted lists; use selection sort for fewer swaps when stability is not needed.