binary quicksort
Posted
by davit-datuashvili
on Stack Overflow
See other posts from Stack Overflow
or by davit-datuashvili
Published on 2010-05-26T14:38:29Z
Indexed on
2010/05/26
14:41 UTC
Read the original article
Hit count: 169
hi i want implement Binary quicksort algorithm from robert sedgewick book it looks like this
public class quickb{
public static final int bitsword=32;
public static void quicksortB(int a[],int l,int r,int d){
int i=l;
int j=r-1;
if (r<=l || d>bitsword) return ;
while (j!=i)
{
while (digit(a[i],d)==0 && (i<j)) i++;
while (digit(a[j],d)==1 && (j>i)) j++;
int t=a[i];
a[i]=a[j];
a[j]=t;
}
if (digit(a[r-1],d)== 0) j++;
quicksortB(a,l,j-1,d+1);
quicksortB(a,j,r,d+1);
}
public static void main(String[]args){
int a[]=new int[]{4,7,3,9,8,2};
quicksortB(a,0,a.length-1,0);
for (int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
public static int digit(int m,int d){
return (m>>d)&1;
}
}
but it show me error:
java.lang.ArrayIndexOutOfBoundsException: 6
at quickb.quicksortB(quickb.java:13)
at quickb.main(quickb.java:32)
what is wrong?
© Stack Overflow or respective owner