A C# Quicksort Solution


  • 0
    L
    public int FindKthLargest(int[] nums, int k) {
        partition(nums, 0, nums.Length - 1, nums.Length - k);
        return nums[nums.Length - k];
    }
    private void partition(int[] nums, int start, int end, int k){
        int left = start, right = end;
        while(left < right){
            while(left < right && nums[left] < nums[right]) right--;
            nums[left] = nums[left] ^ nums[right] ^ (nums[right] = nums[left]);
            while(left < right && nums[left] <= nums[right]) left++;
            nums[left] = nums[left] ^ nums[right] ^ (nums[right] = nums[left]);
        }
        if(start < end && right != k){
            partition(nums, start, right - 1, k);
            partition(nums, right + 1, end, k);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.