Welcome to Dream.In.Code
Become a Java Expert!

Join 150,154 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,366 people online right now. Registration is fast and FREE... Join Now!




find N max elements in unsorted array

 
Reply to this topicStart new topic

find N max elements in unsorted array

beginerjava
23 Aug, 2008 - 03:51 AM
Post #1

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 5

Hi guys,

I want to find N max elements in unsorted array of integers in most efficient way as possible.

I could sort and then find the max elements but there should be some way of optimising the performance.

Any Ideas would be very much appreciated.

Cheers
B

User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Find N Max Elements In Unsorted Array
23 Aug, 2008 - 05:05 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 191



Thanked: 9 times
Dream Kudos: 100
My Contributions
You could put them in a SortedSet and use the last and subSet methods.

I'd say it's easier to sort first and ask questions later if you're not allowed to use the API

This post has been edited by JeroenFM: 23 Aug, 2008 - 05:06 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Find N Max Elements In Unsorted Array
23 Aug, 2008 - 05:41 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,282



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
It really depends on the size of N compared to the size of your unsorted list. If N is close enough to list size, then a sort is probably the best bet. However, if N is reasonably small...

Make an array of size N. Fill it with the first N values. Then start iterating through the rest of the list, replacing values in N array if the next value found is greater than the smallest in the N array. For bonus points, keep the N array sorted, so you needn't search through it all the time.

User is offlineProfile CardPM
+Quote Post

Gloin
RE: Find N Max Elements In Unsorted Array
23 Aug, 2008 - 08:22 AM
Post #4

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
If you make some modification to the quicksort algorithm, you will probably get the best algorithm for finding the N largest numbers of an unsorted set. Unfortunately QS is at worst O(n^2) but asymptotically O(n log n).

I would try something like:

CODE


Let L be the unsorted list, N be the number of
elements you're looking for.

QSN(List L, int N) {
  if (N > 0) {
    pivot = last element of L;
    List HighList = the numbers in L that are larger than pivot;
    List LowList = the numbers in L that are smaller than pivot;
    if ((number of Elements in HighList) < N) {
      output(HighList + pivot); // or store values in a List and return it when algorithm has executed
      QSN(LowList, N-(number of Elements in HighList +1)); // +1 for the pivot
    }
    else {
      QSN(HighList, N);
    }
}


Sort of an divide and conquer method. Might exist better algorithms, but this is probably better than sorting first.
As mentioned in one of the comments, you could add a parameter ( QSN(List L, int N, List retList) ) to store the found elements and return the retList in the end of the algorithms execution.

Just like with QS, it's very important how to choose the pivot. If input is a list in reversed order then this algorithm is crap (O(n^2)) if pivot = last element of the list.

This post has been edited by Gloin: 23 Aug, 2008 - 08:39 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:35AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month