示例#1
0
        public IComparable Select(IComparable [] items, int k)
        {
            new FYShuffle().Shuffle(items);

            int startIndex = 0, endIndex = items.Length - 1;

            while (endIndex > startIndex)
            {
                int j = QuickSort.Partition(items, startIndex, endIndex);

                if (j < k)
                {
                    startIndex = j + 1;
                }
                else if (j > k)
                {
                    endIndex = j - 1;
                }
                else
                {
                    return(items[k]);
                }
            }
            return(items[k]);
        }
示例#2
0
        //find nth smallest element. The element returned will have n elements smaller or equal to
        public static IComparable GetNthElement(IComparable[] inputArray, int n)
        {
            new Random().Shuffle(inputArray);
            int startIndex = 0;
            int endIndex   = inputArray.Length - 1;

            while (endIndex > startIndex)
            {
                int j = QuickSort.Partition(inputArray, startIndex, endIndex);
                if (j == n)
                {
                    return(inputArray[n]);
                }
                if (j > n)
                {
                    endIndex = j - 1;
                }
                else
                {
                    startIndex = j + 1;
                }
            }
            return(inputArray[n]);
        }