Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please code using Python. This is what I got so far: def key_positions(seq, key): Returns an array (list) such that the i-th element of the

Please code using Python.

image text in transcribed

image text in transcribed

This is what I got so far:

def key_positions(seq, key): """Returns an array (list) such that the i-th element of the array is the starting position (in the sorted output of the counting sort algorithm) of objects in seq whose key is i""" if type(seq) == range: item = list(range(seq[0], seq[-1] + 1)) seq = item object_input = [] for i in seq: object_input.append(key(i)) k = max(object_input) C = list(range(k + 1)) for i in range(0, k + 1): C[i] = 0 for a in seq: C[key(a)] = C[key(a)] + 1 sum = 0 for index in range(0, k + 1): C[index], sum = sum, sum + C[index] return C

def sorted_array(seq, key, positions): """Produces an array (list) containing the elements of seq sorted according to the key function""" n = len(seq) B = [0] * n for a in seq: B[positions[key(a)]] = a positions[key(a)] = (positions[key(a)] + 1) return B

def radix_sort(numbers, d): """Radix Sort""" array = [] for i from range(1, d): use stable sort to sort array A on digit i

My teacher said we can use modulo to help us extract digits

Write a function radix_sort (numbers, d) that takes a sequence of natural numbers and uses counting sort iteratively to return the sorted sequence. The numbers are sorted up to the d-th digit from the right. The second argument, d, is a positive (and thus non-zero) integer For example, if d is 1, the numbers are sorted only based on the right most decimal digit (1s). If d is 2, the numbers are first sorted with d-1 and then with d-2 which corresponds to the second digit from the right (10s) and so on. Remember to include your implementation of a stable sorting algorithm. For example: Test Result print (radix_sort ([329, 457, 657, 839, 436, 720, 355, 3)) [329, 355, 436, 4! print (radix_sort ([329, 457, 657, 839, 436, 720, 355,1)) [720, 355, 436, 4! print (radix_sort([329, 457, 657, 839, 436, 720, 355], 2)) [720, 329, 436, 8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions