public void Sort_TwoElements_ShouldSort() { var elements = new List<int> { 534, 63}; var sorted = new QuickSort<int>().Sort(elements, Comparer<int>.Default); CollectionAssert.AreEqual(new List<int> { 63, 534 }, sorted); }
public void Sort_SingleElement_ShouldReturnSingleElement() { var elements = new List<int> { 42 }; var sorted = new QuickSort<int>().Sort(elements, Comparer<int>.Default); CollectionAssert.AreEqual(new List<int> { 42 }, sorted); }
private static void TestQuickSort(int[] a) { Random r = new Random(10000); a = new int[1000]; for(int i = 0; i < a.Length; i++) { a[i] = i;// a.Length - i; } QuickSort<int> qs = new QuickSort<int>(); qs.Sort(a, SortDirection.Ascending); //DisplayArray(a); System.Console.WriteLine("Comparison in sorted array : {0}", qs.ComparisonCount); for (int i = 0; i < a.Length; i++) { int j = r.Next(i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } //DisplayArray(a); qs.Sort(a, SortDirection.Ascending); //DisplayArray(a); System.Console.WriteLine("Comparison in random array : {0}", qs.ComparisonCount); DisplaySeparator(); }
public void Sort_EmptyList_ShouldReturnEmptyList() { var elements = new List<int>(); var sorted = new QuickSort<int>().Sort(elements, Comparer<int>.Default); CollectionAssert.AreEqual(new List<int>(), sorted); }
static void Main(string[] args) { QuickSort quickSort = new QuickSort(); quickSort.Run(); Console.ReadLine(); }
public static ulong QuickSortAndCountComparisons(int[] array) { QuickSort qs = new QuickSort(); qs.Sort(ref array, 0, array.Length - 1); Console.WriteLine(string.Format("ComparisonCount: {0}", qs.ComparisonCount)); return qs.ComparisonCount; }
public void Sort_SimpleList_ShouldSort() { var elements = new List<int> {42, 534, 63, 3, 2, 23, 1}; var sorted = new QuickSort<int>().Sort(elements, Comparer<int>.Default); CollectionAssert.AreEqual(new List<int>{1,2,3,23,42,63,534}, sorted); }
public void CanSortUsingQuickSort(int[] input, int[] expected) { var sut = new QuickSort(); var actual = sut.Sort(input); Assert.Equal(expected, actual); }
public static Sort GetSort() { // make the chain of invocations var selectSort = new SelectionSort(); var quickSort = new QuickSort(); selectSort.NextInChain = quickSort; return selectSort; }
// END CUT HERE // BEGIN CUT HERE public static void Main() { try { QuickSort ___test = new QuickSort(); ___test.run_test(-1); } catch(Exception e) { //Console.WriteLine(e.StackTrace); Console.WriteLine(e.ToString()); } }
public void BasicSort() { var qSort = new QuickSort<int>(12); var gen = new Random(15); foreach(var N in new[]{1, 3 , 4,5}) { var array1 = Enumerable.Range(0, N).Select(i => gen.Next(int.MaxValue)).ToArray(); qSort.Sort(array1); Assert.IsTrue(array1.IsSorted(),"#A01 " + N); } }
static void Main() { int[] arr = { 2, 14, 12, 8, 6, 9, 15, 4, 5, 1, 3, 11 }; QuickSort qs = new QuickSort(); qs.quicksort(arr, 0, arr.Length - 1); foreach (int x in arr) { Console.Write(x + " "); } Console.WriteLine(); }
public void IsSortedTest() { Random rnd = new Random(); const int n = 1000; int[] array = new List<int>(Enumerable.Range(1, n).Select(v => rnd.Next())).ToArray(); QuickSort<int> sort = new QuickSort<int>(); array = sort.Sort(array); Assert.IsTrue(TestHelpers<int>.IsSorted(array)); }
public void TestSort() { int[] inputArr1 = new int[] { 3, 4, 12, 15, 8, 6 }; int[] expected1 = new int[] { 3, 4, 6, 8, 12, 15 }; int[] inputArr2 = new int[] { 3, 2, 2, 2, 2, 2 }; int[] expected2 = new int[] { 2, 2, 2, 2, 2, 3 }; int[] inputArr3 = new int[] { 10000, 1000, 500, 200, 100, -100 }; int[] expected3 = new int[] { -100, 100, 200, 500, 1000, 10000 }; QuickSort<int> quickSort = new QuickSort<int>(); TestArray(expected1, quickSort.Sort(inputArr1)); TestArray(expected2, quickSort.Sort(inputArr2)); TestArray(expected3, quickSort.Sort(inputArr3)); }
public void EmptySort() { //Test empty array var qSort = new QuickSort<MockComparable>(3); qSort.Sort(new MockComparable[]{}); bool isSuccess = false; try { qSort.Sort(null); } catch(ArgumentNullException) { isSuccess = true; } Assert.IsTrue(isSuccess, "#Argument null exception should be thrown"); }
public static Sorter displayMenu(Sorter aSorter) { Console.WriteLine("Select Options"); Console.WriteLine("1- Bubble Sort"); Console.WriteLine("2- Insertion Sort"); Console.WriteLine("3- Quick Sort"); String selectionString = Console.ReadLine(); int InputNumber; int.TryParse(selectionString, out InputNumber); switch (InputNumber) { case 1: aSorter = new BubbleSort(); break; case 2: aSorter = new InsertionSort(); break; case 3: aSorter = new QuickSort(); break; } return aSorter; }
public void QuickSortTest() { // Random elements var sorter = new QuickSort<int>(); shuffledList = new List<int>(shuffledList); sorter.Sort(shuffledList);//new List<int>() { 12, 7, 14, 9, 10, 11 }); var temp = shuffledList.ToArray(); Array.Sort(temp); CollectionAssert.AreEqual(temp, shuffledList); //one element var collection = new[] { 0 }; sorter.Sort(collection); temp = collection.ToArray(); Array.Sort(temp); CollectionAssert.AreEqual(temp, collection); //zero elements collection = new int[0]; sorter.Sort(collection); temp = collection.ToArray(); Array.Sort(temp); CollectionAssert.AreEqual(temp, collection); //null elements collection = null; sorter.Sort(collection); CollectionAssert.AreEqual(null, collection); }
static void Main(string[] args) { int[] arr = new int[10000]; Random rnd = new Random(); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } ISort<int> s = new BubbleSort<int>(); DateTime dt = DateTime.Now; arr = s.Sorting(arr); Console.WriteLine("Time for BubbleSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new CocktailSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for CocktailSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new EvenOddSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for EvenOddSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new CombSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for CombSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new GnomeSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for GnomeSort is {0}.", DateTime.Now - dt); arr = new int[10000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new InsertionSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for InsertionSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new BinaryInsertionSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for BinaryInsertionSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new ShellSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for ShellSort is {0}.", DateTime.Now - dt); arr = new int[1000000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); } for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new HeapSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for HeapSort is {0}.", DateTime.Now - dt); int ddd = 0; for (int i = 0; i < arr.Length - 2; i++) { //Console.Write(arr[i] + " "); if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!"); ddd++; } Console.WriteLine("Error count: {0}", ddd); dt = DateTime.Now; s = new MergeSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for MergeSort is {0}.", DateTime.Now - dt); //StreamWriter sw = new StreamWriter("C:/Users/suvorovi/1.txt"); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); //sw.Write(arr[i] + " "); } //sw.WriteLine(""); dt = DateTime.Now; s = new QuickSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for QuickSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); //sw.Write(arr[i] + " "); } //sw.WriteLine(""); dt = DateTime.Now; s = new TimSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for TimSort is {0}.", DateTime.Now - dt); ddd = 0; for (int i = 0; i < arr.Length - 2; i++) { //Console.Write(arr[i] + " "); if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!"); ddd++; } Console.WriteLine("Error count: {0}", ddd); Console.ReadLine(); //sw.Close(); }
public void QuickSortTest() { //Arrange var quickSort = new QuickSort<int>(); var arrayToSort = new []{ 19, 42, 25, 17, 10, 73, 13, 88, 80, 91, 18, 50 }; //_arrayToSort.CopyTo(arrayToSort, 0); //Act arrayToSort.Sort(quickSort); //Assert Assert.IsTrue(arrayToSort.IsSorted()); }
public void Initialize() { sorter = new QuickSort<int>(); }
public static void QuickSort(int[] a, SortDirection direction) { QuickSort<int> qs = new QuickSort<int>(); qs.Sort(a, direction); }