Topic describes

Their thinking

  • First, sort the array from smallest to largest.
  • Use reverse to reverse the array.
  • Return the k- 1st element of the inverted array.
  • This time do not take JS built-in sort, but their own quick sort.

The problem solving code

var findKthLargest = function (nums, k) {
    // Quicksort

    function sort(nums, Left, Right) {
        let l = Left;
        let r = Right;
        let pivot = nums[l];
        if (l >= r) return;

        while (l < r) {
            while (nums[r] >= pivot && l < r) {
                r--;
            }
            if (nums[r] < pivot) {
                nums[l] = nums[r];
                l++;
            }
            while (nums[l] <= pivot && l < r) {
                l++;
            }
            if (nums[l] > pivot) {
                nums[r] = nums[l];
                r--;
            }
            if (l === r) {
                nums[l] = pivot
            }
        }

        sort(nums, Left, l - 1);
        sort(nums, r + 1, Right);
    }
    
    sort(nums,0,nums.length-1);
    nums
    return nums.reverse()[k-1]};Copy the code

The title to reflect

  • Proficient in quicksort.
  • Skilled use of JS in a variety of common API.