using System;
using System.Collections.Generic;


namespace WorldOfPeacecraft
{
	class Sorttest
	{
		public static void testSort()
		{
			for (int i = 1;i <= 11;i++)
			{
				Console.WriteLine ("Teste Listen länge " + i);
				Console.WriteLine ();
				List<int> sorted = getList(i, 0);
				for (int listNo = 0;listNo < fac(i);listNo++)
				{
					List<int> listToSort = getList (i, listNo);
					Quicksort<int>.sort(listToSort, intIsSmaller);
					if (!listsEqual(listToSort, sorted))
					{
						Console.WriteLine("Incorrectly sorted list");
						printList(getList(i, listNo));
						Console.WriteLine("Sorting result:");
						printList (listToSort);
						Console.WriteLine("Expected result:");
						printList (sorted);
						Console.WriteLine();
					}
				}
			}
		}

		static bool listsEqual (List<int> l1, List<int> l2)
		{
			if (l1.Count != l2.Count)
				return false;
			for (int i = 0; i < l1.Count; i++) {
				if (l1[i] != l2[i])
					return false;
			}
			return true;
		}

		static void printList (List<int> l)
		{
			foreach (int zahl in l) {
				Console.Write(zahl + ", ");
			}
			Console.WriteLine();
		}

		public static bool intIsSmaller(int i1, int i2)
		{
			return i1 < i2;
		}

		public static List<int> getList (int length, int listNum)
		{
			List<int> availableNums = new List<int> (length);
			for (int i = 1; i <= length; i++) {
				availableNums.Add(i);
			}
			List<int> result = new List<int>(length);
			for (int pos = 0;pos < length;pos++) {
				int nextNo;
				if (pos == length - 1)
				{
					nextNo = 0;
				}
				else
				{
					int divider = fac (length - pos - 1);
					nextNo = listNum / divider;
					listNum %= divider;
				}
				result.Add(availableNums[nextNo]);
				availableNums.RemoveAt(nextNo);
			}
			return result;
		}

		public static int fac (int nr)
		{
			int result = 1;
			for (int i = 2; i <= nr; i++) {
				result *= i;
			}
			return result;
		}
	}
}