Esempio n. 1
0
        public static int FindOrderStatisticsRecursive(int[] array, int i)
        {
            if (i < 1 || i > array.Length)
            {
                throw new ArgumentOutOfRangeException($"There is no {i}th smallest index in the array");
            }
            if (array.Length == 1)
            {
                return(array[0]);
            }

            var pivotPartitioner = new PivotPartitioner(PivotSelect.Random);

            i--;

            var pivot = pivotPartitioner.Partition(array, 0, array.Length - 1);

            if (pivot == i)
            {
                return(array[i]);
            }
            if (pivot < i)
            {
                var rightArray = array.Skip(pivot + 1).ToArray();
                return(FindOrderStatisticsRecursive(rightArray, i - pivot));
            }

            var leftArray = array.Take(pivot + 1).ToArray();

            return(FindOrderStatisticsRecursive(leftArray, i + 1));
        }
Esempio n. 2
0
        public static int FindOrderStatistics(int[] array, int i)
        {
            if (i < 1 || i > array.Length)
            {
                throw new ArgumentOutOfRangeException($"There is no {i}th smallest index in the array");
            }

            var pivotPartitioner = new PivotPartitioner(PivotSelect.Random);

            i--;
            var startIndex = 0;
            var endIndex   = array.Length - 1;

            var pivot = pivotPartitioner.Partition(array, startIndex, endIndex);

            while (pivot != i)
            {
                if (pivot > i)
                {
                    endIndex = pivot - 1;
                    pivot    = pivotPartitioner.Partition(array, startIndex, endIndex);
                }
                else
                {
                    startIndex = pivot + 1;
                    pivot      = pivotPartitioner.Partition(array, startIndex, endIndex);
                }
            }

            return(array[pivot]);
        }
Esempio n. 3
0
 public QuicksortComparesCounter(int[] array, PivotSelect pivotSelectionMethod)
 {
     Partitioner = new PivotPartitioner(pivotSelectionMethod);
     QuickSort(array);
 }