Bubblesort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NoviceJava
    New Member
    • Mar 2008
    • 14

    Bubblesort

    hello,

    I am having trouble with a program. It seems easy, but for some reason I am just not getting it. I need to create an array of random numbers and call a bubblesort method that I create myself. I then need to print the sorted array. I created the random array but I cant seem to figure out the sort method, which needs to follow in descending order. This is what I have:

    [CODE=java] public static int[] sorting(int vals[])
    {
    int i,j;
    int max[] = new int [vals.length];

    System.arraycop y(vals,0,max,0, vals.length);

    for (i = 1; i < vals.length; i++)
    {
    for (j = 1; j < max.length; j++)
    {
    if (vals[j-1] > vals[j])
    {
    max[j-1] = vals[j];
    max[j] = vals[j-1];
    }
    }
    }
    return max;
    }
    }[/CODE]

    The message I am getting is the random numbers (the number of numbers is defined by the user and i printed the numbers just to see if it worked) along with the message [I@1eed786.

    Please help,
    Thanks
    Last edited by Killer42; Apr 13 '08, 02:19 PM. Reason: Added CODE=java tag
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You have two problems:

    1) You are trying to print an array and get this: [I@1eed786; that's Java's way of
    printing arrays: [ == array, I == int ,1ee786 == the hash code of the array. If you
    want to print the contents of an array read the API documentation of the Arrays class.

    2) Your bubble sort algorithm is like a Swedish Nilfisk vacuum cleaner: it s*cks
    big times: it starts off nice by making a copy of the array but then you should
    use *only* the copy for the comparisons and swaps.

    kind regards,

    Jos

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      i woul prefer if you post the hole code so i can test it.
      i am not sure, but i think there is something with the sort algorithm (not sure). the i loop is good but with the j loop why dont you start with the last index of the array and go backward. it would be ;better.
      please post the hole code so i can test it.

      P.S. PLEASE USE THE CODE TAGS

      hsn

      Comment

      • hsn
        New Member
        • Sep 2007
        • 237

        #4
        a tip to save time. in the i loop and the j loop you are calling the length of the array in every time it loops. it is better to store the size in a variable and use the variable in the loop. you wont feel the difference. but trust me it matters.

        good luck

        hsn

        Comment

        • NoviceJava
          New Member
          • Mar 2008
          • 14

          #5
          [HTML]
          import java.io.*;

          public class Bubble
          {
          public static void main(String[] args)
          throws java.io.IOExcep tion
          {
          String s1, convert;
          int i, num;

          InputStreamRead er isr = new InputStreamRead er(System.in);

          BufferedReader br = new BufferedReader( isr);

          do
          {
          System.out.prin t("How many random numbers? ");

          s1 = br.readLine();

          num = Integer.parseIn t(s1);

          if (num > 9999 || num < 0)
          System.out.prin tln("Not valid.\n");
          }
          while(num > 9999 || num < 0);

          System.out.prin tln();

          int mkrand[] = new int [num];

          for(i = 0; i < num; i++)
          {
          mkrand[i] = 1 + (int)(Math.rand om()* 101);
          System.out.prin t(mkrand[i] + " ");
          }
          System.out.prin t(sort(mkrand)) ;

          }
          public static int[] sort(int vals[])
          {
          int i,j;
          int max[] = new int [vals.length];

          System.arraycop y(vals,0,max,0, vals.length);

          for (i = 1; i < vals.length; i++)
          {
          for (j = 1; j < max.length; j++)
          {
          if (vals[j-1] > vals[j])
          {
          max[j-1] = vals[j];
          max[j] = vals[j-1];
          }
          }
          }
          return max;
          }
          }[/HTML]


          Here it is

          Comment

          • NoviceJava
            New Member
            • Mar 2008
            • 14

            #6
            Originally posted by JosAH
            You have two problems:

            1) You are trying to print an array and get this: [I@1eed786; that's Java's way of
            printing arrays: [ == array, I == int ,1ee786 == the hash code of the array. If you
            want to print the contents of an array read the API documentation of the Arrays class.

            2) Your bubble sort algorithm is like a Swedish Nilfisk vacuum cleaner: it s*cks
            big times: it starts off nice by making a copy of the array but then you should
            use *only* the copy for the comparisons and swaps.

            kind regards,

            Jos
            I am not sure what you mean by API documentation. As you may tell, I'm pretty new to this stuff.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by NoviceJava
              I am not sure what you mean by API documentation. As you may tell, I'm pretty new to this stuff.
              API == Application Program Interface; the docs describe all the classes and their
              methods that come with your Java distribution. You should use them whereever
              you can and not reinvent the wheel. Here's a link.

              kind regards,

              Jos

              Comment

              • hsn
                New Member
                • Sep 2007
                • 237

                #8
                i am shocked that you don't know about the API. it is the first thing i have learned in java. sownload it, it will help you alot..


                hsn

                Comment

                • NoviceJava
                  New Member
                  • Mar 2008
                  • 14

                  #9
                  Originally posted by hsn
                  i am shocked that you don't know about the API. it is the first thing i have learned in java. sownload it, it will help you alot..


                  hsn
                  Ok, I'm on it. But did you see any thing else wrong with my code?

                  Comment

                  • hsn
                    New Member
                    • Sep 2007
                    • 237

                    #10
                    hello m8.
                    as Jos said your bubble sort algorithm ******. it is not correct for
                    88 23 70 6 it gave me
                    23 88 6 70
                    you should do some reading about this algorithm and understand it well. do some search and you will understand it it is one of the simplest algorithms.
                    and you can't say
                    Code:
                    System.out.print(sort(mkrand));
                    you have to go in a loop to print the array.

                    Good luck

                    hsn

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by hsn
                      you have to go in a loop to print the array.
                      No he doesn't; that's what me fIrst remark was about (see my first reply).
                      He could simply do this:

                      [code=java]
                      System.out.prin tln(Arrays.toSt ring(hisArray)) ;
                      [/code]

                      kind regards,

                      Jos

                      Comment

                      • hsn
                        New Member
                        • Sep 2007
                        • 237

                        #12
                        Originally posted by JosAH
                        No he doesn't; that's what me fIrst remark was about (see my first reply).
                        He could simply do this:

                        [code=java]
                        System.out.prin tln(Arrays.toSt ring(hisArray)) ;
                        [/code]

                        kind regards,

                        Jos
                        sorry m8 i didn't see that one

                        Comment

                        • NoviceJava
                          New Member
                          • Mar 2008
                          • 14

                          #13
                          Originally posted by hsn
                          sorry m8 i didn't see that one
                          I tried this:

                          System.out.prin tln(Arrays.toSt ring(sorting(mk rand)))

                          but I got the error message saying that it cannot find symbol
                          symbol: variable Arrays

                          Comment

                          • NoviceJava
                            New Member
                            • Mar 2008
                            • 14

                            #14
                            Originally posted by NoviceJava
                            I tried this:

                            System.out.prin tln(Arrays.toSt ring(sorting(mk rand)))

                            but I got the error message saying that it cannot find symbol
                            symbol: variable Arrays
                            Oh, I also created a new a new selection sort method:
                            [HTML]
                            public static int[] sorting(int vals[])
                            {
                            for (int i = 0; i < vals.length; i++)
                            {
                            int index = i;
                            for (int j = i+1; j < vals.length; j++)
                            if (vals[j] > vals[index])
                            index = j;

                            int largerNumber = vals[index];
                            vals[index] = vals[i];
                            vals[i] = largerNumber;
                            }
                            return vals;
                            }[/HTML]

                            Can you tell me if this will give me the desired results? I still can't figure out how to print the array. I tried what was suggested but it still did not work.

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by NoviceJava
                              I still can't figure out how to print the array. I tried what was suggested but it still did not work.
                              You have to import that class, just like every class not in the package java.lang.
                              This is how you do that: before you define any class add the line:

                              [code=java]
                              java.util.Array s;
                              [/code]

                              How did I know what package this Arrrays class is in? By reading the API docs.

                              kind regards,

                              Jos

                              Comment

                              Working...