Golang program to implement radix sort using bucket sort as a subroutine


A subroutine is a part of a program which performs a specific task and can be called repeatedly based on the purpose and use. When a subroutine is called, the program shifts to that routine and executes the instructions inside that routine.Radix sort is an algorithm that sorts elements from their digits. It has a linear time complexity and is used for sorting large amounts of data. It uses counting sort to calculate the frequencies of digits.In this Golang article, we will write programs to implement radix sort using bucket sort as a subroutine.

Syntax

func len(v Type) int

To determine the length of any argument, use the len() function. It accepts one input, and returns the integer value that is the variable's length.

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 utilize this, we must first put 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.

Algorithm

  • Step 1 − Create a bucketSort function.

  • Step 2 − Find the array's maximum element.

  • Step 3 − For each digit position, do Radix Sort

  • Step 4 − Use the LSD Method to implement Radix Sort

  • Step 5 − Use the iteration Method to implement Radix Sort

Example 1: Least significant Digit

In this example, we will write a Go language program to implement radix sort using bucket sort as a subroutine. In this method the elements are distributed into buckets depending on digits and reconstructed in the original order for every position of digit.

package main

import (
   "fmt"
   "math"
)

func main() {
   array := []int{10, 14, 2, 43, 65, 76, 23, 674}

   fmt.Println("Before sorting :", array)
   radixSortLSD(array)
   fmt.Println("After sorting :", array)
}

func bucketSort(arr []int, exp int) {
   n := len(arr)
   buckets := make([][]int, 10)
   for i := 0; i < 10; i++ {
      buckets[i] = make([]int, 0)
   }

   for i := 0; i < n; i++ {
      digit := (arr[i] / exp) % 10
      buckets[digit] = append(buckets[digit], arr[i])
   }

   index := 0
   for i := 0; i < 10; i++ {
      for j := 0; j < len(buckets[i]); j++ {
         arr[index] = buckets[i][j]
         index++
      }
   }
}

func radixSortLSD(arr []int) {
   max := int(math.Inf(-1))
   for _, num := range arr {
      if num > max {
         max = num
      }
   }

   exp := 1
   for exp <= max {
      bucketSort(arr, exp)
      exp *= 10
   }
}

Output

Before sorting : [10 14 2 43 65 76 23 674]
 After sorting : [2 10 14 23 43 65 76 674]

Example 2: Using Iteration

In this example, we will write a Go language program to implement radix sort using bucket sort as a subroutine. In this method the elements are distributed iteratively into buckets based on their digits.

package main

import (
   "fmt"
   "math"
)

func bucketSort(arr []int, exp int) {
   n := len(arr)
   buckets := make([][]int, 10)
   for i := 0; i < 10; i++ {
      buckets[i] = make([]int, 0)
   }

   for i := 0; i < n; i++ {
      digit := (arr[i] / exp) % 10
      buckets[digit] = append(buckets[digit], arr[i])
   }

   index := 0
   for i := 0; i < 10; i++ {
      for j := 0; j < len(buckets[i]); j++ {
         arr[index] = buckets[i][j]
         index++
      }
   }
}

func radixSortIterative(arr []int) {
   max := int(math.Inf(-1))
   for _, num := range arr {
      if num > max {
         max = num
      }
   }

   for exp := 1; max/exp > 0; exp *= 10 {
      bucketSort(arr, exp)
   }
}

func main() {
   array := []int{11, 15, 1, 42, 64, 78, 23, 674}

   fmt.Println("Before Sorting:", array)     
   radixSortIterative(array)
   fmt.Println("After sorting :", array)
}

Output

Before Sorting: [11 15 1 42 64 78 23 674]
After sorting : [1 11 15 23 42 64 78 674]

Conclusion

In this article we have checked how we can perform radix sort using bucket sort as a subroutine in go language. We explored the implementation of this program using iteration and LSD.

Updated on: 06-Jul-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements