How to resolve the algorithm Sorting algorithms/Bogosort step by step in the VBA programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Bogosort step by step in the VBA programming language
Table of Contents
Problem Statement
Bogosort a list of numbers.
Bogosort simply shuffles a collection randomly until it is sorted.
"Bogosort" is a perversely inefficient algorithm only used as an in-joke.
Its average run-time is O(n!) because the chance that any given shuffle of a set will end up in sorted order is about one in n factorial, and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence.
Its best case is O(n) since a single pass through the elements may suffice to order them.
Pseudocode:
The Knuth shuffle may be used to implement the shuffle part of this algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Bogosort step by step in the VBA programming language
Source code in the vba programming language
Private Function Knuth(a As Variant) As Variant
Dim t As Variant, i As Integer
If Not IsMissing(a) Then
For i = UBound(a) To LBound(a) + 1 Step -1
j = Int((UBound(a) - LBound(a) + 1) * Rnd + LBound(a))
t = a(i)
a(i) = a(j)
a(j) = t
Next i
End If
Knuth = a
End Function
Private Function inOrder(s As Variant)
i = 2
Do While i <= UBound(s)
If s(i) < s(i - 1) Then
inOrder = False
Exit Function
End If
i = i + 1
Loop
inOrder = True
End Function
Private Function bogosort(ByVal s As Variant) As Variant
Do While Not inOrder(s)
Debug.Print Join(s, ", ")
s = Knuth(s)
Loop
bogosort = s
End Function
Public Sub main()
Debug.Print Join(bogosort(Knuth([{1,2,3,4,5,6}])), ", ")
End Sub
You may also check:How to resolve the algorithm Sum and product of an array step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the REXX programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the PL/I programming language
You may also check:How to resolve the algorithm Filter step by step in the Maple programming language
You may also check:How to resolve the algorithm Cullen and Woodall numbers step by step in the BASIC programming language