Sorting algorithms for beginners
Posted: Jan. 18, 2026
Sorting algorithms are one of the first major topics most people learn when studying programming or computer science. They teach you how to think logically, how algorithms affect performance, and why some solutions are better than others.
This beginner-friendly guide explains:
- What each sorting algorithm does
- A simple Python example for each
- When you should (and shouldn’t) use it
Don’t worry if some of these feel slow or inefficient — that’s part of the learning process.
What Is a Sorting Algorithm?
A sorting algorithm is just a step-by-step method for putting items in order.
Example:
[5, 2, 9, 1]Sorted:
[1, 2, 5, 9]Different algorithms do this in different ways, and some are much faster than others.
Bubble Sort
What it does: Repeatedly swaps neighboring elements if they’re out of order.
How it thinks: “Keep bubbling the biggest number to the end.”
def bubble_sort(arr):
for i in range(len(arr)):
for j in range(0, len(arr) - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]Use case: Learning how sorting works (not used in real apps)
Selection Sort
What it does: Finds the smallest number and puts it in the correct position.
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]Use case: Understanding how selection-based logic works
Insertion Sort
What it does: Builds the sorted list one item at a time.
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = keyUse case: Small lists or nearly sorted data
Merge Sort
What it does: Splits the list in half, sorts each half, then merges them.
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1Use case: Large datasets where reliability matters
Quick Sort
What it does: Picks a pivot and places smaller values on one side and larger ones on the other.
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)Use case: Fast general-purpose sorting
Heap Sort
What it does: Uses a special tree structure called a heap.
import heapq
def heap_sort(arr):
heapq.heapify(arr)
return [heapq.heappop(arr) for _ in range(len(arr))]Use case: When memory efficiency matters
Counting Sort
What it does: Counts how many times each value appears.
def counting_sort(arr):
max_val = max(arr)
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
return sorted_arrUse case: Small-range integers (like scores or ages)
Radix Sort
What it does: Sorts numbers digit by digit.
def radix_sort(arr):
max_num = max(arr)
exp = 1
while max_num // exp > 0:
buckets = [[] for _ in range(10)]
for num in arr:
buckets[(num // exp) % 10].append(num)
arr = [num for bucket in buckets for num in bucket]
exp *= 10
return arrUse case: Large numbers or IDs
Bucket Sort
What it does: Spreads values into buckets, then sorts each bucket.
def bucket_sort(arr):
buckets = [[] for _ in range(len(arr))]
for num in arr:
index = int(num * len(arr))
buckets[index].append(num)
return [num for bucket in buckets for num in sorted(bucket)]Use case: Uniformly distributed decimal values
Which Sorting Algorithm Should You Use?
If you’re a beginner:
- Bubble Sort
- Selection Sort
- Insertion Sort
If the data is small or almost sorted:
- Insertion Sort
- Shell Sort
If the data is large:
- Merge Sort
- Quick Sort
If you need guaranteed performance:
- Heap Sort
- Intro Sort
If the data has special rules:
- Counting Sort (small integers)
- Radix Sort (digits)
- Bucket Sort (uniform distribution)
In real-world programming:
- Use built-in sorting (
sorted()in Python) - These use optimized hybrids like Tim Sort
sorted_list = sorted([5, 2, 9, 1])You don’t need to memorize every algorithm. Focus on:
- Understanding how they work
- Knowing why some are faster
- Learning when to use each one
Once sorting “clicks,” many other topics like searching, recursion, and data structures become much easier.
Read more!

What is OpenClaw?
Artificial intelligence is rapidly evolving from simple chatbots into autonomous systems that can…
Topic: Tech

Why Promises Can't Be Stopped in JavaScript
JavaScript developers often ask a simple but surprisingly deep question: why can’t you stop a…
Topic: Tech

Getting Started with MCP
The term MCP can mean different things depending on context, but in today’s tech landscape it most…
Topic: Tech

How to Use Old RAM in Light of Recent Price Hikes
With RAM prices climbing again due to supply chain issues, increased demand, and new-generation…
Topic: Tech

Why YouTube is an Ideal Resource for Learning Japanese
Learning Japanese can feel like a daunting challenge — three writing systems, unfamiliar grammar…
Topic: Tech

How to write scalable and excellent code
Writing code that works is easy. Writing scalable, maintainable, and excellent code is the real…
Topic: Tech
@alxlynnhd