Results 1 to 7 of 7
- 07-27-2010, 07:45 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 2
- Rep Power
- 0
- 07-27-2010, 07:49 PM #2
All without a loop.Java Code:yourArray Arrays.sort(yourArray) sysout(yourArray[yourArray.length-2])
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-27-2010, 09:42 PM #3
Can you show us your code and describe where your problem is?
Have you worked out a design for finding the answer? You need that before you write code.Last edited by Norm; 07-27-2010 at 10:26 PM.
- 07-28-2010, 04:55 AM #4
This one may work :
Hopefully, I got the right algorithm.PHP Code:int [] ar={1,5,6,2,9,5,7,7,8,3}; int first=ar[0]; int second=0; int tmpFirst=0; for(int i=1;i<ar.length;i++) { if(first<ar[i]) { tmpFirst=first; first=ar[i]; second=tmpFirst; } if(second<ar[i] && first!=ar[i]) second=ar[i]; } System.out.println("first : "+first+" ; second : "+second);
- 07-28-2010, 05:13 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 07-28-2010, 08:37 AM #6
Actually, sort() compares between each value in an array more than one time.
Hence, it fails to do as we have been asked in the question.
BTW - sort() uses tuned-quick-sort algorithm, which means O(n*log n), while we need O(n).
For more information, google "quick-sort" & "tuned quick-sort" & "sort() in java".
- 07-28-2010, 09:21 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Array highest grade
By Cdlove in forum New To JavaReplies: 5Last Post: 05-05-2010, 09:25 PM -
Finding a the max value of the array using a for loop
By soccer_kid_6 in forum New To JavaReplies: 1Last Post: 04-11-2010, 11:25 PM -
Finding Most Effecient Distribution of Change -- Won't Enter Loop
By Cod in forum New To JavaReplies: 18Last Post: 11-29-2009, 11:58 PM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM -
Finding the highest number
By jigglywiggly in forum New To JavaReplies: 7Last Post: 11-04-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks