Golang Program to Implement Radix Sort for Sorting Integers in Descending Order


Radix sort is a non-comparative sorting algorithm that works by distributing elements into different buckets based on their significant digits. In this article, we will explore a Golang program that implements radix sort for sorting integers in descending order. Here we are going to use three different methods: Getmax,countsort, and radixsort along with examples to elaborate the concept.

Syntax

func getMax(arr []int) int

The Syntax "func getMax(arr []int) int" defines a function named "getMax" that takes a slice of integers as a parameter and returns an integer value.

func countSort(arr []int, exp int)

The Syntax "func countSort(arr []int, exp int)" defines a function named "countSort" that takes two parameters: a slice of integers named "arr" and an integer named "exp".

func radixSort(arr []int)

The Syntax "func radixSort(arr []int)" defines a function named "radixSort" that takes a single parameter: a slice of integers named "arr".

Algorithm

  • Start by defining a function named RadixSort that takes an array of integers as input.

  • Find the maximum element in the input array to determine the number of digits in the largest number. Let's call this value max.

  • Iterate through each digit position, starting from the least significant digit to the most significant digit.

  • Within each iteration, create 10 buckets (0 to 9) to temporarily store the integers based on their digit value at the current position.

  • Iterate through each integer in the input array and distribute them into the appropriate bucket based on the digit value at the current position.

  • After distributing all the integers into buckets, collect them back into the input array in the order of the buckets from 9 to 0.

  • Repeat steps 4-6 for each digit position until all digits have been processed.

  • Finally, the input array will be sorted in descending order using radix sort.

Example

The following program defines three functions − getMax, countSort, and radixSort. The getMax function finds the maximum value in the given array. The countSort function performs counting sort based on the given exponent (exp). The radixSort function implements radix sort by repeatedly calling the countSort function for each digit place. In the main function, an array of integers is defined, and then radix sort is applied to sort the array in descending order. The sorted array is printed before and after the sorting process.

package main

import (
   "fmt"
)

func getMax(arr []int) int {
   max := arr[0]

   for _, num := range arr {
      if num > max {
         max = num
      }
   }

   return max
}

func countSort(arr []int, exp int) {
   n := len(arr)
   output := make([]int, n)
   count := make([]int, 10)

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

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

   for i := n - 1; i >= 0; i-- {
      output[count[(arr[i]/exp)%10]-1] = arr[i]
      count[(arr[i]/exp)%10]--
   }

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

func radixSort(arr []int) {
   max := getMax(arr)

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

func main() {
   arr := []int{170, 45, 75, 90, 802, 24, 2, 66}
   fmt.Println("Array before sorting:", arr)

   radixSort(arr)

   fmt.Println("Array after sorting:", arr)
}

Output

Array before sorting: [170 45 75 90 802 24 2 66]
Array after sorting: [2 24 45 66 75 90 170 802]

Conclusion

In Conclusion, we have explored the implementation of radix sort for sorting integers in descending order using the Go programming language. Radix sort is a non-comparative sorting algorithm that operates on the individual digits of the numbers being sorted. Throughout the article, we have demonstrated a step-by-step approach to implementing radix sort, covering the key concepts and operations involved in the algorithm. By distributing the numbers into different buckets based on their significant digits, radix sort efficiently sorts the integers.

Updated on: 20-Jul-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements