public IChromosome GetSample() { //TODO: Unit tests! var length = Variances.Length; var positions = new List <PositionValuePair <double> >(); for (int i = 0; i < length; i++) { var mean = Variances[i].Mean; var deviation = Variances[i].Deviation; var position = Random.GetGaussian(mean, deviation); positions.Add(new PositionValuePair <double>(position, i)); } var quickSorter = new QuickSorter <PositionValuePair <double> >(); quickSorter.Sort(positions, PositionValuePair <double> .ComparerByPosition.Instance, 0, length - 1); var genes = new int[length]; for (int i = 0; i < length; i++) { genes[i] = positions[i].Value; } return(new PermutationChromosome(genes)); }
public void When_sorting(int[] input, int[] answer) { var sorter = new QuickSorter <int>(); var sorted = sorter.Sort(input); Assert.Equal(sorted, answer); }
public void TestQuickSort2() { QuickSorter qs = new QuickSorter(); int[] arr = new int[] { }; qs.QuickSort(ref arr, 0, arr.Length - 1); arr = new int[] { 1 }; qs.QuickSort(ref arr, 0, arr.Length - 1); arr.AreEqual(new int[] { 1 }); arr = new int[] { 1, 1, 1, 1, 1, 1 }; qs.QuickSort(ref arr, 0, arr.Length - 1); arr.AreEqual(new int[] { 1, 1, 1, 1, 1, 1 }); arr = new int[] { 14, 16, 5, 9, 2, 7, 1, 13 }; qs.QuickSort(ref arr, 0, arr.Length - 1); arr.AreEqual(new int[] { 1, 2, 5, 7, 9, 13, 14, 16 }); arr = new int[] { 1, 1, 2, 3, 4, 5 }; qs.QuickSort(ref arr, 0, arr.Length - 1); arr.AreEqual(new int[] { 1, 1, 2, 3, 4, 5 }); arr = new int[] { 5, 3, 2, 7, 1, 6, 4, 9 }; qs.QuickSort(ref arr, 0, arr.Length - 1); arr.AreEqual(new int[] { 1, 2, 3, 4, 5, 6, 7, 9 }); arr = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 }; qs.QuickSort(ref arr, 0, arr.Length - 1); arr.AreEqual(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); }
public void searchMember() { string input; do { Console.Write("\t\t\t\tInput: "); input = Console.ReadLine().Trim(); } while (String.IsNullOrEmpty(input)); bool longInput = long.TryParse(input, out long result); if (longInput) { var sortedList = QuickSorter.QuickSort(Members.ToArray(), 0, Members.Count - 1, o => o.SocialSecurityNr); var searchResult = LinearSearch(sortedList, o => o.SocialSecurityNr, result); UI.PrintMembers("\t\tSearch Result (Social Security Number)", searchResult); } else { var sortedList = QuickSorter.QuickSort(Members.ToArray(), 0, Members.Count - 1, o => o.LastName); var searchResult = LinearSearch(sortedList, o => o.LastName.ToLower(), input.ToLower()); UI.PrintMembers("\t\t Search Result (Last Name)", searchResult); } }
public void Uneven() { var players = new[] { 1, 10, 20, 3, 4, 7, 6, 5, 11, 13, 12, 14, 17, 16, 2, 19, 8, 9, 15, 18 }; Sorter <int> sorter = new QuickSorter(); sorter.Sort(players); // Assert int bestOfWeakest = 0; var half = players.GetLength(0) / 2 - 1; for (int i = 0; i < half; i++) { if (players[i] > bestOfWeakest) { bestOfWeakest = players[i]; } } for (int i = half + 1; i < players.GetLength(0) - 1; i++) { Assert.IsTrue(players[i] >= bestOfWeakest); } }
public void ThrowInvalidOperationException_WhenCollectionIsEmpty() { var collection = new List <int>(); var sorter = new QuickSorter <int>(); Assert.Throws <InvalidOperationException>(() => sorter.Sort(collection)); }
public void Test_QuickSorter_ShouldThrowExceptionWhenCollectionIsNull() { var sorter = new QuickSorter <int>(); var emptyCollection = new SortableCollection <int>(null); emptyCollection.Sort(sorter); }
public void QuickSortTest(int[] unsorted, int[] expected, int[] sorted, QuickSorter quickSorter) { "Given an unsorted array" .x(() => { }); "And a QuickSorter" .x(() => { quickSorter = new QuickSorter(); }); "When I QuickSort it" .x(() => { sorted = quickSorter.Sort(unsorted); }); "Then it is sorted as I expect" .x(() => { Xunit.Assert.Equal(expected, sorted); }); }
public void Sort_NullArray_ReturnArgumentException() { int[] array = null; var sorter = new QuickSorter <int>(); sorter.Sort(array); }
/// <summary> /// Sorts the specified list. /// </summary> /// <typeparam name="T">The type of item in the list.</typeparam> /// <param name="list">The list.</param> /// <param name="sortOrder">The order in which to sort the list.</param> public static void Sort <T>(this IList <T> list, SortOrder sortOrder) { Guard.ArgumentNotNull(list, "list"); var sorter = new QuickSorter <T>(); sorter.Sort(list, sortOrder); }
public void QuickSortTest() { int[] arr = { 1, 2, 8, 7, 4, 3, 5, 6 }; var qs = new QuickSorter(); qs.QuickSort(arr); Assert.AreEqual(arr[7], 8); }
public void SortByLastName() { Console.Clear(); UI.PrintMembers("\t\t\t Unsorted", Members); var sortedList = QuickSorter.QuickSort(Members.ToArray(), 0, Members.Count - 1, o => o.LastName); UI.PrintMembers("\t\t\t Sorted (Last Name)", sortedList.ToList()); }
public void SortByEldest() { Console.Clear(); UI.PrintMembers("\t\t\t Unsorted", Members); var sortedList = QuickSorter.QuickSort(Members.ToArray(), 0, Members.Count - 1, o => o.SocialSecurityNr); UI.PrintMembers("\t\t\t Sorted (Age)", sortedList.ToList()); }
public void QuickSortingTest(int[] heap, int[] expectedArray) { ISortingService sorter = new QuickSorter(); sorter.Sort(heap); Assert.Equal(heap, expectedArray); }
public void Test1() { var list = new List <int> { 5, 8, 4, 1, 2, 6, 4, 7, 9, 2 }; QuickSorter.QuickSort <int>(list, Comparer <int> .Default); }
public void NaiveQuickSorter_SortStringsDescending_SortsCorrectly(IList <string> actual) { var sorter = new QuickSorter <string>(); sorter.Sort(actual, SortOrder.Descending); SortAssert.IsSortedDescending(actual, Comparer <string> .Default); }
public void PassingOneItemCollection() { List<int> numbers = new List<int>() {1}; QuickSorter<int> sorter = new QuickSorter<int>(numbers); List<int> sortedList = new List<int>(sorter.Sort()); Assert.AreEqual(numbers[0], sortedList[0]); }
public static void Main() { List<int> numbers = new List<int>() { 1,3,4,5,3,5,7,8,4,3,23,2 }; QuickSorter<int> quickSorter = new QuickSorter<int>(numbers); numbers = quickSorter.Sort(); Console.WriteLine(string.Join(",", numbers)); }
static void Main(string[] args) { try { while (true) { /* 예제 입력 * 3 * 1 2 3 * 9 * 1 1 1 1 2 2 2 2 2 */ /* 예제 출력 * 1 3 2 * 2 2 2 2 2 1 1 1 1 */ string lFirstLine = Console.ReadLine(); //테스트 케이스 수 int lCnt_TestCase = Convert.ToInt32(lFirstLine); if (lCnt_TestCase <= 50) { if (lCnt_TestCase < 1) { Console.WriteLine("프로그램을 종료합니다."); break; } string lSecondLine = Console.ReadLine(); int[] lParams = lSecondLine.Split(new char[] { ' ' }) .Select(x => Convert.ToInt32(x)) .ToArray(); Sorter lSorter = new QuickSorter(lParams); //정렬 실행 lSorter.Execute(); //프린트 lSorter.Print(); // 1. 입력받은 순서대로 데이터를 처리. // 1. 입력받은 값을 이진 탐색 트리에 넣는다. // 2. 연속된 숫자가 아니면서 가작 작은 수를 // 차례대로 결과 배열에 넣는다. } else { Console.WriteLine("첫번째 라인의 숫자는 50보다 작거나 같아야 합니다."); } } } catch (Exception lEx) { Console.WriteLine(lEx.Message); } }
/// <summary> /// Sorts the specified list. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">The list.</param> /// <param name="comparison">The comparison.</param> /// <param name="sortOrder">The order in which to sort the list.</param> public static void Sort <T>(this IList <T> list, Comparison <T> comparison, SortOrder sortOrder) { Guard.ArgumentNotNull(list, "list"); Guard.ArgumentNotNull(comparison, "comparison"); var sorter = new QuickSorter <T>(); sorter.Sort(list, comparison, sortOrder); }
public void ShouldSortEmptyArray() { var sorter = new QuickSorter <int>(); int[] items = { }; sorter.QuickSort(items); Assert.IsEmpty(items); }
public void Sort_hugeArrayTest() { int[] unsortedArray_1 = GenerateArray(); int[] unsortedArray_2 = new int[1000]; unsortedArray_1.CopyTo(unsortedArray_2, 0); Array.Sort(unsortedArray_1); QuickSorter.Sort(unsortedArray_2); CollectionAssert.AreEqual(unsortedArray_1, unsortedArray_2); }
public void ShouldSortMultipleItems() { var sorter = new QuickSorter <int>(); int[] items = { 8, 3, 4, 12, 73, 1, 3, 4, 5 }; sorter.QuickSort(items); Assert.AreEqual(new int[] { 1, 3, 3, 4, 4, 5, 8, 12, 73 }, items); }
/// <summary> /// Sorts the specified list. /// </summary> /// <typeparam name="T">The type of item in the list.</typeparam> /// <param name="list">The list.</param> /// <param name="comparer">The comparer.</param> public static void Sort <T>(this IList <T> list, IComparer <T> comparer) { Guard.ArgumentNotNull(list, "list"); Guard.ArgumentNotNull(comparer, "comparer"); var sorter = new QuickSorter <T>(); sorter.Sort(list, comparer); }
private void PopulateInvokerTree( RunInvokerTree tree, RunInvokerVertex parent, Type t ) { // Try and find setup method MethodRunInvoker setup = null; if (TypeHelper.HasMethodCustomAttribute(t, typeof(SetUpAttribute))) { setup = new MethodRunInvoker(this, TypeHelper.GetAttributedMethod(t, typeof(SetUpAttribute))); } // Gather all methods, with order. ArrayList methods = new ArrayList(); foreach (MethodInfo mi in TypeHelper.GetAttributedMethods(t, this.AttributeType)) { // get sequence attribute TestSequenceAttribute seq = (TestSequenceAttribute)TypeHelper.GetFirstCustomAttribute(mi, typeof(TestSequenceAttribute)); methods.Add(new OrderedMethod(mi, seq.Order)); } // sort the methods QuickSorter sorter = new QuickSorter(); sorter.Sort(methods); // Try and find teardown method MethodRunInvoker teardown = null; if (TypeHelper.HasMethodCustomAttribute(t, typeof(TearDownAttribute))) { teardown = new MethodRunInvoker(this, TypeHelper.GetAttributedMethod(t, typeof(TearDownAttribute))); } // populate execution tree. RunInvokerVertex child = parent; foreach (OrderedMethod om in methods) { if (setup != null) { child = tree.AddChild(child, setup); } child = tree.AddChild(child, InstanceInvoker(om.Method)); if (teardown != null) { child = tree.AddChild(child, teardown); } } }
public void SortEmptyTest() { List <int> testCollection = new List <int>(); QuickSorter <int> testQuickSorter = new QuickSorter <int>(); testQuickSorter.Sort(testCollection); Assert.AreEqual(testCollection.Count, 0); }
public void ShouldSortTwoItems() { var sorter = new QuickSorter <int>(); int[] items = { 8, 3 }; sorter.QuickSort(items); Assert.AreEqual(new int[] { 3, 8 }, items); }
static void Main(string[] args) { int[] a = { 13, 88, 37, 44, 69, 74, 59, 23 }; QuickSorter.Sort(a, 0, a.Length - 1); foreach (int element in a) { Console.Write(element + " "); } }
public void PassingSortedItemCollection() { List<int> sortedNumbers = new List<int>() { 1, 1, 2, 2, 2, 3, 4, 5, 5, 7, 8, 9 }; QuickSorter<int> sorter = new QuickSorter<int>(sortedNumbers); List<int> sortedList = new List<int>(sorter.Sort()); for (int i = 0; i < sortedNumbers.Count; i++) { Assert.AreEqual(sortedNumbers[i], sortedList[i]); } }
public void ShouldSortNegativeNumbersCorrectly() { var array = new int[] { -103, -166, -42, -3, -1 }; var mergeSorter = new QuickSorter <int>(); mergeSorter.Sort(array); var expected = new int[] { -166, -103, -42, -3, -1 }; CollectionAssert.AreEqual(expected, array); }
public void ShouldSortNumbersCorrectly() { var array = new int[] { 1, 4, 100, -3, 1 }; var mergeSorter = new QuickSorter <int>(); mergeSorter.Sort(array); var expected = new int[] { -3, 1, 1, 4, 100 }; CollectionAssert.AreEqual(expected, array); }
public void NaiveQuickSorter_EmptyList_RemainsEmptyList() { var expected = new List <string>(); var sorter = new QuickSorter <string>(); var actual = new List <string>(); sorter.Sort(actual); Assert.AreEqual(expected, actual); }
public void aaa(int[] numbers) { var quickSorter = new QuickSorter(); var expectedResult = new List <int>(numbers); expectedResult.Sort(); var result = quickSorter.SortNumbers(numbers); Assert.Equal(expectedResult, result); }
public void SortExample() { var sorter = new QuickSorter<int>(); var list = new List<int> {13, 5, 77, 9, 12}; sorter.Sort(list, Comparer<int>.Default); Assert.AreEqual(5, list[0]); Assert.AreEqual(9, list[1]); Assert.AreEqual(12, list[2]); Assert.AreEqual(13, list[3]); Assert.AreEqual(77, list[4]); }
public void SortListOrderExample() { ComparisonSorter<int> sorter = new QuickSorter<int>(); var list = new List<int> {13, 5, 77, 9, 12}; sorter.Sort(list, SortOrder.Ascending); Assert.AreEqual(77, list[4]); Assert.AreEqual(13, list[3]); Assert.AreEqual(12, list[2]); Assert.AreEqual(9, list[1]); Assert.AreEqual(5, list[0]); }
public void SortListDelegateExample() { ComparisonSorter<int> sorter = new QuickSorter<int>(); var list = new List<int> {13, 5, 77, 9, 12}; sorter.Sort(list, IntComparison); Assert.AreEqual(5, list[0]); Assert.AreEqual(9, list[1]); Assert.AreEqual(12, list[2]); Assert.AreEqual(13, list[3]); Assert.AreEqual(77, list[4]); }
static void SortStrings() { Console.WriteLine("对字符串数组排序:"); string[] strArray = { "zpasd", "qwe", "wer", "fgdg", "ertw", "qrtre" }; QuickSorter<string> strSorter = new QuickSorter<string>(strArray, CompareString); Console.WriteLine("排序之前的字符串数组:"); foreach ( string str in strArray ) { Console.Write(str + " "); } Console.WriteLine("\n排序之后的字符串数组:"); foreach ( string str in strSorter.ArrayAfterSort() ) { Console.Write(str + " "); } }
static void SortInts() { Console.WriteLine("对整型数组排序:"); int[] intArray = { 12, 2, 34, 15, 23, 11, 6, 9, 11 }; QuickSorter<int> intSorter = new QuickSorter<int>(intArray, CompareInt); Console.WriteLine("排序之前的整型数组:"); foreach ( int num in intArray ) { Console.Write(num + " "); } Console.WriteLine("\n排序之后的整型数组:"); foreach ( int num in intSorter.ArrayAfterSort() ) { Console.Write(num + " "); } }
public void TestQuickSorter() { int[] s = new int[6]; s[0] = 3; s[1] = 2; s[2] = 4; s[3] = 1; s[4] = 0; s[5] = 5; QuickSorter<int> ms = new QuickSorter<int>(); int[] sorted_s = ms.Sort(s); Assert.AreEqual(sorted_s[0], 0); Assert.AreEqual(sorted_s[1], 1); Assert.AreEqual(sorted_s[2], 2); Assert.AreEqual(sorted_s[3], 3); Assert.AreEqual(sorted_s[4], 4); Assert.AreEqual(sorted_s[5], 5); }
/// <summary> /// Creates the sort algorithm implementation. /// </summary> /// <param name="algorithm">The algorithm.</param> /// <returns></returns> internal static ISortAlgorithm CreateSortAlgorithmImplementation(SortAlgorithm algorithm) { ISortAlgorithm toReturn = null; switch(algorithm) { case SortAlgorithm.SelectionSort: toReturn = new SelectionSorter(); break; case SortAlgorithm.ShellSort: toReturn = new ShellSorter(); break; case SortAlgorithm.QuickSort: toReturn = new QuickSorter(); break; } return toReturn; }
public void Test_QuickSorter_ShouldSortCorrectly() { var quickSorter = new QuickSorter<int>(); collection.Sort(quickSorter); bool isSortedCorrectly = true; for (int i = 0; i < collection.Items.Count - 1; i++) { if (collection.Items[i].CompareTo(collection.Items[i + 1]) > 0) { isSortedCorrectly = false; break; } } Assert.IsTrue(isSortedCorrectly, "QuickSorter should sort correctly."); }
public void PassingNullCollection() { QuickSorter<int> sorter = new QuickSorter<int>(null); }
public void Test_QuickSorter_ShouldThrowExceptionWhenCollectionIsNull() { var sorter = new QuickSorter<int>(); var emptyCollection = new SortableCollection<int>(null); emptyCollection.Sort(sorter); }
public void PassingEmptyCollection() { QuickSorter<int> sorter = new QuickSorter<int>(new List<int>()); }