Solved

Finding Largest Numbers in Array

Posted on 2002-03-29
3
2,885 Views
Last Modified: 2007-12-19
Hi, How do write a function that returns the largest number in an array on the first execution of the loop, then on the second execution, it returns the second largest, then the third and so on till finally the smallest number.

Thanx!!
0
Comment
Question by:q_bic
3 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 25 total points
ID: 6905431
try this (doesn't support negative numbers):

int prevLargest=0;
int curLargest=0;
int[] yourArray = { 5,2,3,4,0,121,157,54,33,18,76,32,12,87,43,343,68 };


for (int i=0;i<yourArray.length;i++) {
   for (int j=0;j<yourArray.length;j++) {
       if (i==0) {
          if (yourArray[j]>curLargest) { curLargest = yourArray[j]; }
       } else {
            if (yourArray[j]>curLargest && prevLargest>yourArray[j]) { curLargest = yourArray[j];}
      }
   }

   out.println("Largest = " + curLargest);
   prevLargest = curLargest;
   curLargest = 0;

}
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6905618
Or...

int[] array = {3,4,8,1,4,2,9,10,2,5,45,4};

Arrays.sort(array);

now, you array will be sorted, so you only have to loop over it once.

If you need to keep the order of the first array, allocate a second array and do System.arrayCopy before the sort.
0
 

Author Comment

by:q_bic
ID: 6917489
Thanx for this great idea!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now