Complete the code to get the digit at a given place value in Radix Sort.
func getDigit(num, place int) int {
return (num / [1]) % 10
}We divide the number by the place value to isolate the digit, then take modulo 10 to get that digit.
Complete the code to find the maximum number in the array for Radix Sort.
func getMax(arr []int) int {
max := arr[0]
for i := 1; i < len(arr); i++ {
if arr[i] [1] max {
max = arr[i]
}
}
return max
}We update max when the current element is greater than max.
Fix the error in the counting sort step of Radix Sort to correctly count digit frequencies.
for i := 0; i < len(arr); i++ { count[(arr[i]/[1])%10]++ }
We divide by place to get the digit at the current place value.
Fill both blanks to correctly build the output array in counting sort for Radix Sort.
for i := len(arr) - 1; i >= 0; i-- { index := (arr[i] / [1]) % 10 output[count[index][2]] = arr[i] count[index]-- }
We use the place value to get the digit, then place the element at count[index] - 1 position and decrement count.
Fill all three blanks to complete the main Radix Sort loop controlling place values.
for place := 1; [1] <= maxNum; [2] { countingSort(arr, [3]) }
The loop starts with place = 1, continues while place <= maxNum, and multiplies place by 10 each iteration.