Golang program to implement radix sort for sorting strings


Radix sort is efficient for sorting strings because of its inherent structure of string data type.In this article, we will write a Go language program to implement radix sort for sorting strings. We start working on an unsorted array string and demonstrate how radix sort can be applied to sort it. A string is a character array or combination of characters.

Syntax

func make ([] type, size, capacity)

The make function in Go is used to build an array/map. It receives as arguments the kind of variable to be generated, as well as its size and capacity.

func range(variable)

The range function iterates through any data type. To utilise this, first type the range keyword followed by the data type to which we want to iterate, and the loop will iterate until the final element of the variable is reached.

func len(v Type) int

The len() function is used to get the length of 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

  • Begin with a string array that has to be sorted.

  • Find the string with the longest length in the array.

  • Apply the Radix Sort algorithm to each character position, beginning at the right and working your way to the left.

  • The array of strings will be sorted in lexicographic order after sorting all character locations.

  • Print the sorted strings.

Example

In this example, we will write a Golang program to implement radix sort for sorting strings using a radix sort algorithm which compares the array elements to buckets. Here as a subroutine, the counting sort function is used to sort the string based on characters at current digit position.

package main

import (
   "fmt"
)

func countingSort(strs []string, index int) {
   n := len(strs)
   output := make([]string, n)
   count := make([]int, 256)

   for i := 0; i < n; i++ {
      char := getCharAtIndex(strs[i], index)
      count[char]++
   }

   for i := 1; i < 256; i++ {
      count[i] += count[i-1]
   }

   // Build the output array
   for i := n - 1; i >= 0; i-- {
      char := getCharAtIndex(strs[i], index)
      output[count[char]-1] = strs[i]
      count[char]--
   }

   for i := 0; i < n; i++ {
      strs[i] = output[i]
   }
}

func getCharAtIndex(str string, index int) int {
   if index < len(str) {
      return int(str[index])
   }
   return 0
}

func radixSort(strs []string) {
   maxLen := getMaxLen(strs)

   for i := maxLen - 1; i >= 0; i-- {
      countingSort(strs, i)
   }
}

func getMaxLen(strs []string) int {
   maxLen := 0
   for _, str := range strs {
      if len(str) > maxLen {
         maxLen = len(str)
      }
   }
   return maxLen
}

func main() {
   strs := []string{"banana", "apple", "cherry", "date", "grape", "kiwi", "mango", "orange"}
   radixSort(strs)
   fmt.Println("Sorted strings:", strs)
}

Output

Sorted strings: [apple banana cherry date grape kiwi mango orange]

Conclusion

In this article we have checked how we can perform radix sort for sorting strings. We explored the implementation of this program using a counting sort as a subroutine.

Updated on: 06-Jul-2023

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements