How to generate all the possible arrangements of 1,2,3
(for eg:..123,132,231,231,312,321.) using VB.net
i should get an generalised solution suitable for n numbers
How to generate all the possible arrangements of 1,2,3
(for eg:..123,132,231,231,312,321.) using VB.net
i should get an generalised solution suitable for n numbers
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Sep 24, 2008, 3:32 PM
This code will generate and display all possible arrangements of the numbers 1,2,3:
Imports System
Imports System.Collections.Generic
Module Permutations
Sub Main()
Dim perms As New List(Of Integer())
For i As Integer = 1 To 3
For j As Integer = 1 To 3
If i = j Then Continue For
For k As Integer = 1 To 3
If k <> i AndAlso k <> j Then
perms.Add(New Integer(2){i,j,k})
End If
Next k
Next j
Next i
Console.WriteLine("All possible arrangements of {1,2,3} are:")
Console.WriteLine()
For Each perm As Integer() in perms
Console.Write("{")
For i As Integer = 0 To 2
Console.Write(perm(i))
If i < 2 Then Console.Write(",")
Next i
Console.WriteLine("}")
Next perm
End Sub
End Module
However, as this sounds like homework, you'll have to do the general case of 'n' numbers yourself to get some benefit from this. To avoid having 'n' For statements, I'd look for a recursive solution.