Skip to contentKEEP THE CRAFTTHE SANCTUARY

Sort a Bounded Integer Range

Sort integers from a small range by counting how often each value appears.

The problem

Implement counting_sort(values). values is a list of integers that may be negative and may repeat, and the gap between the smallest and largest value is small. Return a new list with the same integers in ascending order. Do not call sorted or list.sort: use counting sort, which counts how many times each value appears and rebuilds the list from those counts.

Starter code

def counting_sort(values):
    pass

Public examples

  1. values = [3, -1, 2, -1]gives[-1, -1, 2, 3]
  2. values = [4, 4]gives[4, 4]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.