
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.length1; 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

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.length1; 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

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
July 10th, 2013, 04:42 AM

Hi Cameron
thx for your help.
i rewrite 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<(n1); k++)
{
if (x[k1] > x[k])
{
tmp= x[k1];
x[k1]= x[k];
x[k]=tmp;
}
}
}
System.out.println("Array After Bubble Sort");
for( i=0; i < x.length; i++){
System.out.print(x[i] + " ");
}
}
}
July 10th, 2013, 05:11 AM

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:
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.
July 10th, 2013, 05:56 AM

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.
 Iterate through all the elements in a list
 Check the current element against the next element
 If the current element is smaller than the next element, then swap them
 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.
July 10th, 2013, 11:03 AM

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.