Abstract

Wenn Sie alle Permutationen eines Arrays erzeugen wollen, empfehle ich den Algorithmus (externer Link!) Quickperm. Es ist einer der effizientesten Permutationsalgorithmen. Er basiert auf dem dem Austausch (engl. Swap) einzelner Elemente und wurde durch Heap Sort inspiriert.

Die hier vorgestellte Variante ist die CountDown Variante. Für die Implementierung in Excel / VBA wurden alle Indizes um 1 erhöht, damit Arrays mit dem Index 1 beginnen.

Beispiel

Eingabe:

quickperm_input

Ausgabe:

quickperm_output

(alle Links sind extern!) Bitte den Haftungsausschluss im Impressum beachten.

Quickperm:

Quickperm - Scalable Permutations! The Heart of Artificial Intelligence. Die originale Website.

Baeldung - Generate All Permutations of an Array. Ein hilfreiches Tutorial (engl.) welches in Quickperm mündet.

Permutationen mit Nebenbedingungen:

Rosetta Code - Permutations with some identical elements

Computer Science - Enumerating all partial permutations of given length in lexicographic order

Appendix – Programmcode quickperm

Bitte den Haftungsausschluss im Impressum beachten.

Option Explicit

Public r As Long 'Output row

Sub QuickPerm(a As Variant)
'Generates all permutations of array a.
'Quickperm is one of the most efficient permutation algorithms.
'It is based on swapping and inspired by Heap sort.
'Countdown variant, indexes increased by 1.
'Algorithm originally from https://www.quickperm.org, migrated to VBA.
'See also: https://www.baeldung.com/cs/array-generate-all-permutations
'Version 0.1 28-Jun-2024
Dim i As Long, idx As Long, j As Long, n As Long, v As Variant
With Application.WorksheetFunction
a = .Transpose(.Transpose(a)) 'Assuming horizontal range
Call VisitPerm(a)
n = UBound(a)
ReDim p(1 To n + 1) As Long
For i = 1 To n + 1: p(i) = i: Next i 'Initialize p()
idx = 2
Do While idx < n + 1
  p(idx) = p(idx) - 1
  If idx Mod 2 = 0 Then
    j = p(idx)
  Else
    j = 1
  End If
  v = a(j): a(j) = a(idx): a(idx) = v 'Swap a(j) and a(idx)
  Call VisitPerm(a)
  idx = 2
  Do While p(idx) = 1
    p(idx) = idx
    idx = idx + 1
  Loop
Loop
End With
End Sub

Sub VisitPerm(a As Variant)
'Print current permutation in immediate window and on sheet Output.
'You can analyze the permutation or do other things as well.
Dim i As Long
For i = 1 To UBound(a)
  Debug.Print a(i);
  wsO.Cells(r, i) = a(i)
Next i
Debug.Print
r = r + 1
End Sub

Sub test()
wsO.Cells.ClearContents
r = 1
Call QuickPerm(wsI.Range("A1:D1"))
Call QuickPerm(wsI.Range("A2:C2"))
End Sub

Download

Bitte den Haftungsausschluss im Impressum beachten.

quickperm.xlsm [22 KB Excel Datei, ohne jegliche Gewährleistung]