Results 1 to 10 of 10

Thread: Bubble Sort

  1. #1
    riyazm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Bubble Sort

    Dear All,

    I am very new to Java technology, I would like seek your kind help on my quary. I am trying to find out the logic behind the Bubble sort, some how I am unable to get it due to lack of experience and knowledge in this.

    Since I am a begineer and I am trying to learn Java with help of this forum.

    Struggle part is


    for(a=1; a < size; a++)
    for(b=size-1; b >=a ; b--){
    if(nums[b-1] > nums[b]){
    t=nums[b-1];
    nums[b-1] = nums[b];
    nums[b]=t;
    ---------------------------------------------------------------------------------------

    Actual code is as follows :

    class Bubble {
    public static void main(String[] args){
    int nums[] = {6, 8, 2, 10, 5};
    int a, b, t;
    int size =5;
    System.out.println("Original array is ");
    for(int i = 0; i < size ; i++)
    System.out.println(" " + nums[i]);
    System.out.println();
    for(a=1; a < size; a++)
    for(b=size-1; b >=a ; b--){
    if(nums[b-1] > nums[b]){
    t=nums[b-1];
    nums[b-1] = nums[b];
    nums[b]=t;
    }
    }
    System.out.println("Sorted array is : ");
    for(int i=0; i < size; i++)
    System.out.println(" " + nums[i]);
    System.out.println();
    }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Bubble Sort

    And what kind of answers are you now expecting? I don't see what this has to do with learning Java to be honest, this is more to do with learning how specific algorithms work and that you can do without Java at all.

    Is there anything particular about this code that you do not understand?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    riyazm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Bubble Sort

    Quote Originally Posted by gimbal2 View Post
    And what kind of answers are you now expecting? I don't see what this has to do with learning Java to be honest, this is more to do with learning how specific algorithms work and that you can do without Java at all.

    Is there anything particular about this code that you do not understand?
    Dear gimbal2,

    In this code I am unable to understand the logic behind it, could you please it in a better manner so that I can grasp things with help of your explanation.

    Thanks for understanding.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Bubble Sort

    Yes but a forum is not a substitute tutor. Do you think anyone can explain in a forum post what whole chapters of books attempt to explain? Perhaps you should get a good book on software algorithms to help you understand.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    riyazm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Bubble Sort

    Quote Originally Posted by gimbal2 View Post
    Yes but a forum is not a substitute tutor. Do you think anyone can explain in a forum post what whole chapters of books attempt to explain? Perhaps you should get a good book on software algorithms to help you understand.
    Dear gimbal2,

    Thanks for your reply. I have tried 4 to 5 books but none of them have given much explanation on my questions. Could you please refere a book for me.

    Thanks.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Bubble Sort

    At this point I would rather suspect your study methods as the cause of your problems. If five books didn't help, a sixth one isn't going to either.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    riyazm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Bubble Sort

    Quote Originally Posted by gimbal2 View Post
    At this point I would rather suspect your study methods as the cause of your problems. If five books didn't help, a sixth one isn't going to either.
    Dear gimbal2,

    Thanks for your kind advice on my request. As I told I am unable to understand the concept in those books doesn't mean that Iam not interested in learning Java. I will keep try to read as many books as I find wherever it is.
    At last, World is not small right....! I will seek some other experts help who can guide me in a better manner.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Bubble Sort

    Okay, good luck! If you stick around perhaps another forum regular has something more constructive to add. We're all on different timezones here, the more patient people who are more experienced in guiding those who cannot explain themselves properly tend to post a few hours later than me.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Bubble Sort

    Hi,

    I understood, that you got the code of the bubblesort algorithm but you do not understand, how it works.

    So it is not a problem with Java. It is just understanding the algorithm itself.

    I will try to help you a little bit - but be aware that I am not a tutor and that even a forum might not be the best solution. Such things are much easier to show/explain when people meet.

    So algorithms are implemented in a programing language but from my experience they are best developed / explained on a piece of paper.

    So lets start - just let us write a small number of numbers on a piece of paper. I just did it with:

    4 3 7 6 5

    Now we want to sort it. So we could simply go through the numbers from right to left. We always take pairs and compare them. If the left one is bigger than the right one, we swap the numbers.

    So we start with the pair 6 5, 6 is greater than 5 so we have to swap them. We get
    4 3 7 5 6
    Now we continue with the next pair (One step to the left) so we have to handle the pair 7 5. 7 > 5 so we swap them:
    4 3 5 7 6
    Now we continue with the next pair (3 5) 3 is not > 5 so nothing to do.
    Now we continue with the next (last) pair: 4 3 - 4 > 3 so we swap them:
    3 4 5 7 6

    This is the first run through the numbers. The result of this first run is, that the smallest number is on the left side (the 3 in our example).

    So the left item is already on the correct place. For our next run, we only have to take care of the rest of the numbers:
    4 5 7 6

    So the array gets smaller and smaller with each run.

    So lets take the different code parts:

    t=nums[b-1];
    nums[b-1] = nums[b];
    nums[b]=t;
    this simply swaps the numbers in nums[b] and nums[b-1]

    if(nums[b-1] > nums[b])
    This is the check if the numbers must be swapped. (As we did on paper)

    for(a=1; a < size; a++)
    This is the main loop. The names are quite hard to read. a is the first element to check. The array as numbers from 0 to size-1.
    We always check with the previous element, so the whole array is handled. (So one check is the check of nums[1] with nums[1-1] (which is nums[0] of course).
    So this loop just tells which is the start of the array that we want to check.

    for(b=size-1; b >=a ; b--){
    This is now the loop that is doing the compares. We start at the left side (which element has the number size-1). And we go to the start of the element which we named a (see above chapter).

    So the core algorithm is that we go trough the array and move the smallest element to the left. And then redo the action till the remaining elements to be sorted is smaller than 2 (so no more check can be done).

    Just as a side note: I know the BubbleSort algorithm a little different. The BubbleSort algorithm that I am doing is doing the compare from left to right and that way moves the highest element to the right side. So the high numbers flow up like bubbles. And the array size is decreased on the right side and not on the left side of course. But the core algorithm is the same.

    I hope this helped a little bit to understand the algorithm.

    With kind regards,

    Konrad

  10. #10
    riyazm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Bubble Sort

    Quote Originally Posted by kneitzel View Post
    Hi,

    I understood, that you got the code of the bubblesort algorithm but you do not understand, how it works.

    So it is not a problem with Java. It is just understanding the algorithm itself.

    I will try to help you a little bit - but be aware that I am not a tutor and that even a forum might not be the best solution. Such things are much easier to show/explain when people meet.

    So algorithms are implemented in a programing language but from my experience they are best developed / explained on a piece of paper.

    So lets start - just let us write a small number of numbers on a piece of paper. I just did it with:

    4 3 7 6 5

    Now we want to sort it. So we could simply go through the numbers from right to left. We always take pairs and compare them. If the left one is bigger than the right one, we swap the numbers.

    So we start with the pair 6 5, 6 is greater than 5 so we have to swap them. We get
    4 3 7 5 6
    Now we continue with the next pair (One step to the left) so we have to handle the pair 7 5. 7 > 5 so we swap them:
    4 3 5 7 6
    Now we continue with the next pair (3 5) 3 is not > 5 so nothing to do.
    Now we continue with the next (last) pair: 4 3 - 4 > 3 so we swap them:
    3 4 5 7 6

    This is the first run through the numbers. The result of this first run is, that the smallest number is on the left side (the 3 in our example).

    So the left item is already on the correct place. For our next run, we only have to take care of the rest of the numbers:
    4 5 7 6

    So the array gets smaller and smaller with each run.

    So lets take the different code parts:

    t=nums[b-1];
    nums[b-1] = nums[b];
    nums[b]=t;
    this simply swaps the numbers in nums[b] and nums[b-1]

    if(nums[b-1] > nums[b])
    This is the check if the numbers must be swapped. (As we did on paper)

    for(a=1; a < size; a++)
    This is the main loop. The names are quite hard to read. a is the first element to check. The array as numbers from 0 to size-1.
    We always check with the previous element, so the whole array is handled. (So one check is the check of nums[1] with nums[1-1] (which is nums[0] of course).
    So this loop just tells which is the start of the array that we want to check.

    for(b=size-1; b >=a ; b--){
    This is now the loop that is doing the compares. We start at the left side (which element has the number size-1). And we go to the start of the element which we named a (see above chapter).

    So the core algorithm is that we go trough the array and move the smallest element to the left. And then redo the action till the remaining elements to be sorted is smaller than 2 (so no more check can be done).

    Just as a side note: I know the BubbleSort algorithm a little different. The BubbleSort algorithm that I am doing is doing the compare from left to right and that way moves the highest element to the right side. So the high numbers flow up like bubbles. And the array size is decreased on the right side and not on the left side of course. But the core algorithm is the same.

    I hope this helped a little bit to understand the algorithm.

    With kind regards,

    Konrad
    Dear Konrad,

    Thanks for valuable explanation on this algorithm. Its really helped me a lot. I really appreciate for your time and understanding on this issue.
    Once again thanks a lot and I like your attitude.

    Regards

Similar Threads

  1. Bubble Sort
    By Cruger in forum New To Java
    Replies: 2
    Last Post: 10-10-2013, 04:23 PM
  2. Bubble Sort For 2D Arrays
    By tmantonym in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 09:40 PM
  3. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 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
  •