public void TestSortFind_2() { int[] a = new int[] { 1 }; int s = SortLevel.SortFindMedian(a, 0, 0); Assert.AreEqual(1, s); }
public void TestSortFind_3() { int[] a = new int[] { 1, 2 }; int s = SortLevel.SortFindMedian(a, 0, 1); Assert.AreEqual(2, s); }
internal Node Sort(SortLevel level) { if (Left != null) { Left = Left.Sort(level); } if (Right != null) { Right = Right.Sort(level); } if (!(this is BinaryOperatorNode)) { return(Clone() as Node); } BinaryOperatorNode binOp = this as BinaryOperatorNode; if (binOp.Operator != BinaryOperator.Add && binOp.Operator != BinaryOperator.Subtract && binOp.Operator != BinaryOperator.Multiply && binOp.Operator != BinaryOperator.Divide) { return(Clone() as Node); } bool isSum = binOp.Operator == BinaryOperator.Add || binOp.Operator == BinaryOperator.Subtract; List <Tuple <Node, TreeAnalyzer.LinearChildTag> > children = TreeAnalyzer.LinearChildren(this, isSum ? BinaryOperator.Add : BinaryOperator.Multiply, isSum ? BinaryOperator.Subtract : BinaryOperator.Divide); List <List <Tuple <Node, TreeAnalyzer.LinearChildTag> > > groups = TreeAnalyzer.groupByHash(children, level); List <Tuple <Node, TreeAnalyzer.LinearChildTag> > groupedChildren = new List <Tuple <Node, TreeAnalyzer.LinearChildTag> >(); foreach (List <Tuple <Node, TreeAnalyzer.LinearChildTag> > group in groups) { groupedChildren.Add(TreeAnalyzer.internalMultihang(group, isSum ? BinaryOperator.Add : BinaryOperator.Multiply, isSum ? BinaryOperator.Subtract : BinaryOperator.Divide)); } return(TreeAnalyzer.MultiHang(groupedChildren, isSum ? BinaryOperator.Add : BinaryOperator.Multiply, isSum ? BinaryOperator.Subtract : BinaryOperator.Divide)); }
public void TestSortFind_1() { int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int s = SortLevel.SortFindMedian(a, 0, 10); Assert.AreEqual(8, s); }
public void KthOrderStatisticsStepTest() { int[] array = { 19, 13, 6, 7, 5 }; int[] expectedArray = { 5, 6, 7, 13, 19 }; List <int> expectedList = new List <int> { 0, 1 }; List <int> actualList = SortLevel.KthOrderStatisticsStep(array, 0, array.Length - 1, 1); int[] actualArray = array; Array.ForEach(actualArray, (item) => Console.Write(item + " ")); Console.WriteLine(); actualList.ForEach(item => Console.Write(item + " ")); Console.WriteLine(); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expectedArray[i], array[i]); } for (int i = 0; i < actualList.Count; i++) { Assert.AreEqual(expectedList[i], actualList[i]); } }
static void Main(string[] args) { var opt = new int[] { 10, 7, 5, 6, 9, 4, 3, 1, 2, 8 }; //int iii = SortLevel.ArrayChunk(opt); int[] arr = { 3, 9, 4, 6, 5, 1, 2, 8, 10, 7 }; Console.WriteLine("Unsorted"); foreach (int n in arr) { Console.Write(n + " "); } Console.WriteLine(); SortLevel.QuickSort(arr); Console.WriteLine("Unsorted"); foreach (int n in arr) { Console.Write(n + " "); } Console.WriteLine(); Console.ReadLine(); }
public void KnuthSequenceTest_0_1() { List <int> ret_0 = SortLevel.KnuthSequence(0); List <int> ret_1 = SortLevel.KnuthSequence(1); List <int> expectedList_1 = new List <int> { 1 }; int expectedCount = 1; int actualCount_0 = ret_0.Count; int actualCount_1 = ret_1.Count; Assert.AreEqual(expectedCount, actualCount_0); Assert.AreEqual(expectedCount, actualCount_1); for (int i = 0; i < ret_0.Count; i++) { Assert.AreEqual(expectedList_1[i], ret_0[i]); } for (int i = 0; i < ret_1.Count; i++) { Assert.AreEqual(expectedList_1[i], ret_1[i]); } }
public void KnuthSequenceTest_2_3_4() { List <int> ret1_2 = SortLevel.KnuthSequence(2); List <int> ret1_3 = SortLevel.KnuthSequence(3); List <int> ret1_4 = SortLevel.KnuthSequence(4); List <int> expectedList_1 = new List <int> { 1 }; int expectedCount = 1; int actualCount2 = ret1_2.Count; int actualCount3 = ret1_3.Count; int actualCount4 = ret1_4.Count; Assert.AreEqual(expectedCount, actualCount2); Assert.AreEqual(expectedCount, actualCount3); Assert.AreEqual(expectedCount, actualCount4); for (int i = 0; i < ret1_2.Count; i++) { Assert.AreEqual(expectedList_1[i], ret1_2[i]); } for (int i = 0; i < ret1_3.Count; i++) { Assert.AreEqual(expectedList_1[i], ret1_3[i]); } for (int i = 0; i < ret1_4.Count; i++) { Assert.AreEqual(expectedList_1[i], ret1_4[i]); } }
public void InsertionSortStepTest_if_Array_Has_Step_4_startIndex_0() { int[] testArr = { 6, 2, 5, 4, 1, 2, 7 }; Array.ForEach(testArr, (item) => Console.Write(item + " ")); Console.WriteLine(); int step = 4; int startIndex = 0; SortLevel.InsertionSortStep(testArr, step, startIndex); foreach (int item in testArr) { Console.Write(item + " "); } Console.WriteLine(); int[] expectedSortedArr = new int[] { 1, 2, 5, 4, 6, 2, 7 }; for (int i = 0; i < testArr.Length; i++) { Assert.AreEqual(expectedSortedArr[i], testArr[i]); } }
public void BubbleSortStepTest_if_Array_is_Already_Sorted() { int[] testArr = { 1, 2, 3, 4 }; Array.ForEach(testArr, (item) => Console.Write(item + " ")); Console.WriteLine(); bool res = SortLevel.BubbleSortStep(testArr); foreach (int item in testArr) { Console.Write(item + " "); } Console.WriteLine(); int[] expectedSortedArr = new int[] { 1, 2, 3, 4 }; Assert.IsNotNull(testArr); Assert.IsTrue(res); Assert.IsTrue(testArr.Length == 4); for (int i = 0; i < testArr.Length; i++) { Assert.AreEqual(expectedSortedArr[i], testArr[i]); } }
/// <summary> /// Actual sorting with sortHash /// </summary> /// <param name="tree"></param> /// <param name="level"></param> internal static void Sort(ref Entity tree, SortLevel level) { Func <Entity, OperatorEntity> funcIfSum = Const.FuncIfSum; Func <Entity, OperatorEntity> funcIfMul = Const.FuncIfMul; for (int i = 0; i < tree.Children.Count; i++) { Entity tmp = tree.Children[i]; Sort(ref tmp, level); tree.Children[i] = tmp; } if (tree.Name != "sumf" && tree.Name != "mulf" && tree.Name != "minusf" && tree.Name != "divf") { return; } var isSum = tree.Name == "sumf" || tree.Name == "minusf"; var linChildren = TreeAnalyzer.LinearChildren(tree, isSum ? "sumf" : "mulf", isSum ? "minusf" : "divf", isSum ? funcIfSum : funcIfMul); var groups = TreeAnalyzer.GroupByHash(linChildren, level); var grouppedChildren = new List <Entity>(); foreach (var list in groups) { grouppedChildren.Add(TreeAnalyzer.MultiHangLinear(list, isSum ? "sumf" : "mulf", isSum ? Const.PRIOR_SUM : Const.PRIOR_MUL)); } tree = TreeAnalyzer.MultiHangLinear(grouppedChildren, isSum ? "sumf" : "mulf", isSum ? Const.PRIOR_SUM : Const.PRIOR_MUL); }
public void SelectionSortStepTest() { int[] arr = new int[] { 4, 3, 1, 2 }; var arr1 = new int[] { 4, 3, 1, 2 }; var arr2 = new int[] { 4, 3, 1, 2 }; var arr3 = new int[] { 4, 3, 1, 2 }; var opt0 = SortLevel.SelectionSortStep(arr, 0); Thread.Sleep(100); var opt1 = SortLevel.SelectionSortStep(arr1, 1); Thread.Sleep(100); var opt2 = SortLevel.SelectionSortStep(arr2, 2); Thread.Sleep(100); var opt3 = SortLevel.SelectionSortStep(arr3, 3); Thread.Sleep(100); if (!(opt0[0] == 1 && opt0[1] == 3 && opt0[2] == 4 && opt0[3] == 2)) { Assert.Fail(); } if (!(opt1[0] == 4 && opt1[1] == 1 && opt1[2] == 3 && opt1[3] == 2)) { Assert.Fail(); } if (!(opt2[0] == 4 && opt2[1] == 3 && opt2[2] == 1 && opt2[3] == 2)) { Assert.Fail(); } if (!(opt3[0] == 4 && opt3[1] == 3 && opt3[2] == 1 && opt3[3] == 2)) { Assert.Fail(); } }
public static void TestMergeSort() { var array = new List <int> { 3, 5, 2, 4, 1 }; var result = SortLevel.MergeSort(array); List <int> ethalon = new List <int> { 1, 2, 3, 4, 5 }; Assert.AreEqual(ethalon.Count, result.Count); for (int i = 0; i < result.Count; i++) { Assert.AreEqual(ethalon[i], result[i]); } var array2 = new List <int> { 10, 14, 3, 12, 11, 1, 9, 15, 7, 6, 5, 4, 13, 2, 8 }; var result2 = SortLevel.MergeSort(array2); var ethalon2 = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; Assert.AreEqual(ethalon2.Count, result2.Count); for (int i = 0; i < result2.Count; i++) { Assert.AreEqual(ethalon2[i], result2[i]); } }
/// <summary> /// Hash that is convenient to sort with /// </summary> /// <param name="level"></param> /// <returns></returns> internal string SortHash(SortLevel level) { if (this.entType == Entity.EntType.FUNCTION) { return(this.Name + "_" + string.Join("_", from child in Children select child.SortHash(level))); } else if (this.entType == EntType.NUMBER) { if (level == SortLevel.HIGH_LEVEL) { return(""); } else if (level == SortLevel.MIDDLE_LEVEL) { return((this as NumberEntity).Value.Type.ToString()); } else { return(this.Name + " "); } } else if (this.entType == Entity.EntType.VARIABLE) { return("v_" + Name); } else { return((level == SortLevel.LOW_LEVEL ? this.Name + "_" : "") + string.Join("_", from child in Children let hash = child.SortHash(level) where !string.IsNullOrEmpty(hash) select hash)); } }
public void SelectionSortStep_if_Array_is_Empty() { int[] testArr = { }; SortLevel.SelectionSortStep(testArr, 0); Assert.IsNotNull(testArr); Assert.IsTrue(testArr.Length == 0); }
public static void TestKthOrderStatisticsStep() { var array = new int[] { 3, 5, 2, 4, 1 }; var result = SortLevel.KthOrderStatisticsStep(array, 0, 4, 0); List <int> ethalon = new List <int> { 0, 3 }; Assert.AreEqual(ethalon.Count, result.Count); for (int i = 0; i < result.Count; i++) { Assert.AreEqual(ethalon[i], result[i]); } result = SortLevel.KthOrderStatisticsStep(array, 0, 3, 0); ethalon = new List <int> { 0, 0 }; Assert.AreEqual(ethalon.Count, result.Count); for (int i = 0; i < result.Count; i++) { Assert.AreEqual(ethalon[i], result[i]); } var array2 = new int[] { 3, 5, 2, 4, 1 }; var result2 = SortLevel.KthOrderStatisticsStep(array2, 0, 4, 4); List <int> ethalon2 = new List <int> { 4, 4 }; Assert.AreEqual(ethalon2.Count, result2.Count); for (int i = 0; i < result2.Count; i++) { Assert.AreEqual(ethalon2[i], result2[i]); } var array1_teacher = new int[] { 5, 6, 7, 4, 1, 2, 3 }; var result1_teacher = SortLevel.KthOrderStatisticsStep(array1_teacher, 0, 6, 0); List <int> ethalon1_teacher = new List <int> { 0, 2 }; Assert.AreEqual(ethalon1_teacher.Count, result1_teacher.Count); for (int i = 0; i < result1_teacher.Count; i++) { Assert.AreEqual(ethalon1_teacher[i], result1_teacher[i]); } var result2_teacher = SortLevel.KthOrderStatisticsStep(array1_teacher, 0, 2, 0); var ethalon2_teacher = new List <int> { 0, 0 }; Assert.AreEqual(ethalon2_teacher.Count, result2_teacher.Count); for (int i = 0; i < result2_teacher.Count; i++) { Assert.AreEqual(ethalon2_teacher[i], result2_teacher[i]); } }
public void TestShellSort_1() { int[] array = new int[] { 12, 8, 17, 13, 10, 16, 15, 2, 5, 1, 4, 6, 14, 7, 11, 9, 3 }; SortLevel.ShellSort(array); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(i + 1, array[i]); } }
public void TestSelection_1() { int[] array = new int[] { 7, 42, 25 }; SortLevel.SelectionSortStep(array, 1); Assert.AreEqual(7, array[0]); Assert.AreEqual(25, array[1]); Assert.AreEqual(42, array[2]); Assert.AreEqual(3, array.Length); }
public void BubbleSortStep_if_Array_is_Empty() { int[] testArr = { }; bool res = SortLevel.BubbleSortStep(testArr); Assert.IsNotNull(testArr); Assert.IsTrue(res); Assert.IsTrue(testArr.Length == 0); }
public void TestQuickSort_8() { int[] array = new int[] { 4, 2, 7, 1, 8, 5, 3, 9, 15, 11, 13, 12, 14 }; int[] expay = new int[] { 1, 2, 3, 4, 5, 7, 8, 9, 15, 11, 13, 12, 14 }; SortLevel.QuickSort(array, 0, 8); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestQuickSort_10() { int[] array = new int[] { 3, 2, 1 }; int[] expay = new int[] { 1, 2, 3 }; SortLevel.QuickSort(array, 0, 2); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestQuickSort_1() { int[] array = new int[] { 1, 3, 4, 6, 5, 2, 8 }; int[] expay = new int[] { 1, 2, 3, 4, 5, 6, 8 }; SortLevel.QuickSort(array, 0, 6); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestQuickSort_5() { int[] array = new int[] { 4, 2, 7, 1, 8, 5 }; int[] expay = new int[] { 4, 1, 2, 7, 8, 5 }; SortLevel.QuickSort(array, 1, 4); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestArrayChunk_6() { int[] array = new int[] { 1, 3, 4, 6, 5, 2, 8 }; int[] expay = new int[] { 1, 3, 4, 2, 5, 6, 8 }; Assert.AreEqual(5, SortLevel.ArrayChunk(array)); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestBubbles_2() { int[] array = new int[] { 1, 2, 3, 4, 5 }; int[] expay = new int[] { 1, 2, 3, 4, 5 }; Assert.AreEqual(true, SortLevel.BubbleSortAll(array)); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestArrayChunk_5() { int[] array = new int[] { 7, 4, 9, 1, 6, 10 }; int[] expay = new int[] { 1, 4, 9, 7, 6, 10 }; Assert.AreEqual(0, SortLevel.ArrayChunk(array)); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestArrayChunk_4() { int[] array = new int[] { 9, 6, 8, 7, 5, 4, 1, 2 }; int[] expay = new int[] { 2, 1, 4, 5, 6, 7, 8, 9 }; Assert.AreEqual(4, SortLevel.ArrayChunk(array)); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestArrayChunk_2() { int[] array = new int[] { 4, 3, 9, 1, 2 }; int[] expay = new int[] { 4, 3, 2, 1, 9 }; Assert.AreEqual(4, SortLevel.ArrayChunk(array)); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestArrayChunk_1() { int[] array = new int[] { 7, 5, 6, 4, 3, 1, 2 }; int[] expay = new int[] { 2, 1, 3, 4, 6, 5, 7 }; Assert.AreEqual(3, SortLevel.ArrayChunk(array)); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(expay[i], array[i]); } }
public void TestShellSort_5() { int[] array = new int[] { 5, 4, 3, 2, 1 }; int[] expay = new int[] { 0, 33, 11, 7, 9 }; SortLevel.ShellSort(array); for (int i = 0; i < array.Length; i++) { Assert.IsTrue(expay[i] != array[i]); } }