Bogo Sort

Bogo sort, also known as permutation sort, stupid sort, slow sort, shotgun sort or monkey sort, is a particularly ineffective sorting algorithm based on the generate and test paradigm. The algorithm successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting, but may be used for educational purposes, to contrast it with other more realistic algorithms.



									Private Shared Function IsSorted(ByRef data As Integer()) As Boolean
	Dim count As Integer = data.Length

	While System.Threading.Interlocked.Decrement(count) >= 1
		If data(count) < data(count - 1) Then
			Return False
		End If
	End While

	Return True
End Function

Private Shared Sub Shuffle(ByRef data As Integer())
	Dim temp As Integer, rnd As Integer
	Dim rand As New Random()

	For i As Integer = 0 To data.Length - 1
		rnd = rand.[Next](data.Length)
		temp = data(i)
		data(i) = data(rnd)
		data(rnd) = temp
	Next
End Sub

Public Shared Sub BogoSort(ByRef data As Integer())
	While Not IsSorted(data)
		Shuffle(data)
	End While
End Sub
								


Example

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


Output

									-58964
-119
-1
0
25
8547
78596