Results 1 to 10 of 10
Thread: Bubble Sort
 03042014, 11:49 AM #1Member
 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=size1; b >=a ; b){
if(nums[b1] > nums[b]){
t=nums[b1];
nums[b1] = 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=size1; b >=a ; b){
if(nums[b1] > nums[b]){
t=nums[b1];
nums[b1] = 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();
}
}
 03042014, 11:52 AM #2Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,605
 Rep Power
 6
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
 03042014, 11:58 AM #3Member
 Join Date
 Mar 2014
 Posts
 7
 Rep Power
 0
 03042014, 12:00 PM #4Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,605
 Rep Power
 6
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
 03042014, 12:11 PM #5Member
 Join Date
 Mar 2014
 Posts
 7
 Rep Power
 0
 03042014, 12:18 PM #6Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,605
 Rep Power
 6
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
 03042014, 12:29 PM #7Member
 Join Date
 Mar 2014
 Posts
 7
 Rep Power
 0
Re: Bubble Sort
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.
 03042014, 12:34 PM #8Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,605
 Rep Power
 6
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
 03042014, 01:56 PM #9Senior Member
 Join Date
 Feb 2014
 Posts
 447
 Rep Power
 2
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[b1];
nums[b1] = nums[b];
nums[b]=t;
this simply swaps the numbers in nums[b] and nums[b1]
if(nums[b1] > 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 size1.
We always check with the previous element, so the whole array is handled. (So one check is the check of nums[1] with nums[11] (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=size1; b >=a ; b){
This is now the loop that is doing the compares. We start at the left side (which element has the number size1). 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
 03042014, 03:49 PM #10Member
 Join Date
 Mar 2014
 Posts
 7
 Rep Power
 0
Similar Threads

Bubble Sort
By Cruger in forum New To JavaReplies: 2Last Post: 10102013, 04:23 PM 
Bubble Sort For 2D Arrays
By tmantonym in forum New To JavaReplies: 2Last Post: 11302011, 10:40 PM 
Bubble sort
By pineapple in forum New To JavaReplies: 3Last Post: 04252009, 12:45 AM 
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04292008, 08:04 PM 
need help with bubble sort
By lowpro in forum New To JavaReplies: 3Last Post: 12172007, 06:27 PM
Bookmarks