Пример #1
0
        static void SwapTwoDictElements(List <FrquencyKeyValue> arr, int start, int pivot)
        {
            FrquencyKeyValue value = arr[start];

            arr[start] = arr[pivot];
            arr[pivot] = value;
        }
Пример #2
0
 public int CompareTo(Object obj)
 {
     if (obj == null)
     {
         return(1);
     }
     else
     {
         FrquencyKeyValue other = obj as FrquencyKeyValue;
         return(this.value - other.value);  //
     }
 }
Пример #3
0
        public static void QuickSortRecursiveHelperTopK(List <FrquencyKeyValue> arr, int start, int end, int kthArrayIndex)
        {
            if (start >= end)
            {
                return;
            }
            int pivotIndex = RandomPivotIndex(start, end + 1);
            FrquencyKeyValue pivotValue = arr[pivotIndex];

            SwapTwoDictElements(arr, start, pivotIndex);
            int smaller = start;
            int bigger  = start + 1;

            while (bigger <= end)
            {
                if (arr[bigger].CompareTo(pivotValue) < 0)
                {
                    smaller++;
                    SwapTwoDictElements(arr, smaller, bigger);
                }
                bigger++;
            }

            SwapTwoDictElements(arr, smaller, start);

            if (smaller == kthArrayIndex)
            {
                return;
            }
            if (kthArrayIndex < smaller)
            {
                QuickSortRecursiveHelperTopK(arr, start, smaller - 1, kthArrayIndex);
            }
            else
            {
                QuickSortRecursiveHelperTopK(arr, smaller + 1, bigger - 1, kthArrayIndex);
            }
        }