Results 1 to 7 of 7
  1. #1
    siri.th is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Arrow please help me in finding second highest in an array using single loop

    hi to all
    a tried a lot and asked many for getting second highest in the array using only one loop. but am not succeeded. please anyone help me.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Java Code:
    yourArray
    Arrays.sort(yourArray)
    sysout(yourArray[yourArray.length-2])
    All without a loop.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default

    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.

  4. #4
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    This one may work :

    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);
    Hopefully, I got the right algorithm.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by PhHein View Post
    Java Code:
    yourArray
    Arrays.sort(yourArray)
    sysout(yourArray[yourArray.length-2])
    All without a loop.
    This is the most easiest way to move, no point of stuck with complex logics.

  6. #6
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    This is the most easiest way to move, no point of stuck with complex logics.
    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".

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by xorhazard81x View Post
    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.
    True. I'm not blaming on question, but seems it's pointless.

Similar Threads

  1. Array highest grade
    By Cdlove in forum New To Java
    Replies: 5
    Last Post: 05-05-2010, 09:25 PM
  2. Finding a the max value of the array using a for loop
    By soccer_kid_6 in forum New To Java
    Replies: 1
    Last Post: 04-11-2010, 11:25 PM
  3. Replies: 18
    Last Post: 11-29-2009, 11:58 PM
  4. Replies: 21
    Last Post: 11-05-2008, 02:31 PM
  5. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •