bidirectional buble 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:20 UTC
Read the original article
Hit count: 160
algorithm
Here is the code I'm using for shacker sort (or bidirectional buble sort). Something is wrong; the 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]);
}
}
}
© Stack Overflow or respective owner