예제 #1
0
        public void QuickSelectFindsGreatestElement()
        {
            var list = _list;
            var qs = new Quickselect<double>();

            var smallestIndex = qs.Select(list, Length - 1);
            smallestIndex.Should().Be(Length - 1, "because quickselect returns a partially ordered list right of the returned index which is greater or equal - and that list must be empty for the greatest element");

            var smallestElement = list[smallestIndex];
            smallestElement.Should().Be(list.Max(), "because that is the greatest element");
        }
예제 #2
0
        public void QuickSelectFindsSecondSmallestElement()
        {
            var list = _list;
            var qs = new Quickselect<double>();

            var secondSmallestIndex = qs.Select(list, 1);
            secondSmallestIndex.Should().Be(1, "because quickselect returns a partially ordered list left of the returned index which is smaller - and there must only be one element to the left");

            var secondSmallestElement = list[secondSmallestIndex];
            secondSmallestElement.Should().BeGreaterThan(list[0], "because the element left of the returned index should be smaller");
            secondSmallestElement.Should().BeLessOrEqualTo(list[2], "because the elements right of the returned index should be greater or equal");
            list.Skip(2).Should().OnlyContain(value => value >= secondSmallestElement, "because all elements right of the returned index should be greater or equal");
        }
예제 #3
0
        public void QuickSelectFindsNthSmallestElement()
        {
            var list = _list;
            var qs = new Quickselect<double>();

            var n = Length / 2;
            var index = qs.Select(list, n);
            index.Should().BeInRange(0, Length - 1, "because we expect the result to be a list index");

            var element = list[index];
            list.Take(n).Should().OnlyContain(value => value < element, "because all elements left of the returned index should be smaller");
            list.Skip(n).Should().OnlyContain(value => value >= element, "because all elements right of the returned index should be greater or equal");
        }
예제 #4
0
        public void QuickSelectFindsGreatestElement()
        {
            var list = _list;
            var qs   = new Quickselect <double>();

            var smallestIndex = qs.Select(list, Length - 1);

            smallestIndex.Should().Be(Length - 1, "because quickselect returns a partially ordered list right of the returned index which is greater or equal - and that list must be empty for the greatest element");

            var smallestElement = list[smallestIndex];

            smallestElement.Should().Be(list.Max(), "because that is the greatest element");
        }
예제 #5
0
        public void QuickSelectFindsSmallestElement()
        {
            var list = _list;
            var qs   = new Quickselect <double>();

            var smallestIndex = qs.Select(list, 0);

            smallestIndex.Should().Be(0, "because quickselect returns a partially ordered list left of the returned index which is smaller - and that list must be empty for the smallest element");

            var smallestElement = list[smallestIndex];

            smallestElement.Should().Be(list.Min(), "because that is the smallest element");
        }
예제 #6
0
        public void QuickSelectFindsNthSmallestElement()
        {
            var list = _list;
            var qs   = new Quickselect <double>();

            var n     = Length / 2;
            var index = qs.Select(list, n);

            index.Should().BeInRange(0, Length - 1, "because we expect the result to be a list index");

            var element = list[index];

            list.Take(n).Should().OnlyContain(value => value < element, "because all elements left of the returned index should be smaller");
            list.Skip(n).Should().OnlyContain(value => value >= element, "because all elements right of the returned index should be greater or equal");
        }
예제 #7
0
        public void QuickSelectFindsSecondSmallestElement()
        {
            var list = _list;
            var qs   = new Quickselect <double>();

            var secondSmallestIndex = qs.Select(list, 1);

            secondSmallestIndex.Should().Be(1, "because quickselect returns a partially ordered list left of the returned index which is smaller - and there must only be one element to the left");

            var secondSmallestElement = list[secondSmallestIndex];

            secondSmallestElement.Should().BeGreaterThan(list[0], "because the element left of the returned index should be smaller");
            secondSmallestElement.Should().BeLessOrEqualTo(list[2], "because the elements right of the returned index should be greater or equal");
            list.Skip(2).Should().OnlyContain(value => value >= secondSmallestElement, "because all elements right of the returned index should be greater or equal");
        }
예제 #8
0
        public void QuickSelectFindsSmallestElement()
        {
            var list = _list;
            var qs = new Quickselect<double>();

            var smallestIndex = qs.Select(list, 0);
            smallestIndex.Should().Be(0, "because quickselect returns a partially ordered list left of the returned index which is smaller - and that list must be empty for the smallest element");

            var smallestElement = list[smallestIndex];
            smallestElement.Should().Be(list.Min(), "because that is the smallest element");
        }