Solved

Finding Largest Numbers in Array

Posted on 2002-03-29
3
2,890 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
Custom Java Application connects to database: Which one of the 20? 4 41
Is there a simpler dropbox system? 10 44
junit example issue 2 28
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

733 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