Fundamentals 6 min read

Master Python Sorting: Bubble & Selection Sort Explained and Optimized

This article introduces internal and external sorting concepts, then walks through Python implementations of bubble sort and selection sort, detailing basic algorithms, optimization techniques such as early‑exit flags and binary selection, and analyzes their time complexities and trade‑offs.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Sorting: Bubble & Selection Sort Explained and Optimized

Sorting Concepts

Internal sorting processes data stored entirely in memory, while external sorting accesses external storage. Internal sorting is the foundation and can be classified by principle (insertion, exchange, selection, merge) or by complexity (simple vs advanced). Simple sorts include bubble sort, simple selection sort, and direct insertion sort.

Algorithm evaluation criteria are computational effort (number of comparisons and moves) and additional storage required.

Algorithm classification
Algorithm classification

Simple Sort: Bubble Sort Python Implementation and Optimization

Basic implementation follows the classic bubble sort algorithm, repeatedly swapping adjacent out‑of‑order elements.

Bubble sort principle diagram 1
Bubble sort principle diagram 1
Bubble sort principle diagram 2
Bubble sort principle diagram 2

Basic Implementation

Bubble sort code
Bubble sort code

Optimized Implementation

Introduce a flag that records whether any swap occurred during a pass; if no swaps happen, the list is already sorted and the algorithm can terminate early.

Optimized bubble sort code
Optimized bubble sort code

Summary

Bubble sort repeatedly compares adjacent elements. The optimization with a swap‑flag can end the algorithm early when a pass makes no exchanges. Worst‑case complexity is O(n²) with n(n‑1)/2 comparisons; best case (already sorted) requires only n‑1 passes.

Simple Sort: Selection Sort Python Implementation and Optimization

The core of selection sort is to find the extreme (minimum or maximum) value in each pass and place it at the appropriate end, repeating until the list is sorted.

Selection sort principle diagram
Selection sort principle diagram

Basic Implementation

Selection sort code
Selection sort code

Optimized Implementation – Binary Selection Sort

Each pass selects both the maximum and minimum values, reducing the number of iterations by half.

Binary selection sort code
Binary selection sort code

Equal‑Value Optimization

If the maximum and minimum found in a pass are equal, the remaining elements are identical and the algorithm can terminate early.

Equal value optimization
Equal value optimization

Advanced Equal‑Value Optimization

Example: [1,1,1,1,1,1,1,1,2]; the minimum index is -2 and maximum index 8, causing unnecessary swaps of identical values. Adding a check avoids these redundant operations.

Advanced equal value optimization
Advanced equal value optimization

Summary

Selection sort also requires multiple passes, discovering an extreme each round. It cannot know early if the list is sorted, but it can detect when extremes are already in place. Comparisons total n(n‑1)/2, time complexity O(n²). Fewer swaps than bubble sort make it slightly more efficient.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Algorithm Optimizationsortingbubble sortselection sortexternal sortinginternal sorting
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.