K-th smallest/largest element in unsorted array

problem [https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array].

Problem description
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.


 * Example 1
 * Input:  and k = 2
 * Output: 5


 * Example 2
 * Input:  and k = 4
 * Output: 4


 * Note
 * You may assume k is always valid, 1 ≤ k ≤ array's length.

Analysis
For finding the k-th largest element, we can use a min-heap with size k to process all the elements in the array.


 * Why use a min-heap?
 * If we have processed less than k elements in the array, we can just add to the heap.
 * But if we already have k elements in the heap, for each element hereafter, the following steps must be done:
 * Check if the current element is smaller than any elements we have processed so far.
 * If the current element is smaller than the minimum element in the heap, we do not need to do nothing. However, if the current element is greater than the minimum element in the heap, then we need to pop the original smallest element in the heap, and then insert the current element.
 * Also, after processing, the min-heap's minimum element is the result we want.

Similarly, if we want to find the k-th smallest index, we use a max-heap instead.

Time complexity
Let there be a total of n nodes, so we have n iterations in total.

Each time we update the min-heap, the time complexity is:
 * 1) Pop minimum from heap, if the current element to process is greater than minimum. &mdash; O(log k)
 * 2) Insert the current element to the heap. &mdash; O(log k)

So total time complexity is O(n log k).

Space complexity

 * Stack
 * The space complexity on stack is O(1), since we have just used a fixed set of variables and no recursion.


 * Heap
 * The space complexity on heap is O(k), which involves the min-heap.

So the total space complexity is O(k).

C++
Here is the version using a  class:

Here is a revised version using as comparator:

Java
Or, for a min-heap, we can omit the comparator:

C++

 * 
 * A  class takes three type arguments:
 * &mdash; the type of elements in the priority queue.
 * &mdash; the container of the elements. Default is.
 * &mdash; a function that compares two arguments of the element type in the  instance, and returns a   value specifying whether the first element goes before the second element. Default is.
 * If element  goes before element , by the definition of the comparison function, then during the repeated pop process, element   is popped after element.
 * In a min-heap, larger elements go before small elements; when popping repeatedly, smaller elements are popped earlier than larger elements. This is the comparison function for a min-heap:
 * In a max-heap, the popping order is the opposite of the popping order of a min-heap. This is the comparison function for a max-heap:
 * Use to get the type of the comparison function.
 * Use to provide an alias of the priority queue type parameters, to make the code simpler, cleaner, and more readable.
 * Example:
 * In this case the min-heap initialization will be as follows:
 * If we use a  class, the code would be as follows:
 * Comparison utilities
 * 
 * 
 * 
 * 

Java

 * 
 * 
 * To compare these two objects we need to implement this method:
 * 
 * To compare one object of a class that implements  with another object of the same class, we need to implement this method:.
 * To allow the  to determine if one object exists in the queue using, we need to override the  method. Note that the   method passes in an argument of type  , so that type-casting from   to the actual class is necessary to compare the members of the other object.