bidirectional bubble sort
        Posted  
        
            by davit-datuashvili
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by davit-datuashvili
        
        
        
        Published on 2010-05-18T20:10:28Z
        Indexed on 
            2010/05/18
            20:50 UTC
        
        
        Read the original article
        Hit count: 309
        
Here is the code for shacker sort or bidirectional bubble sort. Something is wrong. Error is
java.lang.ArrayIndexOutOfBoundsException   
Can anybody help me?
public class bidirectional{
  public static void main(String[]args){
    int x[]=new int[]{12,9,4,99,120,1,3,10};
    int j;
    int n=x.length;
    int st=-1;
    while (st<n){
      st++;
      n--;
      for (j=st;j<n;j++){
        if (x[j]>x[j+1]){
          int t=x[j];
          x[j]=x[j+1];
          x[j+1]=t;
        }
      }
      for (j=n;--j>=st;){
        if (x[j]>x[j+1]){
          int t=x[j];
          x[j]=x[j+1];
          x[j+1]=t;
        }
      }
    }
    for (int k=0;k<x.length;k++){
      System.out.println(x[k]);
    }
  }
}
thanks i have got result thanks guys i have accepted all answers
© Stack Overflow or respective owner