Golang program to implement radix sort for sorting floating-point numbers


In this article, we will write Go language programs to implement radix sort for sorting floating-point numbers. Radix sort is usually used for sorting the integers, it can also be used to sort floating point numbers. It sorts items based on their particular bits or digits. This article provides an effective approach to sort the floating point numbers using the representation of floating point numbers.

Algorithm

  • Step 1 − Convert the floating-point numbers to a sortable format

  • Step 2 − Perform a Radix Sort for each digit position

  • Step 3 − Sort the integers based on the current digit position

  • Step 4 − Repeat Step 3 for each digit position

  • Step 5 − Revert the sorted representation to floating-point numbers

Syntax

func range(variable)

The range function iterates through any data type. To utilize 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 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.

Example 1: Using String Conversion

In this article, we will write a Golang example to implement radix sort for sorting floating-point numbers. This method involves string conversion to convert the floating point number to strings and then sort string using the sort.string() function.

package main

import (
	"fmt"
	"sort"
	"strconv"
)

func radixSort(numbers []float64) {
	strings := make([]string, len(numbers))
	for i, num := range numbers {
		strings[i] = strconv.FormatFloat(num, 'f', -1, 64)
	}

	sort.Strings(strings)

   for i, str := range strings {
		num, _ := strconv.ParseFloat(str, 64)
		numbers[i] = num
	}
}

func main() {
	numbers := []float64{3.14, 2.718, 1.618, 4.669, 0.577}
	radixSort(numbers)
	fmt.Println("Sorted numbers:", numbers)
}

Output

Sorted numbers: [0.577 1.618 2.718 3.14 4.669]

Example 2: Using Binary Representation

In this article, we will write a Golang example to implement radix sort for sorting floating-point numbers. This method involves a binary representation to convert floating point numbers to binary and then sort them.

package main

import (
	"fmt"
	"sort"
)

func radixSort(numbers []float64) {
	binary := make([]string, len(numbers))
	for i, num := range numbers {
		binary[i] = fmt.Sprintf("%064b", num)
	}

	sort.Strings(binary)

	for i, str := range binary {
		var num float64
		fmt.Sscanf(str, "%b", &num)
		numbers[i] = num
	}
}

func main() {
	numbers := []float64{3.14, 2.718, 1.618, 4.669, 0.577}
	radixSort(numbers)
	fmt.Println("Sorted numbers:", numbers)
}

Output

Sorted numbers: [0.577 4.669 2.718 3.14 1.618]

Consulion

In this article, we discussed how we can implement radix sort for sorting a string. We have implemented this operation using the string conversion as well as binary representation. Each method is simple and straightforward and can be used anytime depending on the demand of the problem in hand.

Updated on: 06-Jul-2023

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements