Ejemplo n.º 1
0
        public static void SolveProblem(PivotPartitionEnum pivotPartition = PivotPartitionEnum.MedianOfThree)
        {
            var arr1 = Helpers.GetArrayIntFromFile(@"./QuickSort.txt");
            var c    = new Assignment3().QuickSort(arr1, 0, arr1.Length - 1, PivotPartitionEnum.MedianOfThree);

            Console.WriteLine($"Total comparisions = {c}");
        }
Ejemplo n.º 2
0
        public int QuickSort(int[] arr, int left, int right, PivotPartitionEnum pivotPartition = PivotPartitionEnum.MedianOfThree)
        {
            if (left >= right)
            {
                return(0);
            }
            var c = right - left;
            var p = pivotPartition switch
            {
                PivotPartitionEnum.FirstElement => PivotPartitionUsingFirstElement(arr, left, right),
                PivotPartitionEnum.LastElement => PivotPartitionUsingLastElement(arr, left, right),
                _ => PivotPartitionUsingMedianOfThreeElement(arr, left, right)
            };

            c += QuickSort(arr, left, p - 1, pivotPartition);
            c += QuickSort(arr, p + 1, right, pivotPartition);
            return(c);
        }