VB .NET Program to Implement Radix Sort

VB .NET Program to Implement Radix Sort

Sorting is one of the most common tasks in programming, especially when working with numbers, records, or large datasets. A good sorting algorithm can make programs faster and easier to manage. One such interesting and powerful technique is Radix Sort. Unlike many traditional sorting algorithms that compare elements directly, Radix Sort works by sorting numbers digit by digit. This makes it unique and very efficient for certain kinds of data.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Radix Sort is widely used when dealing with large lists of integers, such as in databases, digital systems, and data processing applications. In VB .NET, learning Radix Sort helps beginners understand arrays, loops, and the idea of breaking a big problem into smaller steps. Although the name may sound complex, the logic behind Radix Sort is quite friendly once you see it in action with simple examples.

Program 1: Basic Radix Sort Using Loops

This program demonstrates a straightforward implementation of Radix Sort using loops. It sorts numbers by processing each digit from the least significant to the most significant.

Module Module1

    Sub RadixSort(arr() As Integer)

        Dim max As Integer = arr.Max()
        Dim exp As Integer = 1

        While max \ exp > 0

            CountingSort(arr, exp)
            exp *= 10

        End While

    End Sub

    Sub CountingSort(arr() As Integer, exp As Integer)

        Dim output(arr.Length - 1) As Integer
        Dim count(9) As Integer

        For i As Integer = 0 To arr.Length - 1
            count((arr(i) \ exp) Mod 10) += 1
        Next

        For i As Integer = 1 To 9
            count(i) += count(i - 1)
        Next

        For i As Integer = arr.Length - 1 To 0 Step -1

            output(count((arr(i) \ exp) Mod 10) - 1) = arr(i)
            count((arr(i) \ exp) Mod 10) -= 1

        Next

        For i As Integer = 0 To arr.Length - 1
            arr(i) = output(i)
        Next

    End Sub

    Sub Main()

        Dim numbers() As Integer = {170, 45, 75, 90, 802, 24, 2, 66}

        RadixSort(numbers)

        Console.WriteLine("Sorted array:")

        For Each num As Integer In numbers
            Console.Write(num & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

This program works by repeatedly sorting numbers based on each digit position. Beginners can focus on understanding how digits are extracted and sorted step by step, which makes Radix Sort easier to grasp.

Program 2: Radix Sort with Original and Final Output

This version prints the array before and after sorting to clearly show the effect of Radix Sort.

Module Module1

    Sub RadixSort(arr() As Integer)

        Dim maxVal As Integer = arr.Max()
        Dim exp As Integer = 1

        While maxVal \ exp > 0
            CountingSort(arr, exp)
            exp *= 10
        End While

    End Sub

    Sub CountingSort(arr() As Integer, exp As Integer)

        Dim output(arr.Length - 1) As Integer
        Dim count(9) As Integer

        For Each num As Integer In arr
            count((num \ exp) Mod 10) += 1
        Next

        For i As Integer = 1 To 9
            count(i) += count(i - 1)
        Next

        For i As Integer = arr.Length - 1 To 0 Step -1

            Dim digit = (arr(i) \ exp) Mod 10
            output(count(digit) - 1) = arr(i)
            count(digit) -= 1

        Next

        Array.Copy(output, arr, arr.Length)

    End Sub

    Sub Main()

        Dim numbers() As Integer = {88, 12, 345, 2, 9, 100}

        Console.WriteLine("Original array:")

        For Each num As Integer In numbers
            Console.Write(num & " ")
        Next

        Console.WriteLine()

        RadixSort(numbers)

        Console.WriteLine("Sorted array:")

        For Each num As Integer In numbers
            Console.Write(num & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

Displaying both arrays helps beginners visually confirm that Radix Sort is working correctly. This approach makes learning more interactive and less abstract.

Program 3: Radix Sort Handling Different Digit Lengths

This program shows how Radix Sort naturally handles numbers with different digit lengths.

Module Module1

    Sub RadixSort(arr() As Integer)

        Dim maxNumber As Integer = arr.Max()

        Dim place As Integer = 1

        While maxNumber \ place > 0
            CountingSort(arr, place)
            place *= 10
        End While

    End Sub

    Sub CountingSort(arr() As Integer, place As Integer)

        Dim output(arr.Length - 1) As Integer
        Dim count(9) As Integer

        For i As Integer = 0 To arr.Length - 1
            count((arr(i) \ place) Mod 10) += 1
        Next

        For i As Integer = 1 To 9
            count(i) += count(i - 1)
        Next

        For i As Integer = arr.Length - 1 To 0 Step -1

            Dim digit = (arr(i) \ place) Mod 10
            output(count(digit) - 1) = arr(i)
            count(digit) -= 1

        Next

        For i As Integer = 0 To arr.Length - 1
            arr(i) = output(i)
        Next

    End Sub

    Sub Main()

        Dim numbers() As Integer = {3, 33, 333, 1, 22}
        RadixSort(numbers)

        Console.WriteLine("Sorted array:")

        For Each n As Integer In numbers
            Console.Write(n & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

This example helps beginners see that Radix Sort does not require numbers to have the same length. The algorithm adjusts automatically by checking the maximum value.

Program 4: Radix Sort with Clear Method Separation

This program organizes the logic into clear methods for better readability.

Module Module1

    Sub SortArray(arr() As Integer)
        RadixSort(arr)
    End Sub

    Sub RadixSort(arr() As Integer)

        Dim maxVal As Integer = arr.Max()
        Dim exp As Integer = 1

        While maxVal \ exp > 0
            CountingSort(arr, exp)
            exp *= 10
        End While

    End Sub

    Sub CountingSort(arr() As Integer, exp As Integer)

        Dim output(arr.Length - 1) As Integer
        Dim count(9) As Integer

        For Each num As Integer In arr
            count((num \ exp) Mod 10) += 1
        Next

        For i As Integer = 1 To 9
            count(i) += count(i - 1)
        Next

        For i As Integer = arr.Length - 1 To 0 Step -1

            Dim digit = (arr(i) \ exp) Mod 10
            output(count(digit) - 1) = arr(i)
            count(digit) -= 1

        Next

        Array.Copy(output, arr, arr.Length)

    End Sub

    Sub Main()

        Dim numbers() As Integer = {91, 46, 85, 15, 92}
        SortArray(numbers)

        Console.WriteLine("Sorted array:")

        For Each n As Integer In numbers
            Console.Write(n & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

Separating the logic into methods makes the program easier to read and reuse. Beginners can also learn good coding habits from this structure.

Program 5: Radix Sort with Small Dataset

This program focuses on a small dataset to keep things very beginner-friendly.

Module Module1

    Sub RadixSort(arr() As Integer)

        Dim maxNum As Integer = arr.Max()
        Dim exp As Integer = 1

        While maxNum \ exp > 0
            CountingSort(arr, exp)
            exp *= 10
        End While

    End Sub

    Sub CountingSort(arr() As Integer, exp As Integer)

        Dim output(arr.Length - 1) As Integer
        Dim count(9) As Integer

        For i As Integer = 0 To arr.Length - 1
            count((arr(i) \ exp) Mod 10) += 1
        Next

        For i As Integer = 1 To 9
            count(i) += count(i - 1)
        Next

        For i As Integer = arr.Length - 1 To 0 Step -1

            Dim digit = (arr(i) \ exp) Mod 10
            output(count(digit) - 1) = arr(i)
            count(digit) -= 1

        Next

        For i As Integer = 0 To arr.Length - 1
            arr(i) = output(i)
        Next

    End Sub

    Sub Main()

        Dim numbers() As Integer = {8, 3, 1, 6}
        RadixSort(numbers)

        Console.WriteLine("Sorted array:")

        For Each n As Integer In numbers
            Console.Write(n & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

Using fewer numbers makes it easier for beginners to trace each step manually and understand how Radix Sort works internally.

Program 6: Radix Sort with User-Friendly Output

This final example focuses on clarity and beginner understanding.

Module Module1

    Sub RadixSort(arr() As Integer)

        Dim maxVal As Integer = arr.Max()
        Dim exp As Integer = 1

        While maxVal \ exp > 0
            CountingSort(arr, exp)
            exp *= 10
        End While

    End Sub

    Sub CountingSort(arr() As Integer, exp As Integer)

        Dim output(arr.Length - 1) As Integer
        Dim count(9) As Integer

        For Each num As Integer In arr
            count((num \ exp) Mod 10) += 1
        Next

        For i As Integer = 1 To 9
            count(i) += count(i - 1)
        Next

        For i As Integer = arr.Length - 1 To 0 Step -1

            Dim digit = (arr(i) \ exp) Mod 10
            output(count(digit) - 1) = arr(i)
            count(digit) -= 1

        Next

        Array.Copy(output, arr, arr.Length)

    End Sub

    Sub Main()

        Dim numbers() As Integer = {50, 20, 90, 10, 40}
        RadixSort(numbers)

        Console.WriteLine("Radix Sort result:")

        For Each n As Integer In numbers
            Console.Write(n & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

This program keeps everything clean and readable, making it ideal for first-time learners.

Program 7: Radix Sort That Handles Both Positive and Negative Numbers

This program extends the basic Radix Sort to handle both negative and positive numbers, which is a common beginner question. The idea is simple and traditional: separate negative and positive numbers, sort them individually, then combine them back in the correct order.

Module Module1

    Sub RadixSortWithNegatives(arr() As Integer)

        ' Separate negative and positive numbers
        Dim negatives = arr.Where(Function(x) x < 0).Select(Function(x) Math.Abs(x)).ToArray()
        Dim positives = arr.Where(Function(x) x >= 0).ToArray()

        ' Sort both arrays using Radix Sort
        If negatives.Length > 0 Then RadixSort(negatives)
        If positives.Length > 0 Then RadixSort(positives)

        ' Rebuild original array
        Dim index As Integer = 0

        ' Add negatives back in reverse order
        For i As Integer = negatives.Length - 1 To 0 Step -1

            arr(index) = -negatives(i)
            index += 1

        Next

        ' Add positives
        For Each num As Integer In positives

            arr(index) = num
            index += 1

        Next

    End Sub

    Sub RadixSort(arr() As Integer)

        Dim maxVal As Integer = arr.Max()
        Dim exp As Integer = 1

        While maxVal \ exp > 0
            CountingSort(arr, exp)
            exp *= 10
        End While

    End Sub

    Sub CountingSort(arr() As Integer, exp As Integer)

        Dim output(arr.Length - 1) As Integer
        Dim count(9) As Integer

        For Each num As Integer In arr
            count((num \ exp) Mod 10) += 1
        Next

        For i As Integer = 1 To 9
            count(i) += count(i - 1)
        Next

        For i As Integer = arr.Length - 1 To 0 Step -1

            Dim digit = (arr(i) \ exp) Mod 10
            output(count(digit) - 1) = arr(i)
            count(digit) -= 1

        Next

        Array.Copy(output, arr, arr.Length)

    End Sub

    Sub Main()

        Dim numbers() As Integer = {-170, 45, -75, 90, -802, 24, 2, -66}
        RadixSortWithNegatives(numbers)

        Console.WriteLine("Sorted array (with negatives):")

        For Each n As Integer In numbers
            Console.Write(n & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

This program works by treating negative numbers carefully instead of forcing Radix Sort to handle them directly. By converting negatives to positive values, sorting them, and then restoring their sign in reverse order, the algorithm keeps correct numerical order. This approach is very beginner-friendly because it reuses the same Radix Sort logic without adding complex math.

For learners, this example is especially useful because it shows how traditional problem-solving techniques can extend an algorithm safely. Once you understand this pattern, you can apply it to many other sorting challenges involving mixed data.

Frequently Asked Questions (FAQ)

This section answers common questions beginners have about Radix Sort in VB .NET.

Q1. Is Radix Sort faster than comparison-based sorting?
Radix Sort can be faster for large sets of numbers because it avoids direct comparisons.

Q2. Does Radix Sort work with negative numbers?
Standard Radix Sort works best with non-negative integers and needs modification for negatives.

Q3. Is Radix Sort stable?
Yes, Radix Sort is a stable sorting algorithm when implemented correctly.

Q4. Where is Radix Sort used in real life?
It is commonly used in data processing, digital systems, and sorting large numeric datasets.

Q5. Is Radix Sort hard for beginners?
With step-by-step practice, Radix Sort becomes easy to understand and implement.

Conclusion

Radix Sort is a powerful and efficient sorting algorithm that works by sorting numbers digit by digit. In VB .NET, it helps beginners learn about loops, arrays, and logical thinking in a clear and structured way. Even though the idea is different from comparison-based sorting, practicing with small examples makes it simple and enjoyable.

By exploring these VB .NET Radix Sort programs, beginners can build confidence and strengthen their understanding of sorting algorithms. With regular practice, Radix Sort becomes a valuable tool that prepares learners for more advanced programming challenges.

Scroll to Top