public void Insertion_Sorter_Should_Sort_Correctly() { var sorter = new InsertionSorter <int>((a, b) => a.CompareTo(b)); var input = new int[] { 5, 2, 4, 6, 1, 3 }; var emptyInput = new int[] { }; sorter.Sort(input); sorter.Sort(emptyInput); Assert.Equal(new int[] { 1, 2, 3, 4, 5, 6 }, input); Assert.Equal(new int[] { }, emptyInput); }
public void Insertion_Sort_Should_Correctly_Sort_In_Decreasing_Order() { var sorter = new InsertionSorter <int>((a, b) => b.CompareTo(a)); var input = new int[] { 5, 2, 4, 6, 1, 3 }; var emptyInput = new int[] { }; sorter.Sort(input); sorter.Sort(emptyInput); Assert.Equal(new int[] { 6, 5, 4, 3, 2, 1 }, input); Assert.Equal(new int[] { }, emptyInput); }
public void TestEmptyList() { var insertionSorter = new InsertionSorter(); var list = new List<int> { }; insertionSorter.Sort(list); Assert.AreEqual(0, list.Count); }
public void SortIWrote_Check_Behaviour_On_No_Input() { int[] inputArray = { }; var sortedArray = InsertionSorter.Sort(inputArray); Assert.IsTrue(sortedArray != null); }
public void TestProperList() { var insertionSorter = new InsertionSorter(); var list = new List<int> { 3, 2, 1, 9, 7 }; insertionSorter.Sort(list); Assert.AreEqual(5, list.Count); Assert.IsTrue(list.SequenceEqual(new List<int> { 1, 2, 3, 7, 9 })); }
private static void RunIComparable() { var employeeList = GenerateEmployees(); ShowEmployees(employeeList); // before sort InsertionSorter.Sort(employeeList); ShowEmployees(employeeList); // after sort }
static void Main(string[] args) { int[] a = { 11, 9, 16, 5, 7 }; InsertionSorter.Sort(a); for (int i = 0; i < a.Length; i++) { Console.Write(a[i] + " "); } }
public void SortIWrote_Check_Sorting_Behaviour() { int[] inputArray = { 5, 7, 1, 3, 6, 9 }; var sortedArray = InsertionSorter.Sort(inputArray); Assert.IsTrue(sortedArray != null); int[] expectedArray = { 1, 3, 5, 6, 7, 9 }; CollectionAssert.AreEquivalent(sortedArray, expectedArray); }
public void Sort_EmptyArray_Success() { var sorter = new InsertionSorter <int>(); var array = new int[0]; sorter.Sort(array); Assert.IsTrue(sorter.Ensure(array)); }
public void SortIWrote_Check_Behaviour_On_Duplicate_Numbers() { int[] inputArray = { 9, 7, 1, 3, 9, 9 }; var sortedArray = InsertionSorter.Sort(inputArray); Assert.IsTrue(sortedArray != null); int[] expectedArray = { 1, 3, 7, 9, 9, 9 }; CollectionAssert.AreEquivalent(sortedArray, expectedArray); }
public void InsertionSorter_Sort(int[] elements) { var insertionSorter = new InsertionSorter(); var sortedElements = insertionSorter.Sort(elements); for (int i = 1; i < sortedElements.Length-1; i++) { Assert.IsTrue(sortedElements[i] > sortedElements[i-1]); } }
public void IsertionSortTest() { ISorter sorter = new InsertionSorter(); sorter.Sort(data); for (int i = 0; i < result.Count; i++) { Assert.IsTrue(result[i] == data[i]); } }
public void Sort_SingleElementArray_Success() { var sorter = new InsertionSorter <int>(); var array = new int[1] { 0 }; sorter.Sort(array); Assert.IsTrue(sorter.Ensure(array)); }
public void Sort_ValidArray_Success() { var sorter = new InsertionSorter <int>(); var array = new int[10] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; sorter.Sort(array); Assert.IsTrue(sorter.Ensure(array)); }
public void InsertionSorter_Sort_PassNullCollection_ThrowArgumentNullException() { // // Arrange. // double[] array = null; var insertionSorter = new InsertionSorter <double>(); // // Assert. // Assert.ThrowsException <ArgumentNullException>(() => insertionSorter.Sort(array)); }
public void SortExample() { var sorter = new InsertionSorter<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 static void ArraySorted([Random(0, 1000, 100, Distinct = true)] int n) { // Arrange var sorter = new InsertionSorter <int>(); var intComparer = new IntComparer(); var(correctArray, testArray) = RandomHelper.GetArrays(n); // Act sorter.Sort(testArray, intComparer); Array.Sort(correctArray, intComparer); // Assert Assert.AreEqual(testArray, correctArray); }
public void Sort_DoesNotChange_OriginalInput() { //arrange const int firstElement = 5, lastElement = 3; var original = new List <int> { firstElement, 2, 4, 6, 1, lastElement }; var sorter = new InsertionSorter <int>(); //act var sorted = sorter.Sort(original); //assert original[0].Should().Be(firstElement); original[5].Should().Be(lastElement); }
public void SortExample() { var sorter = new InsertionSorter <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 Sort_Returns_AscOrderedCollection() { //arrange var original = new List <int> { 5, 2, 4, 6, 1, 3 }; var sorter = new InsertionSorter <int>(); //act var sorted = sorter.Sort(original).ToList(); //assert var prev = sorted[0]; for (var i = 1; i < sorted.Count; i++) { prev.Should().BeLessOrEqualTo(sorted[i]); prev = sorted[i]; } }
public void Sort_SortsCollection_WhenProvidedCollection() { const int arraySize = 1000; var randomNumGenerator = new Random(); var arrayToSort = new int[arraySize]; for (int i = 0; i < arraySize; i++) { arrayToSort[i] = randomNumGenerator.Next(1, arraySize); } var sut = new InsertionSorter <int>(); sut.Sort(arrayToSort); for (int i = 0; i < arraySize - 1; i++) { Assert.True(arrayToSort[i] <= arrayToSort[i + 1]); } }
public void InsertionSorter_Sort_PassUnsortedCollection_GetSortedCollection() { // // Arrange. // var actualArray = new double[] { 1.56, 0.2, 376.0, 0.15 }; var expectedArray = new double[] { 0.15, 0.2, 1.56, 376.0 }; var insertionSorter = new InsertionSorter <double>(); bool isCollectionSorted; // // Act. // insertionSorter.Sort(actualArray); isCollectionSorted = actualArray.SequenceEqual(expectedArray); // // Assert. // Assert.IsTrue(isCollectionSorted); }
static void Main(string[] args) { var mass = new[] { 10, 4, 5, 8, 3, 1, 7, 6, 9, 1, 2 }; Console.WriteLine("Сортировка пузырьком"); Console.WriteLine(String.Join(",", mass)); Console.WriteLine(); Sorter sorter = new BubleSorter(); var sorted = sorter.Sort(mass.ToArray()); Console.WriteLine(); Console.WriteLine(String.Join(",", sorted)); Console.WriteLine("Сортировка вставкой"); Console.WriteLine(String.Join(",", mass)); Console.WriteLine(); sorter = new InsertionSorter(); Console.WriteLine(); sorted = sorter.Sort(mass.ToArray()); Console.WriteLine(); Console.WriteLine(String.Join(",", sorted)); Console.WriteLine("Сортировка выбором"); Console.WriteLine(String.Join(",", mass)); Console.WriteLine(); sorter = new SelectionSorter(); Console.WriteLine(); sorted = sorter.Sort(mass.ToArray()); Console.WriteLine(); Console.WriteLine(String.Join(",", sorted)); Console.WriteLine("Сортировка слиянием"); Console.WriteLine(String.Join(",", mass)); Console.WriteLine(); sorter = new MergeSorter(); Console.WriteLine(); sorted = sorter.Sort(mass.ToArray()); Console.WriteLine(); Console.WriteLine(String.Join(",", sorted)); Console.ReadLine(); }
/// <summary> /// 插入排序 /// </summary> /// <param name="i"></param> public void InsertionSorter(int i = 0) { Action <bool> action = (p) => { IList <int> list2 = list; InsertionSorter <int> .Sort(list, true); if (p) { Trace.WriteLine("插入排序:"); Trace.WriteLine(list2.ToJson()); } }; if (i < 1) { action(true); } else { string data = ActionExtensions.Time(() => action(false), "插入排序", i); Trace.WriteLine(data.Replace("<br />", Environment.NewLine)); } }
public void ArraySorted([Random(0, 1000, 1000)] int n) { // Arrange var sorter = new InsertionSorter <int>(); var intComparer = new IntComparer(); var random = new Random(); var testArray = new int[n]; var correctArray = new int[n]; for (var i = 0; i < n; i++) { var t = random.Next(0, 1000); testArray[i] = t; correctArray[i] = t; } // Act sorter.Sort(testArray, intComparer); Array.Sort(correctArray, intComparer); // Assert Assert.AreEqual(testArray, correctArray); }
public void TestInsertionSortNullList2() { InsertionSorter <int> sorter = new InsertionSorter <int>(); sorter.Sort(null, Comparer <int> .Default); }
public void TestNullList() { var insertionSorter = new InsertionSorter(); insertionSorter.Sort<int>(null); }
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 TestInsertionSortNullList3() { InsertionSorter <int> sorter = new InsertionSorter <int>(); sorter.Sort(null, SortOrder.Ascending); }
public void TestInsertionSortNullList1() { InsertionSorter <int> sorter = new InsertionSorter <int>(); sorter.Sort(null); }
public void TestInsertionSortNullComparer1() { InsertionSorter <int> sorter = new InsertionSorter <int>(); sorter.Sort(new List <int>(), (IComparer <int>)null); }
public void ExceptionNullList1() { var sorter = new InsertionSorter<int>(); sorter.Sort(null); }
public void ExceptionNullList2() { var sorter = new InsertionSorter<int>(); sorter.Sort(null, Comparer<int>.Default); }
public void ExceptionNullComparer1() { var sorter = new InsertionSorter<int>(); sorter.Sort(new List<int>(), (IComparer<int>)null); }
public void ExceptionNullList3() { var sorter = new InsertionSorter<int>(); sorter.Sort(null, SortOrder.Ascending); }