# Golang program to implement radix sort

In this article we are going to understand how to use least significant bit and the most significant bit to implement radix sort. Radix sort is a sorting Algorithm that sorts elements by grouping them based on their individual digits or by the position of their digits. It is a linear time sorting Algorithm that has a time complexity of O(nk).

## Using the Least Significant Bit

In this method, we will write a go language program to implement the radix sort by using the least significant bit. LSD sorts the elements from right to left by comparing their individual digits.

### Algorithm

• Step 1 − First, we need to import the fmt package. Then create a function named radixSortMSD() to sort the array.

• Step 2 − Inside the function create a new array. Use the for loop to iterate over the array and store the maximum element of the array in a variable.

• Step 3 − Now, initialize a count array to keep track of the number of elements that have a particular digit.

• Step 4 − Traverse the array and increment the count for the corresponding digit for each element.

• Step 5 − Modify the count array to store the actual position of each element in the Output array. Copy the elements from the input array to the Output array in the order specified by the count array.

• Step 6 − Update the input array to the sorted Output array and return the sorted array.

• Step 7 − Start the main() function. Inside the main() initialize an array and store value to it. further, call the radixSort() function and pass the array as an argument to the function.

• Step 8 − Store the result obtained by the function in a variable and print It on the screen.

### Example

The following Example demonstrate how to develop a go language program to implement the radix sort by using the least significant bit

package main

import "fmt"

max := arr
for i := 1; i < len(arr); i++ {
if arr[i] > max {
max = arr[i]
}
}
exp := 1
for max/exp > 0 {
count := make([]int, 10)
for i := 0; i < len(arr); i++ {
count[(arr[i]/exp)%10]++
}
for i := 1; i < 10; i++ {
count[i] += count[i-1]
}
Output := make([]int, len(arr))
for i := len(arr) - 1; i >= 0; i-- {
Output[count[(arr[i]/exp)%10]-1] = arr[i]
count[(arr[i]/exp)%10]--
}
for i := 0; i < len(arr); i++ {
arr[i] = Output[i]
}
exp *= 10
}
return arr
}

func main() {
arr := []int{15, 31, 42, 20, 9}
fmt.Println("The unsorted array is:", arr)
fmt.Println("The sorted array is:", result)
}


### Output

The unsorted array is: [15 31 42 20 9]
The sorted array is: [9 15 20 31 42]


## Using Most Significant Bit

In this method, we will write a go language program to sort an array by using radix sort using the most significant bit with the help of the following Syntax.

### Syntax

func len(v Type) int


The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

### Algorithm

• Step 1 − First, we need to import the fmt package. Then we have created two functions named radixSortMSD() and radixSortMSDHelper().

• Step 2 − The radixSort() function accepts the array to be sorted as an argument and returns the sorted array. the function calls the radixSortMSDHelper() function.

• Step 3 − The radixSortMSDHelper() function accepts the array to be sorted along with the maximum element of the array as an argument.

• Step 4 − The function returns the sorted array to the radixSort() function which finally returns the array to the main() section of the program.

• Step 5 − Now, start the main() function. Inside the main() initialize the array to be sorted and assign values to it.

• Step 6 − Further call the radixSortMSD() function by passing the array as argument to the function and store the result in a separate variable.

• Step 7 − Now, print the final array on the screen by using fmt.Println() function.

### Example

In the following Example, we will learn how to develop a go language program to implement the radix sort by using most significant bit

package main

import "fmt"

max := arr
for i := 1; i < len(arr); i++ {
if arr[i] > max {
max = arr[i]
}
}
return arr
}

func radixSortMSDHelper(arr []int, exp int) []int {
if exp <= 0 {
return arr
}
count := make([]int, 10)
for i := 0; i < len(arr); i++ {
count[(arr[i]/exp)%10]++
}
for i := 1; i < 10; i++ {
count[i] += count[i-1]
}
Output := make([]int, len(arr))
for i := len(arr) - 1; i >= 0; i-- {
Output[count[(arr[i]/exp)%10]-1] = arr[i]
count[(arr[i]/exp)%10]--
}
subarrays := make([][]int, 10)
for i := 0; i < len(Output); i++ {
subarrays[(Output[i]/exp)%10] = append(subarrays[(Output[i]/exp)%10], Output[i])
}
result := make([]int, 0)
for i := 0; i < 10; i++ {
if len(subarrays[i]) > 0 {
result = append(result, subarrays[i]...)
}
}
return result
}

func main() {
arr := []int{5, 3, 2, 15, 9}
fmt.Println("The unsorted array is:", arr)
fmt.Println("The sorted array is:", result)
}


### Output

The unsorted array is: [5 3 2 15 9]
The sorted array is: [2 3 5 9 15]


## Conclusion

We have successfully compiled and executed a go language program to sort an array by using radix sort Algorithm. Here we have implemented two Examples. in the first Example we are sorting the array through least significant bit while in the second one we are sorting it through most significant bit. Both methods have a time complexity of O(nk).

Updated on: 05-Apr-2023

214 Views 