Bucket Sort is a simple and efficient sorting algorithm that works especially well when data is evenly distributed over a known range. Instead of comparing elements one by one like Bubble Sort or Selection Sort, Bucket Sort spreads elements into different groups called buckets. Each bucket is then sorted individually and finally combined to produce the sorted result. This idea comes from traditional sorting methods where items are grouped first and ordered later, making the process easier and faster in many real-world cases.
with hands-on learning.
get the skills and confidence to land your next move.
In VB .NET programming, Bucket Sort is useful when dealing with floating-point numbers, scores, percentages, or values that fall within a predictable range. Beginners often like this algorithm because it is easy to visualize and understand. You can imagine placing numbers into boxes, sorting each box, and then joining everything back together. This article walks through multiple VB .NET programs to implement Bucket Sort in different ways, helping you build confidence step by step.
Program 1: Basic Bucket Sort Using Lists
This program shows a simple and traditional Bucket Sort approach using lists. It works well for decimal values between 0 and 1 and is perfect for beginners who want to understand the core idea.
Module Module1
Sub BucketSort(arr() As Double)
Dim n As Integer = arr.Length
Dim buckets(n - 1) As List(Of Double)
For i As Integer = 0 To n - 1
buckets(i) = New List(Of Double)()
Next
For Each value As Double In arr
Dim index As Integer = CInt(n * value)
If index >= n Then index = n - 1
buckets(index).Add(value)
Next
For i As Integer = 0 To n - 1
buckets(i).Sort()
Next
Dim k As Integer = 0
For i As Integer = 0 To n - 1
For Each value In buckets(i)
arr(k) = value
k += 1
Next
Next
End Sub
Sub Main()
Dim data() As Double = {0.42, 0.32, 0.23, 0.52, 0.25, 0.47}
BucketSort(data)
For Each v As Double In data
Console.Write(v & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis program divides values into buckets based on their range and sorts each bucket using the built-in sort method. Beginners can easily follow how numbers move from the array into buckets and back again. It is useful because it clearly shows the main idea behind Bucket Sort without extra complexity.
Program 2: Bucket Sort for Integer Values
This version modifies Bucket Sort to work with integers instead of decimals. It uses a known range to place numbers into buckets.
Module Module1
Sub BucketSortIntegers(arr() As Integer)
Dim maxVal As Integer = arr.Max()
Dim bucketCount As Integer = 5
Dim buckets(bucketCount - 1) As List(Of Integer)
For i As Integer = 0 To bucketCount - 1
buckets(i) = New List(Of Integer)()
Next
For Each num As Integer In arr
Dim index As Integer = (num * bucketCount) \ (maxVal + 1)
buckets(index).Add(num)
Next
Dim k As Integer = 0
For i As Integer = 0 To bucketCount - 1
buckets(i).Sort()
For Each num In buckets(i)
arr(k) = num
k += 1
Next
Next
End Sub
Sub Main()
Dim numbers() As Integer = {29, 25, 3, 49, 9, 37, 21}
BucketSortIntegers(numbers)
For Each n As Integer In numbers
Console.Write(n & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis approach shows beginners how Bucket Sort can be adapted beyond decimal values. It is useful when working with scores or ranks where the range is known. Understanding this program helps learners see how bucket size affects sorting.
Program 3: Bucket Sort Using Arrays Instead of Lists
This program demonstrates Bucket Sort using arrays, which may feel more familiar to beginners coming from basic VB .NET lessons.
Module Module1
Sub BucketSortArray(arr() As Integer)
Dim maxVal As Integer = arr.Max()
Dim bucket(maxVal) As Integer
For Each num As Integer In arr
bucket(num) += 1
Next
Dim index As Integer = 0
For i As Integer = 0 To maxVal
While bucket(i) > 0
arr(index) = i
index += 1
bucket(i) -= 1
End While
Next
End Sub
Sub Main()
Dim data() As Integer = {4, 1, 3, 2, 4, 1}
BucketSortArray(data)
For Each v As Integer In data
Console.Write(v & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis version feels close to Counting Sort but still follows the bucket idea. It helps beginners understand how memory can be used to simplify sorting. It is very efficient when the range of numbers is small.
Program 4: Bucket Sort with Manual Sorting Inside Buckets
This program avoids built-in sorting and uses a simple insertion method inside each bucket.
Module Module1
Sub InsertSorted(bucket As List(Of Integer), value As Integer)
Dim i As Integer = 0
While i < bucket.Count AndAlso bucket(i) < value
i += 1
End While
bucket.Insert(i, value)
End Sub
Sub BucketSortManual(arr() As Integer)
Dim bucketCount As Integer = 5
Dim buckets(bucketCount - 1) As List(Of Integer)
For i As Integer = 0 To bucketCount - 1
buckets(i) = New List(Of Integer)()
Next
For Each num As Integer In arr
Dim index As Integer = num * bucketCount \ 100
InsertSorted(buckets(index), num)
Next
Dim k As Integer = 0
For Each bucket In buckets
For Each num In bucket
arr(k) = num
k += 1
Next
Next
End Sub
Sub Main()
Dim values() As Integer = {42, 32, 23, 52, 25, 47}
BucketSortManual(values)
For Each v In values
Console.Write(v & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis program is great for learning because it shows how sorting can be done step by step. Beginners can clearly see how numbers are placed in order inside each bucket. It builds strong fundamentals in algorithm thinking.
Program 5: Recursive Bucket Sort Approach
This version introduces a recursive idea, mainly for learning purposes.
Module Module1
Function RecursiveBucketSort(arr As List(Of Integer)) As List(Of Integer)
If arr.Count <= 1 Then Return arr
Dim buckets(4) As List(Of Integer)
For i As Integer = 0 To 4
buckets(i) = New List(Of Integer)()
Next
For Each num As Integer In arr
Dim index As Integer = num * 5 \ 100
buckets(index).Add(num)
Next
Dim result As New List(Of Integer)
For Each bucket As List(Of Integer) In buckets
' Old rule: small bucket → simple sort
If bucket.Count > 1 Then
bucket.Sort()
End If
For Each num As Integer In bucket
result.Add(num)
Next
Next
Return result
End Function
Sub Main()
Dim data As New List(Of Integer) From {34, 12, 45, 9, 27, 18}
Dim sorted = RecursiveBucketSort(data)
For Each v In sorted
Console.Write(v & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis recursive version helps beginners understand how Bucket Sort can be combined with other programming ideas. While not always practical, it strengthens thinking skills and shows how algorithms can be flexible.
Program 6: Bucket Sort That Handles Both Negative and Positive Numbers
This program improves Bucket Sort by separating negative and positive numbers, sorting them individually, and then merging them back together in the correct order. It uses predefined data that includes both negative and positive values, making it practical for real-world scenarios.
Module Module1
Sub BucketSortWithNegatives(arr() As Integer)
Dim negatives As New List(Of Integer)
Dim positives As New List(Of Integer)
For Each num In arr
If num < 0 Then
negatives.Add(Math.Abs(num))
Else
positives.Add(num)
End If
Next
negatives.Sort()
positives.Sort()
Dim index As Integer = 0
For i As Integer = negatives.Count - 1 To 0 Step -1
arr(index) = -negatives(i)
index += 1
Next
For Each num In positives
arr(index) = num
index += 1
Next
End Sub
Sub Main()
Dim data() As Integer = {15, -3, 22, -8, 7, -1, 10}
BucketSortWithNegatives(data)
For Each v In data
Console.Write(v & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis program works by first splitting the array into two parts, one for negative numbers and one for positive numbers. Negative values are converted to positive for easy sorting, then restored after sorting in reverse order so the smallest negative appears first. This approach is useful because standard Bucket Sort does not directly support negative values. Beginners can easily understand and apply this logic when working with mixed datasets such as temperature readings, financial values, or score differences.
Frequently Asked Questions (FAQ)
This section answers common beginner doubts about Bucket Sort in VB .NET.
Q1. Is Bucket Sort faster than Bubble Sort?
Yes, Bucket Sort can be much faster when data is evenly distributed and the range is known.
Q2. Can Bucket Sort handle negative numbers?
Yes, but extra logic is needed to separate negative and positive values.
Q3. Is Bucket Sort stable?
It can be stable if the sorting inside buckets preserves order.
Q4. When should I avoid Bucket Sort?
It is not ideal when the data range is very large or unknown.
Q5. Is Bucket Sort good for beginners?
Yes, it is one of the easiest sorting algorithms to visualize and understand.
Conclusion
Bucket Sort is a friendly and powerful sorting algorithm that fits well with VB .NET programming. By grouping values into buckets and sorting them separately, it offers a clean and logical way to organize data. Through the programs in this article, you have seen different approaches using lists, arrays, manual sorting, and even recursion.
For beginners, practicing these examples helps build confidence and a strong foundation in sorting algorithms. Try modifying the programs, changing bucket sizes, or testing different data sets. The more you experiment, the better you will understand how traditional sorting ideas still play an important role in modern programming.




