Advertisement
stanevplamen

02.01.19.Permutations

Jul 2nd, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Permutations
  5. {
  6.     class Permute
  7.     {
  8.         private void swap(ref char a, ref char b)
  9.         {
  10.             if (a == b) return;
  11.             a ^= b;
  12.             b ^= a;
  13.             a ^= b;
  14.         }
  15.  
  16.         public void Set_Permutation(char[] list)
  17.         {
  18.             int arrayLength = list.Length - 1;
  19.             Permutation_Method(list, 0, arrayLength);
  20.         }
  21.  
  22.         private void Permutation_Method(char[] list, int k, int m)
  23.         {
  24.             // k intial index passed
  25.             // m size of char array
  26.             int i;
  27.             if (k == m)  
  28.             {
  29.                 Console.Write(list);
  30.                 Console.WriteLine(" ");
  31.             }
  32.             else
  33.             {
  34.                 for (i = k; i <= m; i++)
  35.                 {
  36.                     swap(ref list[k], ref list[i]);  
  37.  
  38.                     //recursive call
  39.                     Permutation_Method(list, k + 1, m);
  40.  
  41.                     swap(ref list[k], ref list[i]);  
  42.                 }
  43.             }
  44.         }
  45.     }
  46.  
  47.     class Class1
  48.     {
  49.         static void Main()
  50.         {
  51.             Permute objPermutation = new Permute();
  52.             string str = "123";
  53.             char[] mycharArray = str.ToCharArray();
  54.             /*calling the permute*/
  55.             objPermutation.Set_Permutation(mycharArray);
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement