示例#1
0
        public void SortExample()
        {
            var sorter = new HeapSorter<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 HeapSorter <int>();
            var intComparer = new IntComparer();

            var(correctArray, testArray) = RandomHelper.GetArrays(n);

            // Act
            sorter.Sort(testArray, intComparer);
            Array.Sort(correctArray);

            // Assert
            Assert.AreEqual(correctArray, testArray);
        }
示例#3
0
        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 HeapSorter <int>();
            //act
            var sorted = sorter.Sort(original);

            //assert
            original[0].Should().Be(firstElement);
            original[5].Should().Be(lastElement);
        }
示例#4
0
        public void SortExample()
        {
            var sorter = new HeapSorter <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]);
        }
示例#5
0
        public void Sort_Returns_AscOrderedCollection()
        {
            //arrange
            var original = new List <int> {
                5, 2, 4, 6, 1, 3
            };
            var sorter = new HeapSorter <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];
            }
        }
示例#6
0
        /// <summary>
        /// 堆排序
        /// </summary>
        /// <param name="i"></param>
        public void HeapSorter(int i = 0)
        {
            Action <bool> action = (p) => {
                IList <int> list2 = list;
                HeapSorter <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));
            }
        }
示例#7
0
		public void ExceptionNullComparer1()
        {
            var sorter = new HeapSorter<int>();
            sorter.Sort(new List<int>(), (IComparer<int>)null);
        }
示例#8
0
		public void ExceptionNullList3()
        {
            var sorter = new HeapSorter<int>();
            sorter.Sort(null, SortOrder.Ascending);
        }
示例#9
0
		public void ExceptionNullList2()
        {
            var sorter = new HeapSorter<int>();
            sorter.Sort(null, Comparer<int>.Default);
        }
示例#10
0
		public void ExceptionNullList1()
        {
            var sorter = new HeapSorter<int>();
            sorter.Sort(null);
        }
示例#11
0
        public void TestHeapSortNullComparer1()
        {
            HeapSorter <int> sorter = new HeapSorter <int>();

            sorter.Sort(new List <int>(), (IComparer <int>)null);
        }
示例#12
0
        public void TestHeapSortNullList3()
        {
            HeapSorter <int> sorter = new HeapSorter <int>();

            sorter.Sort(null, SortOrder.Ascending);
        }
示例#13
0
        public void TestHeapSortNullList2()
        {
            HeapSorter <int> sorter = new HeapSorter <int>();

            sorter.Sort(null, Comparer <int> .Default);
        }
示例#14
0
        public void TestHeapSortNullList1()
        {
            HeapSorter <int> sorter = new HeapSorter <int>();

            sorter.Sort(null);
        }