public TreeNode OrganizedTree(TreeNode tree) { var values = tree.InOrderTraversal().ToArray(); var sortedValues = SortMethods.BubbleSort(values).ToList(); var index = sortedValues.Count / 2; var root = new TreeNode(sortedValues.ElementAt(index)); while (sortedValues.Count != 0) { var j = sortedValues.Count / 2; var node = new TreeNode(sortedValues.ElementAt(index)); sortedValues.Remove(index); } if (sortedValues[index - 1] == sortedValues[index]) { root.right = new TreeNode(sortedValues[index - 1]); } else { } return(root); }
public void BubbleSortInOrder() { var startTime = DateTime.Now; var input = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; var expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; var result = SortMethods.BubbleSort(input); var endTime = DateTime.Now; Console.WriteLine($"Runtime: {(endTime - startTime).Milliseconds.ToString()}"); Assert.IsTrue(expected.SequenceEqual(result)); }
public void BubbleSortPartiallyOutOfOrder() { var startTime = DateTime.Now; var input = new int[] { 7, 2, 3, 4, 5, 6, 1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; var expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; var result = SortMethods.BubbleSort(input); var endTime = DateTime.Now; var evenNumbers = expected.Where(n => n % 2 == 0).ToList(); Console.WriteLine($"Runtime: {(endTime - startTime).Milliseconds.ToString()}"); Assert.IsTrue(expected.SequenceEqual(result)); }