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[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