Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    singularity is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default Print the second Largest in an array

    Hi, I need to print the second largest element of array. I have written the code to print the first largest, can anyone guide me how do I modify it so that it prints second largest instead


    Java Code:
    public class SecondLargest {
    	public static void main(String args[]) {
    		int temp=0,i=0;
    		int arr[] = {277,3,444,78};
    		for(i =0;i<arr.length-1; i++) {
    			
    			if(arr[i] > arr[i+1]) {
    				 temp = arr[i];
    				arr[i] = arr[i+1];
    				arr[i+1] = temp;
    			}
    			
    		}
    		System.out.println("Largest is :" +arr[i]);
    	}
    
    }

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

    Default

    The complicated way or the simple way?
    Java Code:
    int arr[] = {277,3,444,78};
    Arrays.sort(arr);
    System.out.println("largest: " + arr[arr.length-1]);
    System.out.println("second largest: " + arr[arr.length-2]);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhHein View Post
    The complicated way or the simple way?
    s/simple/lazy/ It doesn't take rocket science to find the largest number and the next to largest number in O(n) where 'n' is the length of the sequence of the numbers. Your awfully lazy method takes O(n*log(n)) steps; shame on you ;-)

    kind regards,

    Jos

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

    Default

    I didn't learn programming on stone tablets like you ;-)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhHein View Post
    I didn't learn programming on stone tablets like you ;-)
    Aah, those were the days: we had to carve those bits in stone with our teeth, twentynine hours per day but we were lucky then ...

    kind regards,

    Jos ;-)

  6. #6
    singularity is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    The complicated way or the simple way?
    Java Code:
    int arr[] = {277,3,444,78};
    Arrays.sort(arr);
    System.out.println("largest: " + arr[arr.length-1]);
    System.out.println("second largest: " + arr[arr.length-2]);
    Thanks for the help, but btw how do I find second largest element using hard code approach, and how can I print sorted elements using Arrays.sort(arr);

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

    Default

    @Jos:
    Bare feet in snow, uphill, both ways...
    Oh, no, that was something else ;-)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by singularity View Post
    Thanks for the help, but btw how do I find second largest element using hard code approach, and how can I print sorted elements using Arrays.sort(arr);
    Can you print all elements of an array? If so you can print all elements of a sorted array.

    kind regards,

    Jos

  9. #9
    singularity is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    s/simple/lazy/ It doesn't take rocket science to find the largest number and the next to largest number in O(n) where 'n' is the length of the sequence of the numbers. Your awfully lazy method takes O(n*log(n)) steps; shame on you ;-)

    kind regards,

    Jos
    Do you mean PhHein code is less optimized than doing it hard code way

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by singularity View Post
    Do you mean PhHein code is less optimized than doing it hard code way
    Yes, he is a sissy; us real programmers, we had it rough.

    kind regards,

    Jos ;-)

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

    Default

    Quote Originally Posted by singularity View Post
    Thanks for the help, but btw how do I find second largest element using hard code approach, and how can I print sorted elements using Arrays.sort(arr);
    What is a hard code approach? To print you can use this lazy approach:
    Java Code:
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    singularity is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    What is a hard code approach? To print you can use this lazy approach:
    Java Code:
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    Here is my revised code, but it doesn't give the correct results;

    Java Code:
    public class SecondLargest {
    	public static void main(String args[]) {
    		int temp=0,i=0;
    		int arr[] = {277,3,444,78,45,55,23,30,98,12,87};
    		for(i =0;i<arr.length-1; i++) {
    			
    			if(arr[i] > arr[i+1]) {
    				 temp = arr[i];
    				arr[i] = arr[i+1];
    				arr[i+1] = temp;
    			}
    			
    		}
    		System.out.println("Largest is :" +arr[i]);
    		System.out.println("SORTED ARRAY IS:" +Arrays.toString(arr));
    		System.out.println("The largest array element is: " +arr[arr.length-1]);
    		System.out.println("The second largest element is: " +arr[arr.length-2]);
    	}
    	
    }

    what is wrong and thanks for the help



    by hard code I mean what I have written to find largest element, I am asking how to find the second largest using the same approach

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Store a temp2, with the second largest in it...?

    You should be able to figure out how you use that if you've already done this with the largest.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by singularity View Post
    Here is my revised code, but it doesn't give the correct results;
    I don't know what you're doing there in that code but let's solve this in a more or less OO way. First we design something that we can feed ints and it remembers the largest number we've ever fed it; here it is:

    Java Code:
    class KeeperOfTheLargest {
       private int largest= Integer.MIN_VALUE; // largest number so far
    
       public void feed(int number) { // feed it another number
          if (number > largest)
             largest= number;
       }
    
       public int getLargest() { // get the largest number (sofar)
          return largest;
       }
    }
    ... that's all there is to it; the following code snippet finds the largest number in an array:

    Java Code:
    KeeperOfTheLargest kotl= new KeeperOfTheLargest();
    int[] array= ...
    for (int i= 0; i < array.length; i++)
       kotl.feed(array[i]);
    int largest= kotl.getLargest();
    ... we have found the largest number in the array; the next snippet finds the next to largest number:

    Java Code:
    KeeperOfTheLargest sl= new KeeperOfTheLargest();
    for (int i= 0; i < array.length; i++)
       if (array[i] != largest)
          sl.feed(array[i]);
    int second= sl.getLargest();
    ... Now you have both numbers: largest and second. There are other ways to skin this cat though.

    kind regards,

    Jos

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Pah...two passes?
    :D

    And, of course, Jos is only leaving out the curly brackets he would usually put in for the loop and if for brevity...:)

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Pah...two passes?
    :D
    Yep, and it's a O(n) algorithm but it hasn't all those twisted, nested if-then-else statements :-P

    And, of course, Jos is only leaving out the curly brackets he would usually put in for the loop and if for brevity...:)
    Actually I didn't, I find those curly braces around a single statement silly; I've been programming in BCPL, C, C++, Objective C, Java for many moons now and almost never made a mistake, YMMV.

    kind regards,

    Jos

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Ah, but (as with most things IT) it's not about you making a mistake, but the someone who comes after you making one. And a lot of minor fixing and maintenance work is done by junior developers...

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Ah, but (as with most things IT) it's not about you making a mistake, but the someone who comes after you making one. And a lot of minor fixing and maintenance work is done by junior developers...
    I couldn't care less: we have source code 'beautifiers' (formatters) so that the source code always is stored in the repositories (git, monotone, cvs, whatever) in the same format. When I work on source code I simply never add those braces around a single statement, I'm used to doing it that way. The way it shows up after formatting is not my concern (and I don't care either). I've seen juniors ruin my code more than often, not because of my lack of braces but because they didn't understand how the thing worked; to me commenting the stuff is much more important.

    kind regards,

    Jos

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    I suppose that's the difference.
    You work in one place...I work all over the place.
    Not everywhere has code beautifiers, as you put it...consequently I have to care about what goes into a clients repository.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    I suppose that's the difference.
    You work in one place...I work all over the place.
    Not everywhere has code beautifiers, as you put it...consequently I have to care about what goes into a clients repository.
    I was self employed too but I always refused to deliver one snippet of code if they didn't have a code repository; I might as well have thrown my code in the ditch then. You can keep a repository yourself; I use git, it's open source and very flexible, but monotone is a nice one too.

    kind regards,

    Jos

    edit: for those code beautifiers: have a look at Jalopy, it works fine and it's free. You can install it as a 'filter' in git, before your code enters the repository.
    Last edited by JosAH; 05-05-2010 at 10:42 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  2. two dimensional array print
    By Myles in forum New To Java
    Replies: 0
    Last Post: 04-26-2010, 11:10 AM
  3. Print String from an Array
    By adityasirohi in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 04:10 PM
  4. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  5. second largest array
    By bishnu in forum New To Java
    Replies: 2
    Last Post: 01-03-2009, 10:01 AM

Tags for this Thread

Posting Permissions

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