Question: Hi I got this code that uses counting sort and radix sort to sort a list of numbers.Can u please help change this to sort
Hi I got this code that uses counting sort and radix sort to sort a list of numbers.Can u please help change this to sort alphabets instead like 'ab' first then 'ba' then 'bb'.Pleaseee help already posted twice.I know we can use ord and char?
def counting_sort(arr, max_value, get_index): counts = [0] * max_value
# Counting - O(n) for a in arr: counts[get_index(a)] += 1 # Accumulating - O(k) for i, c in enumerate(counts): if i == 0: continue else: counts[i] += counts[i-1]
# Calculating start index - O(k) for i, c in enumerate(counts[:-1]): if i == 0: counts[i] = 0 counts[i+1] = c
ret = [None] * len(arr) # Sorting - O(n) for a in arr: index = counts[get_index(a)] ret[index] = a counts[get_index(a)] += 1 return ret def get_digit(n, d): for i in range(d-1): n //= 10 return n % 10
def get_num_difit(n): i = 0 while n > 0: n //= 10 i += 1 return i
def radix_sort(arr, max_value=26): num_digits = get_num_difit(max_value) # O(k(n+k)) for d in range(num_digits): # Counting sort takes O(n+k) arr = counting_sort(arr, max_value, lambda a: get_digit(a, d+1)) return arr
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
