示例#1
0
        public void TestWithMultipleKeys()
        {
            const int NumberOfElements = 10000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(-100, 100);
            }

            Array.Sort(elements);

            var collection = new SortableCollection <int>(elements);

            foreach (var element in elements)
            {
                /* this test returns the right result, just a different index than
                 * what the binary search would return, I don't think that there is an Array.InterpolationSearch
                 * otherwise, I think that there's nothing wrong with my algorithm*/
                int expected = Array.BinarySearch(elements, element);
                int result   = collection.InterpolationSearch(element);

                Assert.AreEqual(expected, result);
            }
        }
示例#2
0
        public void TestWithRepeatingItemShouldReturnFirstDiscoveredIndex()
        {
            var collection = new SortableCollection <int>(0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7);
            var result     = collection.InterpolationSearch(3);

            Assert.AreEqual(4, result);
        }
示例#3
0
        public void TestWithMultipleMissingKeysLargerThanMaximum()
        {
            const int NumberOfChecks   = 10000;
            const int NumberOfElements = 1000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
            }

            Array.Sort(elements);

            var collection = new SortableCollection <int>(elements);

            for (int i = 0; i < NumberOfChecks; i++)
            {
                var item = Random.Next(collection.Items[collection.Count - 1], int.MaxValue);

                int result = collection.InterpolationSearch(item);

                Assert.AreEqual(-1, result);
            }
        }
        public static void Main(string[] args)
        {
            const int NumberOfElementsToSort = 22;
            const int MaxValue = 150;

            var array = new int[NumberOfElementsToSort];

            for (int i = 0; i < NumberOfElementsToSort; i++)
            {
                array[i] = Random.Next(MaxValue);
            }

            var collectionToSort = new SortableCollection<int>(array);
            // !!!!!!!!!!!!!! collectionToSort.Sort(new BucketSorter { Max = MaxValue });

            Console.WriteLine(collectionToSort);

            var collection = new SortableCollection<int>(2, -1, 5, 0, -3);
            Console.WriteLine(collection);

            // collection.Sort(new Quicksorter<int>());
            collection.Sort(new InsertionSorter<int>());
            Console.WriteLine(collection);

            Console.WriteLine(collection.InterpolationSearch(0));
        }
        public void InterpolationTest5()
        {
            var collection = new SortableCollection<int>(1, 2, 3, 4, 5);

            var result = collection.InterpolationSearch(4);
            var expected = Array.BinarySearch(collection.ToArray(), 4);

            Assert.AreEqual(expected, result);
        }
        public void TestWithItemAtMidpoint()
        {
            var collection = new SortableCollection<int>(1, 2, 3, 4, 5);
            this.ApplyDelegates(ref collection);
            var result = collection.InterpolationSearch(3);
            var expected = Array.BinarySearch(collection.ToArray(), 3);

            Assert.AreEqual(expected, result);
        }
示例#7
0
        public void TestWithItemToTheRightOfMidpoint()
        {
            var collection = new SortableCollection <int>(1, 2, 3, 4, 5);

            var result   = collection.InterpolationSearch(4);
            var expected = Array.BinarySearch(collection.ToArray(), 4);

            Assert.AreEqual(expected, result);
        }
        public void TestWithEmptyCollectionShouldReturnMissingElement()
        {
            var collection = new SortableCollection<int>();

            var result = collection.InterpolationSearch(0);
            var expected = Array.BinarySearch(collection.ToArray(), 0);

            Assert.AreEqual(expected, result, "No elements are present in an empty collection; method should return -1.");
        }
示例#9
0
        public void TestWithMissingElement()
        {
            var collection = new SortableCollection <int>(-1, 1, 5, 12, 50);

            var result   = collection.InterpolationSearch(0);
            var expected = -1;

            Assert.AreEqual(expected, result, "Missing element should return -1.");
        }
        public void TestWithItemToTheRightOfMidpoint()
        {
            var collection = new SortableCollection<int>(1, 2, 3, 4, 5);

            var result = collection.InterpolationSearch(4);
            var expected = Array.BinarySearch(collection.ToArray(), 4);

            Assert.AreEqual(expected, result);
        }
        public void TestWithMissingElement()
        {
            var collection = new SortableCollection<int>(-1, 1, 5, 12, 50);

            var result = collection.InterpolationSearch(0);
            var expected = -1;

            Assert.AreEqual(expected, result, "Missing element should return -1.");
        }
示例#12
0
        public void TestWithEmptyCollectionShouldReturnMissingElement()
        {
            var collection = new SortableCollection <int>();

            var result   = collection.InterpolationSearch(0);
            var expected = Array.BinarySearch(collection.ToArray(), 0);

            Assert.AreEqual(expected, result, "No elements are present in an empty collection; method should return -1.");
        }
        public void TestWithItemAtMidpoint()
        {
            var collection = new SortableCollection<int>(1, 2, 3, 4, 5);
            var items = collection.Items;

            var result = collection.InterpolationSearch(items, 3, interpolator);
            var expected = Array.BinarySearch(collection.ToArray(), 3);

            Assert.AreEqual(expected, result);
        }
        public void TestWithItemToTheLeftOfMidpoint()
        {
            var collection = new SortableCollection<int>(1, 2, 3, 4, 5);

            var sorter = new Quicksorter<int>();
            collection.Sort(sorter);
            var result = collection.InterpolationSearch(collection.ToArray(), 2);
            var expected = Array.BinarySearch(collection.ToArray(), 2);

            Assert.AreEqual(expected, result);
        }
示例#15
0
        public void TestWithRepeatingItemShouldReturnFirstDiscoveredIndex()
        {
            /* Same thing here, interpolation search
             * this test returns the right result, just a different index than
             *   what the binary search would return, I don't think that there is an Array.InterpolationSearch
             *   otherwise, I think that there's nothing wrong with my algorithm*/
            var collection = new SortableCollection <int>(0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7);
            var result     = collection.InterpolationSearch(3);

            Assert.AreEqual(2, result);
        }
        public void TestWithEmptyCollectionShouldReturnMissingElement()
        {
            var collection = new SortableCollection<int>();
            // the idea for the delegates is given from ttitto
            this.ApplyDelegates(ref collection);
            var result = collection.InterpolationSearch(0);
            var expected = Array.BinarySearch(collection.ToArray(), 0);

            Assert.AreEqual(
                expected,
                result,
                "No elements are present in an empty collection; method should return -1.");
        }
        public void TestWithMultipleKeys()
        {
            const int NumberOfElements = 10000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(-100, 100);
            }

            Array.Sort(elements);

            var collection = new SortableCollection<int>(elements);

            foreach (var element in elements)
            {
                int expected = Array.BinarySearch(elements, element);
                int result = collection.InterpolationSearch(element);

                Assert.AreEqual(expected, result);
            }
        }
        public void TestWithRepeatingItemShouldReturnFirstDiscoveredIndex()
        {
            var collection = new SortableCollection<int>(0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7);
            var sorter = new InsertionSorter<int>();
            collection.Sort(sorter);
            var sortedArray = collection.ToArray(); 
            var result = collection.InterpolationSearch(sortedArray, 3);

            Assert.AreEqual(4, result);
        }
        public void TestWithMultipleMissingKeysLargerThanMaximum()
        {
            const int NumberOfChecks = 10000;
            const int NumberOfElements = 1000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
            }

            Array.Sort(elements);

            var collection = new SortableCollection<int>(elements);
            var sorter = new Quicksorter<int>();
            collection.Sort(sorter);
            var sortedArray = collection.ToArray();

            for (int i = 0; i < NumberOfChecks; i++)
            {
                var item = Random.Next(collection.Items[collection.Count - 1], int.MaxValue);
                
                var result = collection.InterpolationSearch(sortedArray, item);

                Assert.AreEqual(-1, result);
            }
        }
        public void TestWithRepeatingItemShouldReturnFirstDiscoveredIndex()
        {
            var collection = new SortableCollection<int>(0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7);
            var result = collection.InterpolationSearch(3);

            Assert.AreEqual(2, result);
        }
        public void TestWithMultipleMissingKeysSmallerThanMinimum()
        {
            const int NumberOfChecks = 10000;
            const int NumberOfElements = 1000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
            }

            Array.Sort(elements);

            var collection = new SortableCollection<int>(elements);

            for (int i = 0; i < NumberOfChecks; i++)
            {
                var item = Random.Next(int.MinValue, collection.Items[0]);

                int result = collection.InterpolationSearch(item);

                Assert.AreEqual(-1, result);
            }
        }
        public void TestWithMultipleKeys()
        {
            const int NumberOfElements = 10000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(-100, 100);
            }

            Array.Sort(elements);

            var collection = new SortableCollection<int>(elements);
            var items = collection.Items;

            foreach (var element in elements)
            {
                var result = collection.InterpolationSearch(items, element, interpolator);

                Assert.AreEqual(element, items[result]);
            }
        }
        public void InterpolationTest7()
        {
            const int NumberOfChecks = 10000;
            const int NumberOfElements = 1000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
            }

            Array.Sort(elements);

            var collection = new SortableCollection<int>(elements);

            for (int i = 0; i < NumberOfChecks; i++)
            {
                var item = Random.Next(collection.Items[collection.Count - 1], int.MaxValue);

                int result = collection.InterpolationSearch(item);

                Assert.AreEqual(-1, result);
            }
        }
        public void InterpolationTest9()
        {
            var collection = new SortableCollection<int>(0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7);
            var result = collection.InterpolationSearch(3);

            Assert.IsTrue(new int[] { 1, 2, 3, 4 }.Contains(result));
        }