예제 #1
0
        public void TestSmallestRangeUsingHeapMinimumSize()
        {
            // Arrange
            var rangeFinder = new SmallestRangeWithHeap();

            var input = new List <int[]>()
            {
                new int[] { 4 },
                new int[] { 0 },
                new int[] { 5 }
            };

            // Act
            var result = rangeFinder.SmallestRange(input, input[0].Length, input.Count);

            // Assert
            Assert.Equal(new int[] { 0, 5 }, result);
        }
예제 #2
0
        public void TestSmallestRangeUsingHeapWithDuplicates()
        {
            // Arrange
            var rangeFinder = new SmallestRangeWithHeap();

            var input = new List <int[]>()
            {
                new int[] { 4, 7, 10, 12, 12, 12, 15 },
                new int[] { 0, 8, 9, 14, 14, 14, 20 },
                new int[] { 5, 11, 14, 15, 16, 30, 50 }
            };

            // Act
            var result = rangeFinder.SmallestRange(input, input[0].Length, input.Count);

            // Assert
            Assert.Equal(new int[] { 14, 15 }, result);
        }
예제 #3
0
        static void Main(string [] args)
        {
            var largeCollection = new List <int[]>();
            var n = 50;
            var k = 1000;

            Random rnd = new Random();

            for (int i = 0; i < k; i++)
            {
                var next = new List <int>();

                for (int j = 0; j < n; j++)
                {
                    next.Add(rnd.Next());
                }

                next.Sort();

                largeCollection.Add(next.ToArray());
            }

            var smallestRangeFinderWithList = new SmallestRangeWithList();
            var smallestRangeFinderWithHeap = new SmallestRangeWithHeap();

            var watch = new Stopwatch();

            watch.Start();
            smallestRangeFinderWithList.SmallestRange(largeCollection, n, k);
            watch.Stop();

            Console.Out.WriteLine($"Completed list search in {watch.Elapsed}");

            watch.Reset();

            watch.Start();
            smallestRangeFinderWithHeap.SmallestRange(largeCollection, n, k);
            watch.Stop();

            Console.Out.WriteLine($"Completed heap search in {watch.Elapsed}");
        }