Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding Largest Numbers in Array

Posted on 2002-03-29
3
Medium Priority
?
2,896 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 100 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

Industry Leaders: 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

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…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

636 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