Thread: Bubble sort

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Bubble sort


    Hi

    i am new to java. i was trying to write a code for bubble sort.
    i understand the bubble sort completely but not able to implement in code for. please help me


    below is the code which i wrote.

    Code:
    public class bubble {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            int [] x = new int[3];
            x[0] = 50;
            x[1] = 30;
            x[2] = 10;
           
            int t, k ;
            for (t =0; t< x.length-1; t++)  
            {
            System.out.println("XT is " + x[t]);
            
            if (x[t]> x[t+1])
            {
                
                k= x[t+1];
                x[t+1]= x[t];
                x[t] = k;
            }
                      
            }
        
        }
    }
    regards,
    japper
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by japper
    Hi

    i am new to java. i was trying to write a code for bubble sort.
    i understand the bubble sort completely but not able to implement in code for. please help me


    below is the code which i wrote.

    Code:
    public class bubble {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            int [] x = new int[3];
            x[0] = 50;
            x[1] = 30;
            x[2] = 10;
           
            int t, k ;
            for (t =0; t< x.length-1; t++)  
            {
            System.out.println("XT is " + x[t]);
            
            if (x[t]> x[t+1])
            {
                
                k= x[t+1];
                x[t+1]= x[t];
                x[t] = k;
            }
                      
            }
        
        }
    }
    regards,
    japper







    please help me
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    If you fully understood how bubble sort worked, then you would be able to implement it. My suggestion is to take a piece of paper and take an array of values through the bubble sort algorithm. That way you will gain an intimate understanding of what is happening at every step. From there you should be able to program it.

    When you start writing your implementation take it one step at a time. Make sure your bubble sort does one thing properly, then add another thing until you get your final result.

    The algorithm and examples can be found here: http://en.wikipedia.org/wiki/Bubble_sort
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0
    Hi Cameron

    thx for your help.

    i re-write the code code and it seems that i am much closer.

    here, in my code sorting is done only 1 time and i have no idea where i am wrong in my code .

    can you pls have a look and pin point the area where my logic is wrong.


    below is the o/p of my code.
    Code:
    run:
    length of array X is ..3
    Value of n is.. 3
    unsorted array list is mentioned as below
    50 30 10 Array After Bubble Sort
    30 50 10 BUILD SUCCESSFUL (total time: 1 second)

    code mentioned as below.

    Code:
    public class bubble2 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            int i,j,k,n,tmp;
            int x[]= {50,30,10};
            n= (x.length);
            System.out.println("length of array X is .."  + (x.length));
            System.out.println("Value of n is.. " + n);
            System.out.println("unsorted array list is mentioned as below");
            for(i=0;i<x.length;i++)
            {
                System.out.print(x[i] + " ");
            }
            for(j=0;j<n;j++)
            {    
             for(k=1;k<(n-1); k++)
             {    
             if (x[k-1] > x[k])   
             {
                 tmp= x[k-1];
                 x[k-1]= x[k];
                 x[k]=tmp;
             }       
            }
            }
           System.out.println("Array After Bubble Sort");
                    for( i=0; i < x.length; i++){
                            System.out.print(x[i] + " ");
                    }
        }
    }
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    the problem is that you do not understand bubble sort. Your code may kinda sorta resemble some parts of bubble sort, but you do not understand the overall principle.

    You basically have two options now: You can keep fumbling with the code and hope that you'll magically end up with bubble sort. Or you can do what Cameron0960 already suggested: Forget about the code for a second, sit down with a piece of paper and go through the algorithm. That's what I'd suggest as well. Don't start to write code before you know exactly what you need to do.

    Given this sequence of numbers:

    Code:
    5  2  6  10  1
    How would you sort them with bubble sort? I'm not talking about Java or whatever language you happen to use, just the general principle.

    When you really understand the procedure, it won't be difficult to translate it into code.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Your doing some things right, but there are some major logic flaws in what you are writing.

    I'm curious as to why you are using nested for loops, but aren't using the index of the outer for loop. Explained in another way, why aren't you using the variable j in any of your logic? Maybe this goes back to not actually understanding how bubble sort works.

    Let me explain the algorithm to you.
    1. Iterate through all the elements in a list
    2. Check the current element against the next element
    3. If the current element is smaller than the next element, then swap them
    4. Repeat for the length of the list - 1


    That's the whole algorithm. My advice is to get the algorithm to work with one single iteration through the list. After that you can add in the second loop, but get that first part working.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2
    Here's a really good video on bubble sort (and channel about algorithms):
    Code:
    http://www.youtube.com/watch?v=P00xJgWzz2c
    Although Bubble Sort is almost never used in the real world nowadays, it is often an introductory lesson in sorting.

    I think understand how it could be sometimes difficult to translate theoretical knowledge into code. I experience the same and it is frustrating. I get the logic on pen and paper, but I get stumped on how to tell the computer to do this and that (syntax).

    @OP:
    At least you are not trying to cheat and interested to actually learn it. Keep it up.

IMN logo majestic logo threadwatch logo seochat tools logo