From 3dd6628718c03b3021633c7e7b43238502a166ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 23 May 2014 08:39:01 +0200 Subject: [PATCH] =?UTF-8?q?Test=20f=C3=BCr=20Sortieralgorightmus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sorttest.cs | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ inf3.csproj | 1 + 2 files changed, 93 insertions(+) create mode 100644 Sorttest.cs diff --git a/Sorttest.cs b/Sorttest.cs new file mode 100644 index 0000000..40e4edd --- /dev/null +++ b/Sorttest.cs @@ -0,0 +1,92 @@ +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 sorted = getList(i, 0); + for (int listNo = 0;listNo < fac(i);listNo++) + { + List listToSort = getList (i, listNo); + Quicksort.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 l1, List 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 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 getList (int length, int listNum) + { + List availableNums = new List (length); + for (int i = 1; i <= length; i++) { + availableNums.Add(i); + } + List result = new List(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; + } + } +} \ No newline at end of file diff --git a/inf3.csproj b/inf3.csproj index 425e076..53d5c28 100644 --- a/inf3.csproj +++ b/inf3.csproj @@ -113,6 +113,7 @@ Form +