public void TestWithAnEmptyCollection() { List<int> collection = new List<int>(); SelectionSorter<int> sorter = new SelectionSorter<int>(); sorter.Sort(collection); Assert.AreEqual(0, collection.Count); }
public void TestEmptyList() { var sorter = new SelectionSorter(); var list = new List<int> { }; sorter.Sort(list); Assert.AreEqual(0, list.Count); }
public void TestProperList() { var sorter = new SelectionSorter(); var list = new List<int> { 3, 2, 1, 9, 7 }; sorter.Sort(list); Assert.AreEqual(5, list.Count); Assert.IsTrue(list.SequenceEqual(new List<int> { 1, 2, 3, 7, 9 })); }
public void TestSelectionSortWithOneElement() { ISorter<int> sorter = new SelectionSorter<int>(); collection.Items = new List<int>() { 1 }; collection.Sort(sorter); Assert.AreEqual(collection.Items[0], 1); }
public void PassingOneItemCollection() { List<int> numbers = new List<int>() {1}; SelectionSorter<int> selectionSorter = new SelectionSorter<int>(numbers); List<int> sortedList = new List<int>(selectionSorter.Sort()); Assert.AreEqual(numbers[0], sortedList[0]); }
public static void Main() { List<int> numbers = new List<int>(){ 1,3,4,6,7,3,2,2,3,5,7,9,9,1 }; SelectionSorter<int> selectionSorter = new SelectionSorter<int>(numbers); numbers = selectionSorter.Sort(); Console.WriteLine(string.Join(",", numbers)); }
public void SelectionSortTestWithFilledArray() { SelectionSorter<int> selectionSorter = new SelectionSorter<int>(); IList<int> list = new List<int>() { 9, 3, 2, 1, 4, 6, 5, 0, 8, 7 }; IList<int> sortedList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; selectionSorter.Sort(list); Assert.AreEqual(string.Join(",",sortedList), string.Join(",",list)); }
public void SelectionSortTestWithOneMemberArray() { SelectionSorter<int> selectionSorter = new SelectionSorter<int>(); IList<int> list = new List<int>() { 1, }; IList<int> sortedList = new List<int>() { 1, }; selectionSorter.Sort(list); Assert.AreEqual(string.Join(",", sortedList), string.Join(",", list)); }
public void TestSelectionSortShouldReturnCorrectElements() { ISorter<int> sorter = new SelectionSorter<int>(); collection.Sort(sorter); Assert.AreEqual(collection.Items[0], 0); Assert.AreEqual(collection.Items[2], 22); Assert.AreEqual(collection.Items[collection.Items.Count - 1], 101); }
public void TestWithOneItemCollection() { List<int> collection = new List<int>(); collection.Add(3); SelectionSorter<int> sorter = new SelectionSorter<int>(); sorter.Sort(collection); Assert.AreEqual(1, collection.Count); Assert.AreEqual(3, collection[0]); }
public void AlreadySortedNumsTest() { List<int> quickSorter = new List<int>() { 1, 2, 3, 4, 5 }; SelectionSorter<int> selectionSorter = new SelectionSorter<int>(); List<int> expectedArray = new List<int>() { 1, 2, 3, 4, 5 }; selectionSorter.Sort(quickSorter); CollectionAssert.AreEqual(expectedArray, quickSorter); }
public void ReversedNumberSortTest() { List<int> arrayToSort = new List<int>() { 5, 4, 3, 2, 1 }; SelectionSorter<int> quickSorter = new SelectionSorter<int>(); List<int> selectionSorter = new List<int>() { 1, 2, 3, 4, 5 }; quickSorter.Sort(arrayToSort); CollectionAssert.AreEqual(selectionSorter, arrayToSort); }
public void SimpleQuickSortTest() { List<int> arrayToSort = new List<int>() { 2, 5, 3, 1, 4 }; SelectionSorter<int> selectionSorter = new SelectionSorter<int>(); List<int> expectedArray = new List<int>() { 1, 2, 3, 4, 5 }; selectionSorter.Sort(arrayToSort); CollectionAssert.AreEqual(expectedArray, arrayToSort); }
public void PassingSortedItemCollection() { List<int> sortedNumbers = new List<int>() { 1, 1, 2, 2, 2, 3, 4, 5, 5, 7, 8, 9 }; SelectionSorter<int> sorter = new SelectionSorter<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 TestSortLengthOfCollection() { List<int> collection = new List<int>() { 3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 }; int count = collection.Count; SelectionSorter<int> sorter = new SelectionSorter<int>(); sorter.Sort(collection); Assert.AreEqual(count, collection.Count); }
public void SortExample() { var sorter = new SelectionSorter<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 TestSortIsSortedWithListSort() { List<int> collection = new List<int>() { 3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 }; SelectionSorter<int> sorter = new SelectionSorter<int>(); sorter.Sort(collection); List<int> collection2 = new List<int>() { 3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 }; collection2.Sort(); CollectionAssert.AreEqual(collection2, collection); }
public void ScoreboardPrintScores_WhenNoScores_ShouldPrint1Message() { var printer = new Mock<IPrinter>(); var messagesCount = 0; printer.Setup(p => p.PrintLine(It.IsAny<string>())) .Callback(() => ++messagesCount); var sorter = new SelectionSorter(); var scoreDataManager = new TextFileScoreboardDataManager<Dictionary<string, int>>("../../test-score.txt"); var scoreboard = new Scoreboard(printer.Object, sorter, scoreDataManager); scoreboard.PrintScore(); // check printer.Messages // assert Assert.AreEqual(1, messagesCount); }
public void TestWithMultipleItems() { List<int> collection = new List<int>(); collection.Add(3); collection.Add(-111); collection.Add(0); collection.Add(24); collection.Add(-14); collection.Add(-1); collection.Add(32); collection.Add(-1); collection.Add(7); SelectionSorter<int> sorter = new SelectionSorter<int>(); sorter.Sort(collection); Assert.AreEqual(9, collection.Count); Assert.IsTrue(SortableCollection<int>.IsSorted(collection)); }
/// <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 TestSortIsSortedWithCheck() { List<int> collection = new List<int>() { 3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 }; SelectionSorter<int> sorter = new SelectionSorter<int>(); sorter.Sort(collection); bool isSorted = true; for (int i = 0; i < collection.Count - 1; i++) { if (collection[i] > collection[i + 1]) { isSorted = false; } } Assert.IsTrue(isSorted); }
public void TestSelectionSortNullComparer1() { SelectionSorter <int> sorter = new SelectionSorter <int>(); sorter.Sort(new List <int>(), (IComparer <int>)null); }
public void TestSelectionSortNullList3() { SelectionSorter <int> sorter = new SelectionSorter <int>(); sorter.Sort(null, SortOrder.Ascending); }
public void TestSelectionSortNullList2() { SelectionSorter <int> sorter = new SelectionSorter <int>(); sorter.Sort(null, Comparer <int> .Default); }
public void ExceptionNullComparer1() { var sorter = new SelectionSorter <int>(); sorter.Sort(new List <int>(), (IComparer <int>)null); }
public void SelectionSorter_NullList_ThrowsNullReferenceException() { var sorter = new SelectionSorter <string>(); Assert.Throws <NullReferenceException>(() => sorter.Sort(null)); }
public void Test_SelectionSorter_OneElementCollectionShouldBeStable() { var selectionSorter = new SelectionSorter<int>(); var smallCollection = new SortableCollection<int>(new List<int>() { 4 }); smallCollection.Sort(selectionSorter); bool isSortedCorrectly = smallCollection.Items[0] == 4; Assert.IsTrue(isSortedCorrectly, "SelectionSorter should be stable on sorting 1 element."); }
public void TestStringSelectionSorter_RandomNumberOfItems() { ISorter <string> sorter = new SelectionSorter <string>(); TestsHelper.TestStringSorter(sorter); }
public void ExceptionNullList1() { var sorter = new SelectionSorter<int>(); sorter.Sort(null); }
public void Test_SelectionSorter_ShouldThrowExceptionWhenCollectionIsNull() { var sorter = new SelectionSorter<int>(); var emptyCollection = new SortableCollection<int>(null); emptyCollection.Sort(sorter); }
public void Test_SelectionSorter_ShouldSortCorrectly() { var selectionSorter = new SelectionSorter<int>(); collection.Sort(selectionSorter); 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, "SelectionSorter should sort correctly."); }
public void Test_SelectionSorter_ShouldNotSwapWhenCollectionIsSorted() { var selectionSorter = new SelectionSorter<int>(); var smallCollection = new SortableCollection<int>(new List<int>() { 2, 4 }); smallCollection.Sort(selectionSorter); bool isSortedCorrectly = smallCollection.Items[0] == 2 && smallCollection.Items[1] == 4; Assert.IsTrue(isSortedCorrectly, "SelectionSorter should not swap when collection is sorted."); }
public void TestSelectionSorterWhenCollectionIsNull_ThrowsException() { ISorter <int> sorter = new SelectionSorter <int>(); TestsHelper.TestInt32SorterWhenCollectionIsNull(sorter); }
public void TestInt32SelectionSorter_RandomNumberOfItems() { ISorter <int> sorter = new SelectionSorter <int>(); TestsHelper.TestInt32Sorter(sorter); }
public void ExceptionNullList3() { var sorter = new SelectionSorter<int>(); sorter.Sort(null, SortOrder.Ascending); }
public void Simple() { var sorter = new SelectionSorter<int>(); TestSorter(sorter); }
public void PassingNullCollection() { SelectionSorter<int> selectionSorter = new SelectionSorter<int>(null); }
public void ExceptionNullList2() { var sorter = new SelectionSorter<int>(); sorter.Sort(null, Comparer<int>.Default); }
static void Main(string[] args) { Console.WriteLine("请选择算法:"); Console.WriteLine("1.选择排序"); Console.WriteLine("2.冒泡排序"); Console.WriteLine("3.高速排序"); Console.WriteLine("4.插入排序"); Console.WriteLine("5.希尔排序"); Console.WriteLine("6.归并排序"); Console.WriteLine("7.基数排序"); Console.WriteLine("8.计数排序"); Console.WriteLine("9.堆排序"); Console.WriteLine("0.退出"); int type = Convert.ToInt32(Console.ReadLine()); string typename = ""; switch (type) { case 1: //选择排序 typename = "选择排序"; break; case 2: //冒泡排序 typename = "冒泡排序"; break; case 3: //高速排序 typename = "高速排序"; break; case 4: //插入排序 typename = "插入排序"; break; case 5: //希尔排序 typename = "希尔排序"; break; case 6: //归并排序 typename = "归并排序"; break; case 7: //基数排序 typename = "基数排序"; break; case 8: //计数排序 typename = "计数排序"; break; case 9: //堆排序 typename = "堆排序"; break; default: typename = "您未选择算法"; break; } Console.WriteLine("您选择了{0}.{1}:", type, typename); int[] arrInt = new int[] { 4, 2, 7, 1, 8, 3, 9, 0, 5, 6 }; int[] intArray = new int[] { 5, 3, 7, 4, 8, 2, 9, 1, 0, 6 }; int[] newIntArray = intArray; switch (type) { case 1: //选择排序 SelectionSorter selSor = new SelectionSorter(); selSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 2: //冒泡排序 EbullitionSorter ebuSor = new EbullitionSorter(); ebuSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 3: //高速排序 QuickSorter quiSor = new QuickSorter(); quiSor.Sort(arrInt, 0, arrInt.Length - 1); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 4: //插入排序 InsertionSorter insSor = new InsertionSorter(); insSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 5: //希尔排序 ShellSorter sheSor = new ShellSorter(); sheSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 6: //归并排序 while (true) { Console.WriteLine("请选择:"); Console.WriteLine("1.归并排序(非递归)"); Console.WriteLine("2.归并排序(递归)"); Console.WriteLine("3.归并排序(自然合并)"); Console.WriteLine("4.退出"); int Arraynum = Convert.ToInt32(Console.ReadLine()); switch (Arraynum) { case 4: Environment.Exit(0); break; case 1: Console.WriteLine("Please Input Array Length"); int Leng271 = Convert.ToInt32(Console.ReadLine()); Function obj1 = new Function(Leng271); Console.WriteLine("The original sequence:"); Console.WriteLine(obj1); Console.WriteLine("'MergeSort' Finaly Sorting Result:"); obj1.ToMergeSort(); Console.WriteLine(obj1); break; case 2: Console.WriteLine("Please Input Array Length"); int Leng272 = Convert.ToInt32(Console.ReadLine()); Function obj2 = new Function(Leng272); Console.WriteLine("The original sequence:"); Console.WriteLine(obj2); Console.WriteLine("'RecursiveMergeSort' Finaly Sorting Result:"); obj2.ToRecursiveMergeSort(); Console.WriteLine(obj2); break; case 3: Console.WriteLine("Please Input Array Length"); int Leng273 = Convert.ToInt32(Console.ReadLine()); Function obj3 = new Function(Leng273); Console.WriteLine("The original sequence:"); Console.WriteLine(obj3); obj3.ToNaturalMergeSort(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("'NaturalMergeSort' Finaly Sorting Result:"); Console.WriteLine(obj3); break; } } case 7: //基数排序 RadixSorter rS = new RadixSorter(); newIntArray = rS.RadixSort(intArray, intArray.Length); foreach (int i in intArray) { Console.Write(i + " "); } Console.ReadKey(); break; case 8: //计数排序 int[] intNewArray = intArray; intNewArray = Counting.CountingSort(intArray, intArray.Length); foreach (int i in intNewArray) { Console.Write(i + " "); } Console.ReadKey(); break; case 9: //堆排序 HeapSort.HeapSortFunction(intArray); foreach (int i in intArray) { Console.Write(i + " "); } Console.ReadKey(); break; default: Environment.Exit(0); break; } }
public void SelectionSorterTest(int[] unsorted, int[] expected, int[] sorted, SelectionSorter selectionSorter) { "Given an unsorted array" .x(() => { }); "And a SelectionSorter" .x(() => { selectionSorter = new SelectionSorter(); }); "When I SelectionSort it" .x(() => { sorted = selectionSorter.Sort(unsorted); }); "Then it is sorted as I expect" .x(() => { Xunit.Assert.Equal(expected, sorted); }); }
public void TestSelectionSorter() { SelectionSorter <int> sorter = new SelectionSorter <int>(); TestSorter(sorter); }
public void PassingEmptyCollection() { SelectionSorter<int> selectionSorter = new SelectionSorter<int>(new List<int>()); }
public void TestSelectionSortNullList1() { SelectionSorter <int> sorter = new SelectionSorter <int>(); sorter.Sort(null); }
public void Test_SelectionSorter_ShouldSortCorrectlyForAnyTipe() { var selectionSorter = new SelectionSorter<string>(); var stringCollection = new SortableCollection<string>(new List<string>() { "b", "c", "a" }); stringCollection.Sort(selectionSorter); bool isSortedCorrectly = true; for (int i = 0; i < stringCollection.Items.Count - 1; i++) { if (stringCollection.Items[i].CompareTo(stringCollection.Items[i + 1]) > 0) { isSortedCorrectly = false; break; } } Assert.IsTrue(isSortedCorrectly, "SelectionSorter should sort correctly for any Type."); }
static void SortDemo() { // seven custom algorithms; // for each algorithm prepare copies of the same array to sort Random rd = new Random(); int[] z1 = new int[50000]; for (int i = 0; i < z1.Length; i++) { z1[i] = rd.Next(0, z1.Length); } int[] z2 = new int[z1.Length]; z1.CopyTo(z2, 0); int[] z3 = new int[z1.Length]; z1.CopyTo(z3, 0); int[] z4 = new int[z1.Length]; z1.CopyTo(z4, 0); int[] z5 = new int[z1.Length]; z1.CopyTo(z5, 0); int[] z6 = new int[z1.Length]; z1.CopyTo(z6, 0); int[] z7 = new int[z1.Length]; z1.CopyTo(z7, 0); // ---------------------------------------------------------- Console.WriteLine("Selection Sort:"); ISorter <int> sorter = new SelectionSorter(); DateTime dt1 = DateTime.Now; sorter.Sort(z1, 0, z1.Length - 1); DateTime dt2 = DateTime.Now; Console.WriteLine("Time: {0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z1.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Bubble sort"); sorter = new BubbleSorter(); dt1 = DateTime.Now; sorter.Sort(z2, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z2.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Insertion sort"); sorter = new InsertionSorter(); dt1 = DateTime.Now; sorter.Sort(z3, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z3.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Shaker sort"); sorter = new ShakerSorter(); dt1 = DateTime.Now; sorter.Sort(z4, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z4.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Shell sort"); sorter = new ShellSorter(); dt1 = DateTime.Now; sorter.Sort(z5, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z5.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Counting sort"); sorter = new CountingSorter(); dt1 = DateTime.Now; sorter.Sort(z6, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z6.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Quicksort"); sorter = new QuickSorter(); dt1 = DateTime.Now; sorter.Sort(z7, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z7.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.ReadKey(); Console.Clear(); }
public static void InitilizeSelectionSorter(TestContext context) { sorter = new SelectionSorter<int>(); }
public void Test_SelectionSorter_ShouldSwapCorrectly() { var selectionSorter = new SelectionSorter<int>(); var smallCollection = new SortableCollection<int>(new List<int>() { 4, 2 }); smallCollection.Sort(selectionSorter); bool isSortedCorrectly = smallCollection.Items[0] == 2 && smallCollection.Items[1] == 4; Assert.IsTrue(isSortedCorrectly, "SelectionSorter should sort correctly."); }