Sorting
Sorted data is what makes binary search possible, lets you find duplicates
by looking at neighbours, and makes "the top ten" a slice. You will almost
always call sorted. This page is about what it costs, what the
alternatives cost, and the one sort worth being able to write.
The simple sorts are quadratic
You built selection sort and insertion sort in the practice band. Both have a loop inside a loop over the same list, so both are O(n²). Here is insertion sort with a comparison counter to make that concrete:
def insertion_sort(items):
items = list(items)
steps = 0
for i in range(1, len(items)):
j = i
while j > 0 and items[j - 1] > items[j]:
steps += 1
items[j - 1], items[j] = items[j], items[j - 1]
j -= 1
return items, steps
import random
for n in [100, 1000]:
data = [random.randint(0, 10 ** 6) for _ in range(n)]
result, steps = insertion_sort(data)
print(n, "items:", steps, "swaps")
Ten times the items, about a hundred times the work. Insertion sort has one redeeming feature: on data that is already nearly sorted it does very little, because the inner loop stops immediately. That is why it is used inside faster sorts to finish off small pieces.
Merge sort: split, sort the halves, merge
The fast sorts all use the same move: do not sort n items, sort two
halves of n/2 and combine. Merge sort combines by merging, which you
also built in the practice band as merge sorted:
walk two sorted lists with one finger on each, always taking the smaller
front item.
def merge(left, right):
out = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
out.append(left[i])
i += 1
else:
out.append(right[j])
j += 1
out.extend(left[i:])
out.extend(right[j:])
return out
print(merge([1, 4, 9], [2, 3, 10, 11]))
Merging is linear: every item is looked at once. The sort is then a short recursive function, and the halves get sorted by the same function:
def merge(left, right):
out = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
out.append(left[i])
i += 1
else:
out.append(right[j])
j += 1
out.extend(left[i:])
out.extend(right[j:])
return out
def merge_sort(items):
if len(items) <= 1:
return items
mid = len(items) // 2
return merge(merge_sort(items[:mid]), merge_sort(items[mid:]))
print(merge_sort([8, 3, 12, 5, 1, 9, 3]))
The base case is a list of one item, which is already sorted. Why is this
n log n? Halving a list of n down to single items takes about log n
levels of splitting, and at each level the merges together touch all n
items once. n work per level, log n levels.
Watch it against insertion sort on real data:
import random, time
def insertion_sort(items):
items = list(items)
for i in range(1, len(items)):
j = i
while j > 0 and items[j - 1] > items[j]:
items[j - 1], items[j] = items[j], items[j - 1]
j -= 1
return items
def merge(left, right):
out = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
out.append(left[i]); i += 1
else:
out.append(right[j]); j += 1
out.extend(left[i:]); out.extend(right[j:])
return out
def merge_sort(items):
if len(items) <= 1:
return items
mid = len(items) // 2
return merge(merge_sort(items[:mid]), merge_sort(items[mid:]))
data = [random.randint(0, 10 ** 6) for _ in range(3000)]
for f in [insertion_sort, merge_sort, sorted]:
start = time.perf_counter()
f(data)
print(f"{f.__name__:<15}{time.perf_counter() - start:.4f} s")
sorted wins by a distance because it is written in C and uses a
refinement of the same idea (Timsort, merge sort plus insertion sort on
small runs). Merge sort is the one to be able to write, because it is the
cleanest example of divide and conquer and the shape of many other
algorithms.
Counting instead of comparing
When the values come from a small known set, you need not compare at all:
count how many of each, then write them out. Sorting a list of grades
"A" to "F", or a list of 0, 1, 2:
def sort_small_values(items, values):
counts = {v: 0 for v in values}
for item in items:
counts[item] += 1
out = []
for v in values:
out.extend([v] * counts[v])
return out
print(sort_small_values([2, 0, 1, 2, 1, 0, 0], [0, 1, 2]))
Two linear passes, so this is O(n), faster than any comparison sort can be. It only works because the possible values were known in advance.
Getting the most out of sorted
For everything else, sorted or list.sort(). What is worth knowing:
orders = [("bread", 25), ("apple", 40), ("milk", 25), ("eggs", 60)]
print(sorted(orders, key=lambda o: o[1]))
print(sorted(orders, key=lambda o: o[1], reverse=True))
print(sorted(orders, key=lambda o: (o[1], o[0])))
print(sorted(orders, key=lambda o: (-o[1], o[0])))
keyreturns what to compare. A tuple as the key sorts by its first element, then breaks ties with the second, and so on. That is how you sort by price then name.- Negate a number to reverse one field only.
(-o[1], o[0])is price descending, name ascending.reverse=Truewould reverse both. sortedis stable. Items that compare equal keep their original order. In the first line,breadstays beforemilkbecause it came first. Sorting by one key and then by another therefore works as you would hope: sort by the minor key first, then by the major one.items.sort()sorts in place and returnsNone.sorted(items)returns a new list and leaves the original alone. Writingitems = items.sort()loses the list.
Try it
Merge sort
Write merge_sort(items) that returns a new sorted list using merge
sort: split the list in half, sort each half with merge_sort, and merge
the two sorted halves. Do not change the list you were given.
merge_sort([8, 3, 12, 5, 1, 9, 3]) -> [1, 3, 3, 5, 8, 9, 12]
merge_sort([]) -> []
The point is to practise divide and conquer, so write the recursion and
the merge yourself rather than calling sorted. One check sorts fifty
thousand items, where a quadratic sort would not finish in the ten-second
limit.
def merge_sort(items):
...
Sort by price, then name
orders is a list of (name, price) tuples. Write by_price_then_name(orders)
that returns a new list sorted by price from highest to lowest, with
orders of equal price in alphabetical order of name.
by_price_then_name([("bread", 25), ("apple", 40), ("milk", 25), ("eggs", 60)])
-> [("eggs", 60), ("apple", 40), ("bread", 25), ("milk", 25)]
The point is to practise a key that returns a tuple, with one field
reversed by negation.
def by_price_then_name(orders):
...
Sort grades without comparing
grades is a list of letters from "A" to "F" (no "E"). Write
sort_grades(grades) that returns them sorted from "A" to "F"
using counting: tally how many of each grade there are, then build the
result from the tallies in order.
sort_grades(["C", "A", "F", "B", "A", "C"]) -> ["A", "A", "B", "C", "C", "F"]
sort_grades([]) -> []
The point is to practise counting sort, so do it with counts rather than
sorted.
def sort_grades(grades):
...
Next: Hashing.