GeeksforGeeks » Algorithms

kth largest element

(12 posts)
  1. kapil
    guest
    Posted 1 year ago #

    Find kth largest element in an array of n elements where k > 0 and k < n

  2. Sandeep
    Moderator
    Posted 1 year ago #

    All the methods discussed at http://geeksforgeeks.org/?p=2392 for k largest elements can also be used to get the kth largest element.

    Method 1 (use temp array)
    1) Create a temp array temp[0..k-1] of size k.
    2) Traverse the array arr[k..n-1]. While traversing, keep updating the smallest element of temp[]
    3) Return the smallest of temp[]
    Time Complexity: O((n-k)*k).

    Method 2 (Use a O(nlogn) Sorting)
    1) Sort the elements in ascending order.
    2) Return the element at indeax k-1.
    Time complexity: O(nlogn)

    Method 3 (Use partition method of quicksort)
    1) Randomly pick an element and partition the array around it.
    2) If partition method places the mid element at kth position from the start then we have got the kth largest element, stop further processing and return.
    Time complexity: O(n^n)

    Method 4 (Use Max Heap)
    1) Build a Max Heap tree in O(n)
    2) Use Extract Max k times to get kth maximum element. O(klogn)
    Time complexity: O(n + klogn)

    Method 5 (Use Oder Statistics)
    Use order statistic algorithm to find the kth largest element. Please see http://net.pku.edu.cn/~course/cs101/resource/Intro2Algorithm/book6/chap10.htm for details
    Time complexity: O(n)

  3. geek4u
    guest
    Posted 1 year ago #

    Here is another method. This method is mainly an optimization of method 1.

    Instead of using temp[] array, use Min Heap.

    1)  Build a Min Heap of  first k elements in the given array. O(k)
    2)  For each element after the kth element (in given array), compare it with root of  Min Heap
           a) If the element is greater than the root then insert it to the tree.
           b) Else ignore it.
    O((n-k)*logk)
    3) Finally, root of the Min Heap has kth largest element, return it.  O(1)
    

    Time Complexity O(k + (n-k)Logk)

  4. geek4u
    guest
    Posted 1 year ago #

    The method 3 can be optimized. Based on the result of partition, you can reduce the set for next processing. If returned index is smaller than k then call select for left part else for the right part.

    Randomized-Select(A[p..r],i) looking for ith o.s.
    if p = rreturn A[p]
    q <- Randomized-Partition(A,p,r)
    k <- q-p+1 	the size of the left partition
    if i=k		then the pivot value is the answer
    	return A[q]
    else if i < k	then the answer is in the front
    	return Randomized-Select(A,p,q-1,i)
    else		then the answer is in the back half
    	return Randomized-Select(A,q+1,r,i-k)
    

    This optimized version is called randomized selection. You can refer to this ppt http://jonah.cs.elon.edu/sduvall2/courses/csc331/2006spring/Lectures/Order_statistics.ppt

  5. komal
    guest
    Posted 11 months ago #

    Is there any way of finding the nth largest element in a stream of numbers,with the condition that the list should not be storted??

  6. kartik
    Moderator
    Posted 11 months ago #

    @komal
    You can use method 2 or method 6 of http://geeksforgeeks.org/?p=2392 because these methods do not require all the elements to be present in memory.

    Among these two methods, method 6 is a better choice.

  7. dharmendra hans
    guest
    Posted 11 months ago #

    there is another method to find the kth largest element. make tree(balanced ,if elements are sorted choose middle element) such that leaves points the elements.
    every non-leaf contains the number of elements in its left . e-g-- suppose non-leaf node contains k= 5 and you are searching for r= 4th element then you compare r with k .
    since r<k move to left .now again on next left node compare r with k if r>k move right and make r=r-k again keep on searching in right tree in the same way.
    this method will require extra space to build the tree. you can use this method to search kth element in linear linked list also. since tree will be having max depth log(n).so it will require max (log n)+1 number of access.

  8. komal
    guest
    Posted 11 months ago #

    there's a condition that the list should not be sorted directly or indirectly...

  9. komal
    guest
    Posted 10 months ago #

    Can i have a simple program to display all the subsets of a set...like..
    for {1,2,3} the answer would be ..
    1
    2
    3
    1,2
    1,3
    2,3
    1,2,3

  10. Venki
    Moderator
    Posted 10 months ago #

  11. eatesh
    guest
    Posted 3 weeks ago #

    time complexity of partitioning method (quick select ) is now O(n-square) but O(n).

  12. Pracheer
    Member
    Posted 3 weeks ago #

    we can find the kth largest in an array in O(n) time also


Reply

You must log in to post.

RSS feed for this topic