1. Member Join Date
Mar 2014
Posts
7
Rep Power
0

## 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();
}
}  Reply With Quote

2. Just a guy Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## 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.  Reply With Quote

3. Member Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Bubble Sort Originally Posted by gimbal2 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.

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.  Reply With Quote

4. Just a guy Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## 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.  Reply With Quote

5. Member Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Bubble Sort Originally Posted by gimbal2 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.  Reply With Quote

6. Just a guy Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## 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.  Reply With Quote

7. Member Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Bubble Sort Originally Posted by gimbal2 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.  Reply With Quote

8. Just a guy Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## 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.  Reply With Quote

9. Senior Member Join Date
Feb 2014
Posts
447
Rep Power
7

## 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 with nums[1-1] (which is nums 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,  Reply With Quote

10. Member Join Date
Mar 2014
Posts
7
Rep Power
0

## Re: Bubble Sort Originally Posted by kneitzel 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 with nums[1-1] (which is nums 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,

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  Reply With Quote

#### Posting Permissions

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