Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: array bubble sort

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default array bubble sort

    hello
    Okay, with this bit of code im working on listing a disorganized double array into ascending order. I used an example from my text book to run organize using bubble sort. Only issue is that when the computer prints out disorganized, it returns as [D@42e816

    import java.util.*;
    public class SortDoubles
    {
    public static void main(String[] args)
    {
    double[] disorganized = {0.19, 5.16, 45, 4.5, 32, 23.1, 6.52, 41, 15.3, 3.69, 12.35, 80, 62.2, 81.5, 75.6};
    double temp;
    int a;
    int b;


    a = disorganized.length;

    for(b = 0; b < (a - 1); ++b)
    {
    temp = disorganized[b];
    disorganized[b] = disorganized[b + 1];
    disorganized[b + 1] = temp;
    }
    System.out.print(disorganized[]);
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: array bubble sort

    Im still new to this. as Im reading my code Im thinking I need to include an if statement to check then switch the two numbers around inside of the for loop

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: array bubble sort

    Your code won't compile.

    If you use the name of an array in a print or println statement, it prints something about the array and a hashcode that is important to Java and to certain debuggers, but fairly meaningless to "normal" programs and "normal" programmers.

    If you want an easy to print the contents of an array, you can use the Arrays Class static "toString" method:
        System.out.println("Plain vanilla disorganized   : " + disorganized);
        System.out.println("Arrays.toString(disorganized): " + Arrays.toString(disorganized));

    Output
    Plain vanilla disorganized   : [D@e53108
    Arrays.toString(disorganized): [5.16, 45.0, 4.5, 32.0, 23.1, 6.52, 41.0, 15.3, 3.69, 12.35, 80.0, 62.2, 81.5, 75.6, 0.19]


    Reference: Java Arrays Class


    Of course you can do it with individual print statements in a loop if you want some control over formatting of the output (one array element per line, for example).

    Anyhow, whatever method you use to show the contents, you can now proceed with debugging the sort functionality.


    Cheers!

    Z

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Olympaphibian89 (November 1st, 2012)

  5. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: array bubble sort

    huzzah!!! I got it working. I just sat here and really stared at it for a while. then I ran into the problem of this just going through the list once and it would print.
    Posting this in case anyone else can use this to help em out.

    import java.util.*;
    public class SortDoubles
    {
    public static void main(String[] args)
    {
    double[] disorganized = {0.19, 5.16, 45, 4.5, 32, 23.1, 6.52, 41, 15.3, 3.69, 12.35, 80, 62.2, 81.5, 75.6};
    double temp;
    int a;
    int b;
    int c;


    a = disorganized.length;
    for(b = 0; b < (a - 1); ++b)
    for(c = 0; c < (a - 1); ++c)
    if(disorganized[c] > disorganized[c + 1])
    {
    temp = disorganized[c];
    disorganized[c] = disorganized[c + 1];
    disorganized[c + 1] = temp;
    }
    for(b = 0; b < (a - 1); ++b)
    System.out.print(disorganized[b] + ", ");
    }
    }

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: array bubble sort

    huzzah!!! I got it working. I just sat here and really stared at it for a while. then I ran into the problem of this just going through the list once and it would print.
    Posting this in case anyone else can use this to help em out.

    import java.util.*;
    public class SortDoubles
    {
    public static void main(String[] args)
    {
    double[] disorganized = {0.19, 5.16, 45, 4.5, 32, 23.1, 6.52, 41, 15.3, 3.69, 12.35, 80, 62.2, 81.5, 75.6};
    double temp;
    int a;
    int b;
    int c;


    a = disorganized.length;
    for(b = 0; b < (a - 1); ++b)
    for(c = 0; c < (a - 1); ++c)
    if(disorganized[c] > disorganized[c + 1])
    {
    temp = disorganized[c];
    disorganized[c] = disorganized[c + 1];
    disorganized[c + 1] = temp;
    }
    for(b = 0; b < (a - 1); ++b)
    System.out.print(disorganized[b] + ", ");
    }
    }

  7. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: array bubble sort

    Output:
    0.19, 3.69, 4.5, 5.16, 6.52, 12.35, 15.3, 23.1, 32.0, 41.0, 45.0, 62.2, 75.6, 80.0,


    Aren't you missing something in your printout? (What happened to 81.5?)


    Cheers!

    Z
    Last edited by Zaphod_b; November 1st, 2012 at 05:56 PM.

Similar Threads

  1. bubble sort problem please help!!
    By yanikapausini:) in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 24th, 2012, 04:15 PM
  2. bubble sort timer
    By gujinni in forum Other Programming Languages
    Replies: 1
    Last Post: October 15th, 2011, 09:30 AM
  3. Bubble Sort Int Array Problem
    By thisbeme in forum Collections and Generics
    Replies: 1
    Last Post: September 11th, 2011, 09:24 AM
  4. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 08:47 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM