input: Write a function to optimize the following Python code to sort a set of numbers in O(n) time complexity. If the input list is empty or already sorted, the function should return it as is.
  
  def sort_list(unsorted_list):
      sorted_list = []
      while unsorted_list:
          min_elem = min(unsorted_list)
          unsorted_list.remove(min_elem)
          sorted_list.append(min_elem)
      return sorted_list
  
  assert sort_list([2, 6, 3, 8, 4])==[2, 3, 4, 6, 8]
  assert sort_list([1, 2, 3, 4, 5])==[1, 2, 3, 4, 5]
  assert sort_list([])==[]
  assert sort_list([9, 1, 5, 3, 2, 8, 4, 7, 6])==[1, 2, 3, 4, 5, 6, 7, 8, 9]
  assert sort_list([2, 2, 2, 2])==[2, 2, 2, 2]
output: def sort_list(unsorted_list):
      if not unsorted_list:
          return unsorted_list
      max_val = max(unsorted_list)
      count = [0] * (max_val + 1)
      for num in unsorted_list:
          count[num] += 1
      sorted_list = []
      for i in range(len(count)):
          sorted_list.extend([i] * count[i])
      return sorted_list