Golang program for implementation of Radix Sort
Radix Sort is a clever and intuitive little sorting algorithm. Radix Sort puts the elements in order by comparing the digits of the numbers. Radix sort is an integer sorting algorithm that sorts data with integer keys by grouping the keys by individual digits that share the same significant position and value (place value). Radix sort uses counting sort as a subroutine to sort an array of numbers.
Example
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
const digit = 4
const maxbit = -1 << 31
func main() {
var data = []int32{421, 15, -175, 90, -2, 214, -52, -166}
fmt.Println("\n--- Unsorted --- \n\n", data)
radixsort(data)
fmt.Println("\n--- Sorted ---\n\n", data, "\n")
}
func radixsort(data []int32) {
buf := bytes.NewBuffer(nil)
ds := make([][]byte, len(data))
for i, e := range data {
binary.Write(buf, binary.LittleEndian, e^maxbit)
b := make([]byte, digit)
buf.Read(b)
ds[i] = b
}
countingSort := make([][][]byte, 256)
for i := 0; i < digit; i++ {
for _, b := range ds {
countingSort[b[i]] = append(countingSort[b[i]], b)
}
j := 0
for k, bs := range countingSort {
copy(ds[j:], bs)
j += len(bs)
countingSort[k] = bs[:0]
}
}
var w int32
for i, b := range ds {
buf.Write(b)
binary.Read(buf, binary.LittleEndian, &w)
data[i] = w^maxbit
}
}
Output
--- Unsorted ---
[421 15 -175 90 -2 214 -52 -166]
--- Sorted ---
[-175 -166 -52 -2 15 90 214 421]
Most Helpful This Week
How do you handle HTTP authentication with an HTTP client in Go?
Example of Interface with Type Embedding and Method Overriding in GO language
Golang program to print all Permutations of a given string
Golang write struct to XML file
GO Hello World program
How to print string with double quote in Go?
Illustration of the dining philosophers problem in Golang
How to convert String to Boolean Data Type Conversion in Go?
Example: How to use TeeReader from IO Package in Golang?
GO language program with example of Array Reverse Sort Functions for integer and strings