简体   繁体   English

二进制快速排序

[英]binary quicksort

I want to implement the Binary Quicksort algorithm from [Robert Sedgewick's book][1]. 我想实现[Robert Sedgewick的书] [1]中的Binary Quicksort算法。 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;
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],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;



}
}

i have changed it compiles but result is 4 8 9 3 7 2 maybe code is in correct in book can anybody help me to solve this problem? 我已经更改了它可以编译,但是结果是4 8 9 3 7 2也许代码在书中是正确的,有人可以帮助我解决这个问题吗?

shouldn't 不应该

while (digit(a[j],d)==1 && (j>i)) j++;

be

while (digit(a[j],d)==1 && (j>i)) j--;

?

With TJMonk15 correction (-- in the while). 使用TJMonk15校正(-期间)。
I've tried to execute and that's what happens 我试图执行,就是这样

The final array is 489372, with this "log": 最终数组是489372,带有以下“ log”:

Swapping (from left to right): 7 with 8 交换(从左到右):7与8
Swapping (from left to right): 3 with 3 交换(从左到右):3与3
Swapping (from left to right): 3 with 9 交换(从左到右):3与9
Swapping (from left to right): 3 with 3 交换(从左到右):3与3
Swapping (from left to right): 7 with 7 交换(从左到右):7与7

No swapping is correct according to me.. 根据我的说法,没有交换是正确的。

I don't understand why int j=r-1 and you use length-1 as r, then j is equal to length-2 at the beginning. 我不明白为什么int j=r-1而您将length-1用作r,那么j开头就等于length-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM