Bucket Sort

Bucket sort (a.k.a Bin Sort) is a sorting algorithm that works by partitioning an array into a number buckets. Each bucket then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm.



									Public Shared Sub BucketSort(ByRef data As Integer())
	Dim minValue As Integer = data(0)
	Dim maxValue As Integer = data(0)

	For i As Integer = 1 To data.Length - 1
		If data(i) > maxValue Then
			maxValue = data(i)
		End If
		If data(i) < minValue Then
			minValue = data(i)
		End If
	Next

	Dim bucket As List(Of Integer)() = New List(Of Integer)(maxValue - minValue) {}

	For i As Integer = 0 To bucket.Length - 1
		bucket(i) = New List(Of Integer)()
	Next

	For i As Integer = 0 To data.Length - 1
		bucket(data(i) - minValue).Add(data(i))
	Next

	Dim k As Integer = 0
	For i As Integer = 0 To bucket.Length - 1
		If bucket(i).Count > 0 Then
			For j As Integer = 0 To bucket(i).Count - 1
				data(k) = bucket(i)(j)
				k += 1
			Next
		End If
	Next
End Sub
								


Example

									Dim data() As Integer = { -1, 25, -58964, 8547, -119, 0, 78596 }
BucketSort(data)
								


Output

									-58964
-119
-1
0
25
8547
78596