Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Few question about BubbleSort

    Hello, i am doing the exercise on text book.
    Can anyone deal with my question?

    The following code is for bubble sort.

    public class BubbleSort {

      public void bubbleSort(Integer [] list) {

       boolean sorted = false;


       for (int top = list.length - 1; top > 0 && !sorted; top--) {

        sorted = true;

        for (int i = 0; i < top; i++) {

         if (_____fill in code here_______ ) {

          sorted = false;

           ...

           ... fill in code here

           ...
         }

        }

       }

      }

     }

    Question :
    Complete the codes provided and write an executable Java class BubbleSortTester which contains a static main method to create BubbleSort objects and pass the following array to perform the sorting. The testing program will print the array before and after the sorting. Show the program source code and the screen dumps of testing.

    11  2  8  3  6  15  12  0  7  4  1  13  5  9  14  10

    Sample output:
      The elements are
      11 2 8 3 6 15 12 0 7 4 1 13 5 9 14 10
      The sorted order is
      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  2. #2
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    The following code is what I've typed.
    I am totally lost within the code.


    public class BubbleSort {

    public void bubbleSort(Integer [] list){
    boolean sorted = false;

    for (int top = list.length - 1; top > 0 && !sorted; top--) {
    sorted = true;

    for (int i = 0; i < top; i++) {
    if ( list[i] > list[i+1] ) {
    sorted = false;
    int temp = list[i];
    list[i] = list[i+1];
    list[i+1] = temp;


    }

    }
    }
    }
    }


    public class BubbleSorTester {

    public static void main(String [] args) {
    int [] num = {11, 2, 8, 3, 6, 15, 12, 0, 7, 4, 1, 13, 5, 9, 14, 10 };
    asort(num);
    System.out.println("The element are \n");
    for (int i = 0; i < list.length; i++)
    System.out.print (list[i] + " " );
    System.out.println();
    }
    }

  3. #3
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Its a lot easier to see your logic if you indent your braces like this:

    Java Code:
    if (foo + bar == krump)
    {
    
        while (krump - bar== feelings)
        {
    
    
    
    
        }
    }

  4. #4
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Quote Originally Posted by ras_oscar View Post
    Its a lot easier to see your logic if you indent your braces like this:

    Java Code:
    if (foo + bar == krump)
    {
    
        while (krump - bar== feelings)
        {
    
    
    
    
        }
    }
    Do you mean the code like 1st post?
    I did it before I posted, but it seems not work 2nd post.
    By the way, can u fill in the code to let me familiarize it ??

  5. #5
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Java Code:
    public class BubbleSort {
    
      public void bubbleSort(Integer [] list) {
    
       boolean sorted = false;
    
    
       for (int top = list.length - 1; top > 0 && !sorted; top--) {
    
        sorted = true;
    
        for (int i = 0; i < top; i++) {
    
         if (_____fill in code here_______ ) {
    
          sorted = false;
    
           ...
    
           ... fill in code here
    
           ...
         }
    
        }
    
       }
    
      }
    
     }

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

    Default Re: Few question about BubbleSort

    I htink ras_oscar was thinking more about doing that for the code you'd written.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    I have already corrected the code.
    It can be compiled successfully. But, the code haven't sort.

    Java Code:
    public class BubbleSort {
    	
    public void bubbleSort(Integer [] list){
     boolean sorted = false;
    		
      for (int top = list.length - 1; top > 0 && !sorted; top--) {
       sorted = true;
    			
       for (int i = 0; i < top; i++) {
        if ( list[i-1] > list[i] ) {
         sorted = false;
         int temp = list[i];
         list[i] = list[i-1];
         list[i-1] = temp;
        }
       }
      }
     }
    }
    
    public class BubbleSorTester {
        
     public static void main(String [] args) {
      int list [] = new int [] {11, 2, 8, 3, 6, 15, 12, 0, 7, 4, 1, 13, 5, 9, 14, 10 };
            
       System.out.println("The element are ");
       for (int i = 0; i < list.length; i++){
        System.out.print (list[i] + " " );
       }
        
        BubbleSort access = new BubbleSort();
    
        System.out.println(" ");
            
        System.out.println("The sorted order is ");
        for(int i=0; i < list.length; i++){
         System.out.print(list[i] + " ");
                    
      }
     }
    }

  8. #8
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Few question about BubbleSort

    If it doesn't sort, what does it do?

  9. #9
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Quote Originally Posted by ras_oscar View Post
    If it doesn't sort, what does it do?
    Output:
    The elements are
    11 2 8 3 6 15 12 0 7 4 1 13 5 9 14 10
    The sorted order is
    11 2 8 3 6 15 12 0 7 4 1 13 5 9 14 10

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

    Default Re: Few question about BubbleSort

    Quote Originally Posted by ras_oscar View Post
    Its a lot easier to see your logic if you indent your braces like this: [ ... ]
    That is all in the eye of the beholder; I, for one, hate the style you're recommending; I use (proper) K&R style only; the style you mentioned is for folks from a Pascal world.

    kind regards,

    Jos

    ps. seriously though, as long as a style is used consistently, it is fine with me. Formatters can change the bracket positions for me after all ...
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    What i did wrong?

  12. #12
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Where do you pass the integer array to your sort routine?

    to josah: I've never even seen a pascal program. I did c++, but didn't realize there were different styles of indentation for each language, so I've learned something. Thanks. I find the format I illustrated is easier for me to see the statement nesting. I guess its what you've become accustomed to that matters. I'll give the "official" Java style a go nect time I gert lost in my code and see if it helps.
    Last edited by ras_oscar; 10-17-2013 at 08:13 PM.

  13. #13
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Quote Originally Posted by ras_oscar View Post
    Where do you pass the integer array to your sort routine?

    to josah: I've never even seen a pascal program. I did c++, but didn't realize there were different styles of indentation for each language, so I've learned something. Thanks. I find the format I illustrated is easier for me to see the statement nesting. I guess its what you've become accustomed to that matters. I'll give the "official" Java style a go nect time I gert lost in my code and see if it helps.
    I dont know.
    I just base on the code and run the program.
    was I missed to call the method??

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Few question about BubbleSort

    Calling a method to invoke the sort routine is relatively simple compared to writing the sort algorithm. So did you actually write this code yourself? Do you understand how it works?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Quote Originally Posted by jim829 View Post
    Calling a method to invoke the sort routine is relatively simple compared to writing the sort algorithm. So did you actually write this code yourself? Do you understand how it works?

    Regards,
    Jim
    yes I write it myself.
    in this case, how to call the method?
    please show it

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Few question about BubbleSort

    Your method is called bubbleSort. You have an instance of BubbleSort called access.

    So access.bubbleSort(list);

    is how you call it. I recommend you change your method signature to use int[] as opposde to Integer[].

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Quote Originally Posted by jim829 View Post
    Your method is called bubbleSort. You have an instance of BubbleSort called access.

    So access.bubbleSort(list);

    is how you call it. I recommend you change your method signature to use int[] as opposde to Integer[].

    Regards,
    Jim
    Java Code:
    public class BubbleSorTester {
        
        public static void main(String [] args) {
            int list [] = new int [] {11, 2, 8, 3, 6, 15, 12, 0, 7, 4, 1, 13, 5, 9, 14, 10 };
            
            System.out.println("The elements are ");
            for (int i = 0; i < list.length; i++){
                System.out.print(list[i] + " " );
            }
        
    
            bubbleSort access = new bubbleSort ();
            access.bubbleSort(list);
            
            
            System.out.println(" ");
            
            System.out.println("The sorted order is ");
          
            
            for(int i=0; i < list.length; i++){
                System.out.print(list[i] + " ");
                    
            }
        }
    }
    compile error happened

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Few question about BubbleSort

    Why did you change the following?


    bubbleSort access = new bubbleSort ();


    Put it the way it was. BubbleSort is the class name. bubbleSort is the method within the class. Also, it is not a good idea to name your methods the same as the class in which they reside.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Few question about BubbleSort-mfs2qau.png
    FINALLY I DID IT!!!!!
    THANK JIM
    YOU ARE KIND and Helpful

  20. #20
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Few question about BubbleSort

    Also, thank all of you who have answered my question, I love you guys!!!

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2012, 04:00 PM
  2. Bubblesort Issue within Method
    By burrish in forum New To Java
    Replies: 9
    Last Post: 09-27-2011, 02:39 AM
  3. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  4. Question with bubbleSort()
    By Metastar in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 05:55 PM
  5. Die Bubblesort
    By N3VRMND in forum New To Java
    Replies: 2
    Last Post: 11-06-2009, 03:44 PM

Posting Permissions

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