File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Algorithm to find the kth largest element in unsorted list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Algorithm to find the kth largest element in unsorted list" Watch "Algorithm to find the kth largest element in unsorted list" New topic
Author

Algorithm to find the kth largest element in unsorted list

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Here is what I could write.



It has a bug. It is producing incorrect output for k=2. For k=1,3,4,5,6 , it is working fine.


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Mansukhdeep Thind wrote:Here is what I could write.

Rather than prolong this too much, here's my solution. See if you follow it:it probably needs a check or two to make it bulletproof; but hopefully you get the general idea.

Winston

PS: So I lied; 30 lines, not 20.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I nailed it Winston. Here is my code:



I shall mark this as resolved now. Thanks all you guys.
 
GeeCON Prague 2014
 
subject: Algorithm to find the kth largest element in unsorted list