Link to home
Start Free TrialLog in
Avatar of tttntt
tttntt

asked on

how to do a buble sort animation using java?

for(int x = 0; x < array.length - 1; x++){
    for(int y = 0; y < array.length - 2; y ++){
         if(array[y] > array[y+1]){
            swap(array, y, y+1);
         }
         repaint(); // ask to repaint the component
     }
}

if code is on above, but i can't get the result i expect. then repaint() paint the sorted array.
my expected result is to paint the array that is not sorted yet.
how!? help!!
Avatar of Mayank S
Mayank S
Flag of India image

That is definitely not the loop for bubble-sort, and I still don't understand your question properly. Do you want to first display the unsorted array, then sort it, and then display the sorted array again?

Mayank.
ASKER CERTIFIED SOLUTION
Avatar of funnyveryfunny
funnyveryfunny

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of funnyveryfunny
funnyveryfunny

Hi mayankeagle,

My code is correct but not efficient like yrs, given the time complexity of bubble sort is O(n^2). So logically I thought it would go something like that.

You're right.

Anyway, I just wrote a little code for the animation here using your version. How creative are you???

public class BubbleSortAnimation extends Frame{
  int[] data;
  Button[] b_data;
  final int MAX =10;
  Panel p;
  Label l;

  public BubbleSortAnimation() {
    data = new int[MAX];
    b_data = new Button[MAX];
    p = new Panel(new FlowLayout());
    Random r = new Random();
    int temp;
    for(int i=0;i<MAX;i++){
      temp = r.nextInt(100);
      data[i]=temp;
      b_data[i] = new Button(Integer.toString(temp));
      b_data[i].setEnabled(false);
      b_data[i].setBackground(Color.white);
      p.add(b_data[i]);
    }
    l = new Label();
    this.add("Center",p);
    this.add("South",l);
    this.setSize(600,100);
    this.setVisible(true);
  }

  public void bubbleSort() throws Exception{
    l.setText("Sorting....");
    repaint();
    for(int i=1;i<MAX;i++)
      for(int j=0;j<MAX-i;j++){
         b_data[j].setBackground(Color.green);
         b_data[j+1].setBackground(Color.yellow);
         repaint();
         Thread.sleep(500);
         if(data[j]>data[j+1]){
           int temp = data[j];
           data[j] = data[j+1];
           data[j+1] = temp;
           b_data[j].setBackground(Color.yellow);
           b_data[j].setLabel(Integer.toString(data[j]));
           b_data[j+1].setBackground(Color.green);
           b_data[j+1].setLabel(Integer.toString(data[j+1]));
           repaint();
           Thread.sleep(500);
         }
         b_data[j].setBackground(Color.white);
         b_data[j+1].setBackground(Color.white);
         Thread.sleep(500);
      }
    l.setText("Sorted!!!");
    repaint();
  }

  public static void main(String[] args) {
    BubbleSortAnimation b = new BubbleSortAnimation();
    try{
      b.bubbleSort();
    }
    catch(Exception e){
      System.out.println(e);
    }
  }
}

Bye.
His code is correct.  Remember C arrays index from 0 to Length-1  So Arr[6] has indexes 0, 1, 2 , 3 , 4 , 5

The code provided by funnyveryfunny and mayankeagle will step outside the array boundaries when y = length-1

mlmcc
To mlmcc,

>>>"The code provided by funnyveryfunny and mayankeagle will step outside the array boundaries when y = length-1"

First of all your statement is correct in its respect but I think you're confused here, y only reaches y = length-2 so it'll never step outside the upper-boundary. To support yr statement the code will now have to be like this:

for(int x = 0; x <= array.length - 1; x++){
   for(int y = 0; y <= array.length - 2; y ++){
        if(array[y] > array[y+1]){
           swap(array, y, y+1);
        }
    }
}

Otherwise the last element will never be sorted unless yr assuming item[y]<=item[length-1] for all y=0..length-2.

fvf.
mlmcc ,

>> The code provided by funnyveryfunny and mayankeagle will step outside the array boundaries when y = length-1

Oh ya??

WELL, 'y' WILL NEVER BE 'LENGTH - 1' IF YOU FOLLOW MY CODE,

because 'x' starts from 1 in my loop and goes till n - 1 (n-2 iterations for the outer loop - standard bubble-sort), and so, the maximum value of 'y' ( y < array.length - x ) will be when 'x' is 1, i.e., 'array.length - 2', meaning that (y + 1) in the 'if' statement will be array.length - 1, and so, both, 'y' and 'y + 1' will be within the array's bounds!

Please verify your own comments before posting them and contradicting somebody else.

Mayank.
tttntt:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Split points between mayankeagle and funnyveryfunny.
Ok with me.
Me too :-)