Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Permutations
- {
- static void Print(int[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- {
- Console.Write(arr[i] + " ");
- }
- Console.WriteLine();
- }
- static void Permute(int[] arr, int index,bool[] usedPositions,int[] result)
- {
- if (index == arr.Length)
- {
- Print(result);
- }
- else
- {
- for (int i = 0; i < arr.Length; i++)
- {
- if (usedPositions[i])
- {
- continue;
- }
- result[index] = arr[i];
- usedPositions[i] = true;
- Permute(arr, index + 1, usedPositions, result);
- usedPositions[i] = false;
- }
- }
- }
- static void Main()
- {
- int[] arr = new int[] { 1, 2, 3, 2 };
- int[] result = new int[arr.Length];
- bool[] usedPositions = new bool[arr.Length];
- Permute(arr, 0,usedPositions,result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement