Radix sort implementation in python
- User
Radix sort implementation in python
Assistant
Radix Sort is a non-comparative integer sorting algorithm. It sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. The digits are processed from the least significant digit to the most significant digit.
Here is a simple implementation of the Radix Sort algorithm in Python:
def countingSort(arr, exp1): n = len(arr) output = [0] * (n) count = [0] * (10) for i in range(0, n): index = (arr[i] // exp1) count[ (index)%10 ] += 1 for i in range(1, 10): count[i] += count[i - 1] i = n - 1 while i >= 0: index = (arr[i] // exp1) output[count[(index)%10 ] - 1] = arr[i] count[ (index)%10 ] -= 1 i -= 1 for i in range(0, len(arr)): arr[i] = output[i] def radixsort(arr): max1 = max(arr) exp = 1 while max1 / exp > 0: countingSort(arr, exp) exp *= 10 arr = [170, 45, 75, 90, 802, 24, 2, 66] radixsort(arr) print ("Sorted array is") for i in range(len(arr)): print ("%d" %arr[i], end = " ")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.