Ejemplo n.º 1
0
 public static int SortAndCountComparisons(
     int[] input,
     PivotStrategy pivotStrategy)
 {
     int comparisonsCount = 0;
     Sort(input, 0, input.Length - 1, pivotStrategy, ref comparisonsCount);
     return comparisonsCount;
 }
Ejemplo n.º 2
0
        private void QuickSort(ulong[] array, int start, int end, PivotStrategy strategy, ref int comparsionsCount)
        {
            if (start >= end)
            {
                return;
            }

            var pivotIndex =
                strategy == PivotStrategy.First ? start
                                : strategy == PivotStrategy.Last ? end
                                : quickSort.CalcMedian(array, start, end);

            pivotIndex = quickSort.Partition(array, start, end, pivotIndex);

            comparsionsCount += end - start;

            QuickSort(array, start, pivotIndex - 1, strategy, ref comparsionsCount);

            QuickSort(array, pivotIndex + 1, end, strategy, ref comparsionsCount);
        }
Ejemplo n.º 3
0
 public static int GetPivotIndex(int[] input, int startIndex, int endIndex, PivotStrategy pivotStrategy)
 {
     switch (pivotStrategy)
     {
         case PivotStrategy.First:
             return startIndex;
         case PivotStrategy.Last:
             return endIndex;
         case PivotStrategy.MedianOfTrhee:
             {
                 int arrayLength = endIndex - startIndex + 1;
                 int mediumIndex = arrayLength%2 == 0 ? arrayLength/2 - 1 : arrayLength/2;
                 mediumIndex += startIndex;
                 if (input[startIndex] >= input[mediumIndex])
                 {
                     if (input[startIndex] <= input[endIndex])
                     {
                         return startIndex;
                     }
                     if (input[mediumIndex] >= input[endIndex])
                     {
                         return mediumIndex;
                     }
                     return endIndex;
                 }
                 if (input[startIndex] >= input[endIndex])
                 {
                     return startIndex;
                 }
                 if (input[mediumIndex] < input[endIndex])
                 {
                     return mediumIndex;
                 }
                 return endIndex;
             }
         default:
             throw new NotImplementedException();
     }
 }
Ejemplo n.º 4
0
        private static void Partition(
            int[] input, 
            int startIndex, 
            int endIndex, 
            PivotStrategy pivotStrategy, 
            out int pivotIndex)
        {
            pivotIndex = GetPivotIndex(input, startIndex, endIndex, pivotStrategy);
            Swap(input, startIndex, pivotIndex);
            pivotIndex = startIndex;

            int lastLessValueIndex = startIndex + 1;
            int pivot = input[pivotIndex];
            for (int index = startIndex + 1; index <= endIndex; index++)
            {
                if (input[index] < pivot)
                {
                    Swap(input, index, lastLessValueIndex);
                    lastLessValueIndex++;
                }
            }
            Swap(input, pivotIndex, lastLessValueIndex - 1);
            pivotIndex = lastLessValueIndex - 1;
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Solver{T}"/> class.
 /// </summary>
 /// <param name="strategy">The pivot strategy that needs to be used.</param>
 /// <param name="size">The number of equations/variables.</param>
 protected Solver(PivotStrategy <T> strategy, int size)
     : base(size)
 {
     NeedsReordering = true;
     Strategy        = strategy.ThrowIfNull(nameof(strategy));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComplexSolver"/> class.
 /// </summary>
 /// <param name="size">The number of equations and variables.</param>
 /// <param name="strategy">The pivot strategy.</param>
 public ComplexSolver(int size, PivotStrategy <Complex> strategy)
     : base(strategy, size)
 {
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Solver{T}"/> class.
 /// </summary>
 /// <param name="strategy">The pivot strategy that needs to be used.</param>
 /// <param name="size">The number of equations/variables.</param>
 protected Solver(PivotStrategy <T> strategy, int size)
     : base(size)
 {
     NeedsReordering = true;
     Strategy        = strategy;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Solver{T}"/> class.
 /// </summary>
 /// <param name="strategy">The pivot strategy that needs to be used.</param>
 protected Solver(PivotStrategy <T> strategy)
 {
     NeedsReordering = true;
     Strategy        = strategy;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="size">Size</param>
 /// <param name="strategy">Strategy</param>
 public RealSolver(int size, PivotStrategy <double> strategy)
     : base(strategy, size)
 {
 }
Ejemplo n.º 10
0
        private void QuickSort(ulong[] array, PivotStrategy strategy, ref int comparsionsCount)
        {
            var copy = array.ToArray();

            QuickSort(copy, 0, copy.Length - 1, strategy, ref comparsionsCount);
        }
Ejemplo n.º 11
0
        private static void Sort(
            int[] input, 
            int startIndex, 
            int endIndex, 
            PivotStrategy pivotStrategy,
            ref int comparisonsCount)
        {
            if (endIndex - startIndex <= 0)
            {
                return;
            }

            comparisonsCount += endIndex - startIndex;

            int partitionIndex;
            Partition(input, startIndex, endIndex, pivotStrategy, out partitionIndex);
            Sort(input, startIndex, partitionIndex - 1, pivotStrategy, ref comparisonsCount);
            Sort(input, partitionIndex + 1, endIndex, pivotStrategy, ref comparisonsCount);
        }