public static T Select(T[] elements, int k)
        {
            KnuthShuffle <T> .Shuffle(elements);

            int low = 0, high = elements.Length - 1;

            while (high > low)
            {
                // The element known to be in place.
                int partitionElement = Partition(elements, low, high);

                if (k < partitionElement)
                {
                    high = partitionElement - 1;
                }
                else if (k > partitionElement)
                {
                    low = partitionElement + 1;
                }
                else
                {
                    return(elements[k]);
                }
            }

            return(elements[k]);
        }
        public void KnuthSuffle_ShufflesNumbersUniformly()
        {
            var elements = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            KnuthShuffle <int> .Shuffle(elements);

            Assert.True(true);
        }
Beispiel #3
0
        public void testSort()
        {
            var a = new int[100];
            var b = new int[100];

            for (var i = 0; i < a.Length; ++i)
            {
                a[i] = i;
                b[i] = i;
            }
            KnuthShuffle.Shuffle(a);
            Assert.NotEqual(b, a);
            ShellSort.Sort(a);
            Assert.Equal(b, a);
        }
        public void testSort()
        {
            var a = new int[100];
            var b = new int[100];

            for (var i = 0; i < a.Length; ++i)
            {
                a[i] = i;
                b[i] = i;
            }
            KnuthShuffle.Shuffle(a);
            Assert.NotEqual(b, a);
            ThreeWayQuickSort.Sort(a);

            for (var i = 0; i < a.Length; ++i)
            {
                Assert.Equal(b[i], a[i]);
            }
        }
Beispiel #5
0
        public void testSort()
        {
            var a = new int[100];
            var b = new int[100];

            for (var i = 0; i < a.Length; ++i)
            {
                a[i] = i;
                b[i] = i;
            }
            KnuthShuffle.Shuffle(a);
            Assert.NotEqual(b, a);
            HeapSort.Sort(a);
            for (var i = 0; i < a.Length; ++i)
            {
                console.WriteLine("{0}", a[i]);
            }
            for (var i = 0; i < a.Length; ++i)
            {
                Assert.Equal(b[i], a[i]);
            }
        }