惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
D
Docker
T
The Blog of Author Tim Ferriss
罗磊的独立博客
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
J
Java Code Geeks
Jina AI
Jina AI
博客园 - 【当耐特】
C
Check Point Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Sorting in Python
Yasir Jafri · 2026-06-28 · via DEV Community

Yasir Jafri

Sorting is a fundamental operation in computer science and programming. Whether organizing data for analysis, building efficient algorithms, or enhancing application performance, sorting plays a critical role. Python provides robust tools for sorting and managing sorted data, making it a go-to language for developers. In this article, we’ll explore sorting in Python, covering everything from basics to advanced techniques.


1. Basic Sorting

Python offers two primary methods for sorting collections:

  • list.sort(): This method sorts a list in place and modifies the original list.
  • sorted(): This function returns a new sorted list without modifying the original.

Both methods use Timsort, a hybrid sorting algorithm derived from merge sort and insertion sort, ensuring efficiency for real-world data.

Examples:

# Using list.sort()
numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers)  # Output: [1, 2, 5, 9]

# Using sorted()
words = ["apple", "orange", "banana"]
sorted_words = sorted(words)
print(sorted_words)  # Output: ['apple', 'banana', 'orange']

Customization:

  • Key parameter: Sort elements based on custom logic.
# Sort by length
data = ["pear", "banana", "apple"]
print(sorted(data, key=len))  # Output: ['pear', 'apple', 'banana']

  • Reverse parameter: Sort in descending order.
print(sorted(numbers, reverse=True))  # Output: [9, 5, 2, 1]


2. Time Complexity of Sorting

Python’s Timsort algorithm has the following complexities:

  • Best case: O(n) for nearly sorted data.
  • Average case: O(n log n).
  • Worst case: O(n log n).

The algorithm’s efficiency stems from its ability to exploit runs (ordered subsequences) within the data and optimize merging operations.


3. Stable Sorting

A sorting algorithm is stable if it preserves the relative order of equal elements. Python’s sort() and sorted() are stable by design, which is useful in scenarios like multi-key sorting.

Example:

students = [("Alice", 90), ("Bob", 90), ("Eve", 85)]
# Sort by score, then by name
sorted_students = sorted(students, key=lambda x: (x[1], x[0]))
print(sorted_students)
# Output: [('Eve', 85), ('Alice', 90), ('Bob', 90)]


4. Sorting Data Structures

sortedcontainers Module:

  • SortedList, SortedDict, and SortedSet maintain data in sorted order dynamically.
  • Efficient for insertions, deletions, and lookups.
from sortedcontainers import SortedList
sl = SortedList([5, 1, 3])
sl.add(4)
print(sl)  # Output: [1, 3, 4, 5]

heapq Module:

  • Implements a min-heap for priority queues.
  • Useful for maintaining partial order efficiently.
import heapq
nums = [5, 2, 9, 1]
heapq.heapify(nums)
print(nums)  # Output: [1, 2, 9, 5]


5. Taking a Look at the bisect Module

The bisect module provides tools for binary search and maintaining order in sorted lists:

  • bisect.insort(): Insert while maintaining order.
  • bisect.bisect_left() and bisect.bisect_right(): Find positions for insertion.

Example:

import bisect
nums = [1, 3, 4, 10]
bisect.insort(nums, 5)
print(nums)  # Output: [1, 3, 4, 5, 10]


6. Sorting with Multiprocessing

Sorting large datasets can benefit from parallel processing. Python’s multiprocessing module can be used to distribute sorting workloads across multiple processors.

Example:

from multiprocessing import Pool

def sort_chunk(chunk):
    return sorted(chunk)

data = [5, 9, 1, 7, 3, 2, 8, 4]
chunks = [data[:4], data[4:]]

with Pool() as pool:
    sorted_chunks = pool.map(sort_chunk, chunks)

# Merge sorted chunks
result = sorted(sum(sorted_chunks, []))
print(result)  # Output: [1, 2, 3, 4, 5, 7, 8, 9]


7. Generator-Based Sorting

For memory-efficient sorting, generators can process data lazily. Use heapq.merge() to sort multiple sorted iterables without loading them entirely into memory.

Example:

import heapq

data1 = iter([1, 4, 7])
data2 = iter([2, 5, 8])

merged = heapq.merge(data1, data2)
print(list(merged))  # Output: [1, 2, 4, 5, 7, 8]


8. External Sorting

For datasets too large to fit into memory, external sorting divides data into manageable chunks, sorts each chunk, and merges them.

Example:

  • Split large file into smaller sorted chunks.
  • Use heapq.merge() for final merging.

9. Use Cases

Real-World Applications:

  1. Event Scheduling: Sorting events by timestamps.
  2. Leaderboards: Dynamic ranking systems.
  3. Financial Analysis: Sorting stock data for trend analysis.

10. Conclusion

Sorting is more than an academic exercise; it’s a cornerstone of efficient programming. Python provides powerful tools and libraries to handle sorting for various scenarios, from in-memory operations to large-scale data processing. Understanding these techniques can elevate your problem-solving skills and optimize your applications.