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 2 of 2

Thread: bubble sorting

  1. #1
    Junior Member stevierasilim's Avatar
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default bubble sorting

    can anyone help me ? i have an assignment about sorting alphabetically. i tried to search information on google, i found the codes, but i just cant understand. heres what i dont understand. please help
    public class Alphasortingbubble
    {
         public static void main(String[ ] args)
         {
                 String[ ] names = {"joe", "slim", "ed", "george"};
                 sortStringBubble (names);
                 for ( int k = 0;  k < 4;  k++ )
                    System.out.println( names [ k ] );
          }
     
          public static void sortStringBubble( String  names [ ] )
          {
                boolean flag = true;  // will determine when the sort is finished
                String temp;
     
                while ( flag )
                {
                      flag = false; // i dont understand why this is false
                      for (int i = 0;  i < names.length - 1;  i++ )
                      {
                              if ( names [ i ].compareTo( names [ i+1 ] ) > 0 )
                              {
    							  // ascending sort
     
    							/*i print this one and the result is number. i dont understand where the numbers come from
    							  System.out.println( "nama : " + names [ i ].compareTo( names [ i+1 ] )); */
     
                                          temp = names [ i ];
                                          names [ i ] = names [ i+1];     // swapping
                                          names [ i+1] = temp;
                                          flag = true; //why this is true
                               }
                       }
                }
          }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,536
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: bubble sorting

    How does bubble sort work, in your own words?

    You have to understand the principles behind what's going on before you dive into the code. I suggest looking at this page: Sorting Algorithm Animations
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    angstrem (May 30th, 2013)

Similar Threads

  1. Bubble Sorter and 2d Array
    By nwegman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 25th, 2013, 02:07 PM
  2. Bubble Sort Help
    By asundar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 24th, 2012, 05:14 PM
  3. bubble sort timer
    By gujinni in forum Other Programming Languages
    Replies: 1
    Last Post: October 15th, 2011, 10:30 AM
  4. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 09:47 PM
  5. Javascript Bubble Sort
    By Fidelacchius in forum What's Wrong With My Code?
    Replies: 17
    Last Post: May 24th, 2010, 05:43 PM

Tags for this Thread