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