Advertisement
d_brezoev

GetPermutations

Dec 19th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. class Permutations
  5. {  
  6.     static void Print(int[] arr)
  7.     {      
  8.         for (int i = 0; i < arr.Length; i++)
  9.         {
  10.             Console.Write(arr[i] + " ");
  11.         }
  12.         Console.WriteLine();
  13.     }
  14.     static void Permute(int[] arr, int index,bool[] usedPositions,int[] result)
  15.     {        
  16.         if (index == arr.Length)
  17.         {
  18.             Print(result);          
  19.         }
  20.         else
  21.         {
  22.             for (int i = 0; i < arr.Length; i++)
  23.             {
  24.                 if (usedPositions[i])
  25.                 {
  26.                     continue;                    
  27.                 }
  28.                 result[index] = arr[i];
  29.                 usedPositions[i] = true;
  30.                 Permute(arr, index + 1, usedPositions, result);
  31.                 usedPositions[i] = false;
  32.                
  33.             }
  34.         }
  35.     }
  36.     static void Main()
  37.     {
  38.         int[] arr = new int[] { 1, 2, 3, 2 };
  39.         int[] result = new int[arr.Length];
  40.         bool[] usedPositions = new bool[arr.Length];
  41.        
  42.         Permute(arr, 0,usedPositions,result);      
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement