Link to home
Start Free TrialLog in
Avatar of q_bic
q_bic

asked on

Finding Largest Numbers in Array

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!!
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of m_onkey_boy
m_onkey_boy

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.
Avatar of q_bic

ASKER

Thanx for this great idea!!