The greatest gift of sorting is this: the values closest to each other end up side by side. "Which two numbers are closest?" needs every pair tried in an unsorted list (n × n); after sorting you look only at neighbours (n).
ordered = sorted(values)best = ordered[1] - ordered[0]for i in range(2, len(ordered)):best = min(best, ordered[i] - ordered[i - 1])return best
In a sorted list some answers simply sit at a position: the smallest at the start, the largest at the end, the median in the middle. The largest spread (end to end) does not even need sorting: largest minus smallest is found in one pass.