using System;
using System.Collections.Generic;
class PermutationsOfWordsWithoutRepetition_CSharp
{
static void Main()
{
string[] words = { "word-1", "word-2", "word-3", "word-4" };
var result = GeneratePermutations(words, 0, words.Length - 1);
foreach (var permutation in result) {
Console.WriteLine(string.Join(" ", permutation));
}
}
static List<string[]> GeneratePermutations(string[] words, int start, int end) {
var result = new List<string[]>();
if (start == end) {
result.Add((string[])words.Clone());
}
else {
for (int i = start; i <= end; i++) {
Swap(ref words[start], ref words[i]);
result.AddRange(GeneratePermutations(words, start + 1, end));
Swap(ref words[start], ref words[i]);
}
}
return result;
}
static void Swap(ref string a, ref string b) {
string temp = a;
a = b;
b = temp;
}
}
/*
run:
word-1 word-2 word-3 word-4
word-1 word-2 word-4 word-3
word-1 word-3 word-2 word-4
word-1 word-3 word-4 word-2
word-1 word-4 word-3 word-2
word-1 word-4 word-2 word-3
word-2 word-1 word-3 word-4
word-2 word-1 word-4 word-3
word-2 word-3 word-1 word-4
word-2 word-3 word-4 word-1
word-2 word-4 word-3 word-1
word-2 word-4 word-1 word-3
word-3 word-2 word-1 word-4
word-3 word-2 word-4 word-1
word-3 word-1 word-2 word-4
word-3 word-1 word-4 word-2
word-3 word-4 word-1 word-2
word-3 word-4 word-2 word-1
word-4 word-2 word-3 word-1
word-4 word-2 word-1 word-3
word-4 word-3 word-2 word-1
word-4 word-3 word-1 word-2
word-4 word-1 word-3 word-2
word-4 word-1 word-2 word-3
*/