The short answer
Merge sort and quicksort are the two best-known sorting algorithms that use recursion. Both split the list into smaller parts, sort those parts by calling themselves, and combine the results. Many other common sorts, including bubble sort, insertion sort and selection sort, are usually written with loops. Heapsort is also usually written with loops, although its helper step can be recursive. Python’s built-in sort is loop-driven too.
Recursive and loop-based sorts at a glance
| Algorithm | Usual implementation | Notes |
|---|---|---|
| Merge sort | Recursive | Splits the list in half, then merges sorted halves |
| Quicksort | Recursive | Partitions around a pivot, then sorts each side |
| Bubble sort | Loops | Repeatedly swaps neighboring items that are out of order |
| Insertion sort | Loops | Inserts each item into an already sorted front section |
| Selection sort | Loops | Repeatedly picks the smallest remaining item |
| Heapsort | Loops | Heapify step can be written either way |
| Timsort | Loops with a stack | Python’s built-in sort; merges ordered runs |
Merge sort
Merge sort treats a list of zero or one item as already sorted. That is its base case. For a longer list, it splits the list in half, sorts each half by calling itself, and then merges the two sorted halves into one.
def merge_sort(items):
if len(items) <= 1: # base case: already sorted
return items
middle = len(items) // 2
left = merge_sort(items[:middle]) # recursive case: sort each half
right = merge_sort(items[middle:])
return merge(left, right) # combine the sorted halves
def merge(left, right):
result = []
i = 0
j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]: # <= keeps equal items in order
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
print(merge_sort([38, 27, 43, 3, 9, 82, 10])) # [3, 9, 10, 27, 38, 43, 82]
The list is halved about log2(n) times, so the recursion depth is about log2(n). The running time is O(n log n) in the best, average and worst cases. The merge helper uses a loop, which is common in recursive sorts: recursion handles the splitting, and loops handle the combining.
Quicksort
Quicksort chooses a pivot, separates the other items into those smaller than the pivot and those at least as large, and sorts each group by calling itself. On average it runs in O(n log n) time. Its worst case is O(n²), which happens when the pivots keep splitting the data unevenly, such as when a first-item pivot meets an already sorted list.
def quicksort(items):
if len(items) <= 1: # base case
return items
pivot = items[0]
smaller = [x for x in items[1:] if x < pivot]
larger = [x for x in items[1:] if x >= pivot]
return quicksort(smaller) + [pivot] + quicksort(larger)
This version is easy to read, but it builds new lists. The textbook version is an in-place algorithm, which swaps items inside the original array. Careful implementations also recurse into the smaller group first and loop over the larger one, which keeps the call depth near log2(n) even in unlucky cases.
Sorts usually written with loops
Bubble sort repeatedly swaps neighboring items that are out of order until nothing needs to move. Insertion sort takes each item and slides it into the sorted part at the front of the list. Selection sort repeatedly finds the smallest remaining item and places it next. Each of these is naturally written as nested loops, so there is no need for recursion.
Heapsort
Heapsort builds a heap, a tree-shaped structure in which the largest value is always at the top, and then repeatedly removes that top value. Most standard versions use loops for the main sort. The heapify step, which restores the heap order, can be written with a loop or with recursion, so whether heapsort uses recursion depends on how that step is written. For the full sequence of steps, see how the heapsort algorithm works.
Timsort
Python’s built-in sorted() and list.sort() use Timsort. It finds runs of items that are already in order, then merges them. Its structure is built around loops and a list of pending runs rather than deep recursion, which keeps its memory use predictable on large inputs.
Can a recursive sort avoid recursion?
Yes. A bottom-up merge sort merges single items into pairs, pairs into groups of four, and so on, using loops only. Quicksort can keep its pending ranges in an explicit stack instead of the call stack. Recursive versions are often shorter and easier to read, while loop versions give more control over memory.
Why it matters
Recursive sorts use stack memory, so their depth matters. Merge sort’s depth stays small because the list halves each time. Naive quicksort can go much deeper on unlucky input. Knowing which sorts depend on recursion helps you decide whether a sorting routine needs extra attention for very large lists. Stability is another reason to pick one sort over another. For that, see which sorting algorithms are stable, and for how the divide-and-conquer strategy compares with other methods, see divide and conquer algorithm vs dynamic programming.