๐ Introduction
Sorting is a fundamental operation in programming โ and mastering sorting algorithms helps you understand time complexity, algorithm design, and even how Pythonโs built-in tools work under the hood.
In this post, weโll explore:
โ Basic and advanced sorting algorithms
โ Python implementations with step-by-step logic
โ When to use each sorting method
โ Built-in sorting (sorted(), list.sort()) and how it's optimized
โ Time & space complexity comparison
๐ 1๏ธโฃ Why Sorting Matters
Sorting is used everywhere:
1. Organizing data before searching (e.g., binary search) 2. Making duplicate detection easier 3. Efficient comparisons, merging, and filtering 4. Preprocessing before solving harder problems (e.g., greedy, DP)
๐ก 2๏ธโฃ Built-in Sorting in Python
๐น sorted() โ returns a new sorted list
nums = [5, 2, 9, 1] sorted_nums = sorted(nums)
๐น list.sort() โ sorts in place
nums.sort()
โ Both use Timsort, a hybrid of merge and insertion sort
๐ Time complexity: O(n log n) (worst-case)
๐งฎ 3๏ธโฃ Selection Sort โ Simple but Inefficient
def selection_sort(arr): n = len(arr) for i in range(n): min_i = i for j in range(i + 1, n): if arr[j] < arr[min_i]: min_i = j arr[i], arr[min_i] = arr[min_i], arr[i] return arr
๐ฆ Time: O(nยฒ)
โ Easy to understand
โ Not suitable for large data
๐ 4๏ธโฃ Bubble Sort โ Compare and Swap
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr
๐ฆ Time: O(nยฒ)
โ Good for teaching purposes
โ Inefficient for real-world use
๐ง 5๏ธโฃ Insertion Sort โ Builds Sorted Portion
def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return arr
๐ฆ Time: O(nยฒ), but efficient for small or nearly sorted arrays
โ Used in Timsort for small runs
๐งฌ 6๏ธโฃ Merge Sort โ Divide and Conquer
def merge_sort(arr): if len(arr) <= 1: return arr mid = len(arr) // 2 left = merge_sort(arr[:mid]) right = merge_sort(arr[mid:]) return merge(left, right) def merge(left, right): result = [] l = r = 0 while l < len(left) and r < len(right): if left[l] < right[r]: result.append(left[l]) l += 1 else: result.append(right[r]) r += 1 result.extend(left[l:]) result.extend(right[r:]) return result
๐ฆ Time: O(n log n)
๐ง Space: O(n)
โ Stable, consistent
โ Great for linked lists or external sorting
โก 7๏ธโฃ Quick Sort โ Efficient and Elegant
def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[0] less = [x for x in arr[1:] if x <= pivot] more = [x for x in arr[1:] if x > pivot] return quick_sort(less) + [pivot] + quick_sort(more)
๐ฆ Time:
Average: O(n log n) Worst: O(nยฒ) (rare, with bad pivots)
โ Fast and in-place (with extra work)
โ Unstable
๐ 8๏ธโฃ Comparison Table
Algorithm | Time Complexity | Space | Stable? Use When |
---|---|---|---|
Selection Sort | O(nยฒ) | O(1) | โ |
Bubble Sort | O(nยฒ) | O(1) | โ |
Insertion Sort | O(nยฒ), Best: O(n) | O(1) | โ |
Merge Sort | O(n log n) | O(n) | โ |
Quick Sort | O(n log n), Worst O(nยฒ) | O(log n) | โ |
Timsort (Python) | O(n log n) | O(n) | โ |
๐ง Real-World Tips
โ For large unsorted datasets โ Timsort (default)
โ For small arrays or known order โ Insertion Sort
โ For linked lists โ Merge Sort
โ For interview practice โ Quick Sort is a favorite
๐งช Practice Problems
Problem | Technique |
---|---|
Sort Colors (Dutch Flag) | Custom in-place sort |
Merge Intervals | Sorting + merging |
Top K Frequent Elements | Sorting by frequency |
Largest Number | Custom sort with key |
Kth Largest Element | Quickselect |
โ Summary
โ๏ธ Sorting algorithms vary in efficiency, stability, and use cases
โ๏ธ Learn basic ones for intuition, master advanced ones for performance
โ๏ธ Python uses Timsort, a hybrid of merge and insertion sort
โ๏ธ In interviews, know when and why to choose a specific algorithm
๐ Coming Up Next:
๐ Part 7: Searching Algorithms โ Binary Search and Its Powerful Variants
Weโll cover:
1. Linear vs Binary Search Binary search templates
Search in rotated array
Lower/upper bounds
Real-world applications
Top comments (0)