# Kth Largest Element in an Array

## Heap

```python
class Solution:
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        """
        heapq.heappush(heap, item = (priority, object))
        heapq.heappop(heap)
        heapq.heappushpop(heap, item)
        heapq.nlargest(n, iterable, key=None)
        heapq.nsmallest(n, iterable, key=None)
        """
        heapq.heapify(nums)
        elements = heapq.nlargest(k, nums)
        return elements[-1]
```

## Merge Sort

```python
class Solution:
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        """
        Merge Sort
        """
    
        def divide(nums):
            if len(nums) == 1:
                return nums
            mid = int(len(nums)/2)
            left, right = divide(nums[:mid]), divide(nums[mid:])
            return conquer(left, right)
        def conquer(left, right):
            res = []
            pointer1 = pointer2 = 0
            while pointer1 < len(left) and pointer2 < len(right):
                if left[pointer1] < right[pointer2]:
                    res.append(left[pointer1])
                    pointer1 +=1
                else:
                    res.append(right[pointer2])
                    pointer2 +=1           
            res.extend(left[pointer1:])
            res.extend(right[pointer2:])           
            return res            
        ordered = divide(nums)
        return ordered[-k] 
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zcjian.gitbook.io/project/array/kth-largest-element-in-an-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
