Serhat - 1 year ago 100
Vb.net Question

# The Fastest Permutation Code Permutating Numbers in VB NET

There is a famous and the fastest permutation code without any "function" for VB .Net to permutate numbers just in several rows, that I can't remember unfortunately.

Is there anyone know this code? Or know like this?

Some part of the code is here:

UPDATE: I FOUND IT. ALL THE WORKING CODES HERE:

``````Dim L(4) As Byte
Dim I As Byte
Dim K As Byte
Dim J As Byte
Dim RESULTS As String
Dim UB, UBm1 As Integer

L = {1, 2, 3, 4, 5}

UB = L.GetUpperBound(0)
UBm1 = UB - 1

Do
I = UBm1
Do While I > 0 And L(I) >= L(I + 1)
I -= 1
Loop
K = L(I)
J = UB
Do While J > 0 And L(J) <= K
J -= 1
Loop

RESULTS = L(0) & "," & L(1) & "," & L(2) & "," & L(3) & "," & L(4)

L(I) = L(J)
L(J) = K
Array.Reverse(L, I + 1, UB - I)

Loop While J
``````

It seems like you're looking for this ...

http://www.cut-the-knot.org/do_you_know/AllPerm.shtml (2. Lexicographic order and finding the next permutation)

... ?

In case you are, the starting values for `I` and `J` are incorrect (they should be 4 and 5 instead of 3 and 4 respectively).

(I know the example uses `swap`, but that can be replaced with a single colon-delimited line.)

``````Dim L(4) As Byte
L = {1, 2, 3, 4, 5}

Dim K as Byte

For N as integer = 1 to 120 'No. of permutations: 5!
Dim I As Byte = 4, J as Byte = 5

While L(I - 1) >= L(I)
I -= 1
End While
While L(J - 1) <= L(I - 1)
J -= 1
End While

K = L(I - 1) : L(I - 1) = L(J - 1) : L(J - 1) = K
I += 1 : J = 5
While I < J
K = L(I - 1) : L(I - 1) = L(J - 1) : L(J - 1) = K
I += 1 : J -= 1
End While

Dim RESULT as String = L(0) & "," & L(1) & "," & L(2) & "," & L(3) & "," & L(4)
'Export / print RESULT as you like, e.g. Console.WriteLine or something
Next
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download