how to generate the permutation of any set of numbers
11,22,23,45,34
without repetition in vb.net
how to generate the permutation of any set of numbers
11,22,23,45,34
without repetition in vb.net
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ashley Argile97 reputationPosted Oct 8, 2008, 5:08 AM EDT
I know you wanted it in VB but I'm a C# guy primarily and nobody else has responded. I'll post a VB version this evening (UK time), just thought if you wanted it quick that you might be able to convert it.
public List
{
List
permuteWorker(values, 0, values.Length, ref permList);
return permList;
}
private void permuteWorker(int[] values, int start, int n, ref List
{
if (start == n - 1)
{
int[] thisValues = new int[values.Length] ;
values.CopyTo(thisValues, 0);
permList.Add(thisValues);
}
else
{
for (int i = start; i < n; i++)
{
int tmp = values[i];
values[i] = values[start];
values[start] = tmp;
permuteWorker(values, start + 1, n, ref permList);
values[start] = values[i];
values[i] = tmp;
}
}
}
Regards
Ashley
Ashley Argile97 reputationPosted Oct 8, 2008, 1:41 PM EDT
Here's the VB version as promised. Sorry about the quality of the code but as I mentioned in my last post I'm a C# developer. Feel free to refactor! I've included a Main() so that you can see the output.
Imports
System.Collections.GenericModule
Module1 Sub Main() Dim values() As Integer = {1, 3, 5} Dim permList As New List(Of Integer())permList = Permute(values)
Dim perm As Integer() For Each perm In permList Dim output As String = String.Empty Dim value As Integer For Each value In permoutput += value.ToString
Console.WriteLine(output)
Next NextConsole.ReadLine()
End Sub Function Permute(ByVal values() As Integer) As List(Of Integer()) Dim permList As New List(Of Integer())PermuteWorker(values, 0, values.Length, permList)
Return permList End Function Sub PermuteWorker(ByVal values() As Integer, ByVal start As Integer, ByVal n As Integer, ByRef permList As List(Of Integer())) If start = n - 1 Then Dim thisValues(values.Length) As Integervalues.CopyTo(thisValues, 0)
permList.Add(thisValues)
Else Dim i As Integer For i = start To n - 1 Dim tmp As Integer = values(i)values(i) = values(start)
values(start) = tmp
PermuteWorker(values, start + 1, n, permList)
values(start) = values(i)
values(i) = tmp
Next End If End SubEnd
ModuleRegards
Ashley