Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 用最小x<;对4个数字进行排序;比较_Arrays_Algorithm_Sorting - Fatal编程技术网

Arrays 用最小x<;对4个数字进行排序;比较

Arrays 用最小x<;对4个数字进行排序;比较,arrays,algorithm,sorting,Arrays,Algorithm,Sorting,这是一个面试问题。假设您有一个名为A的四个整数的数组,还有这个函数: int check(int x, int y){ if (x<=y) return 1; return 0; } int[4] B=new int[4]; /* The idea: put minimum values in even cells and maximum values in odd cells using check. Then swap (if needed) between mini

这是一个面试问题。假设您有一个名为A的四个整数的数组,还有这个函数:

int check(int x, int y){
   if (x<=y) return 1;
   return 0;
}
int[4] B=new int[4];
/* 
The idea: put minimum values in even cells and maximum values in odd cells using check. 
Then swap (if needed) between minimum values and also between maximum values.
And finally, swap the second element (max of minimums) 
and the third element (min of maximums) if needed.
*/
if (check(A[0],A[1])==1){ //A[0]<=A[1]
    B[0]=A[0];
    B[2]=A[1];
}
else{
    B[0]=A[1];
    B[2]=A[0];  
}
if (check(A[2],A[3])==1){ //A[2]<=A[3]
    B[1]=A[2];
    B[3]=A[3];
}
else{
    B[1]=A[3];
    B[3]=A[2]; 
}
if (check(B[0],B[1])==0){ //B[0]>B[1]
    swap(B[0],B[1]);
}
if (check(B[2],B[3])==0){ //B[2]>B[3]
    swap(B[2],B[3]);
}
if (check(B[1],B[2])==0){ // B[1]>B[2]
    swap(B[1],B[2]);
}

4元素列表有24种可能的排序。(4阶乘)如果只进行4次比较,则只能获得4位信息,这足以区分16种不同的情况,但不足以涵盖所有可能的输出情况。因此,5次比较是最佳最坏情况。

4元素列表有24种可能的排序。(4阶乘)如果只进行4次比较,则只能获得4位信息,这足以区分16种不同的情况,但不足以涵盖所有可能的输出情况。因此,5次比较是最佳最坏情况。

在计算机编程领域,p。183(第3.5.1节),Donald Knuth提供了以下最小比较次数的上下限表:

int[4] B=new int[4];
/* 
The idea: put minimum values in even cells and maximum values in odd cells using check. 
Then swap (if needed) between minimum values and also between maximum values.
And finally, swap the second element (max of minimums) 
and the third element (min of maximums) if needed.
*/
if (check(A[0],A[1])==1){ //A[0]<=A[1]
    B[0]=A[0];
    B[2]=A[1];
}
else{
    B[0]=A[1];
    B[2]=A[0];  
}
if (check(A[2],A[3])==1){ //A[2]<=A[3]
    B[1]=A[2];
    B[3]=A[3];
}
else{
    B[1]=A[3];
    B[3]=A[2]; 
}
if (check(B[0],B[1])==0){ //B[0]>B[1]
    swap(B[0],B[1]);
}
if (check(B[2],B[3])==0){ //B[2]>B[3]
    swap(B[2],B[3]);
}
if (check(B[1],B[2])==0){ // B[1]>B[2]
    swap(B[1],B[2]);
}

int[4] B=new int[4];
/* 
The idea: put minimum values in even cells and maximum values in odd cells using check. 
Then swap (if needed) between minimum values and also between maximum values.
And finally, swap the second element (max of minimums) 
and the third element (min of maximums) if needed.
*/
if (check(A[0],A[1])==1){ //A[0]<=A[1]
    B[0]=A[0];
    B[2]=A[1];
}
else{
    B[0]=A[1];
    B[2]=A[0];  
}
if (check(A[2],A[3])==1){ //A[2]<=A[3]
    B[1]=A[2];
    B[3]=A[3];
}
else{
    B[1]=A[3];
    B[3]=A[2]; 
}
if (check(B[0],B[1])==0){ //B[0]>B[1]
    swap(B[0],B[1]);
}
if (check(B[2],B[3])==0){ //B[2]>B[3]
    swap(B[2],B[3]);
}
if (check(B[1],B[2])==0){ // B[1]>B[2]
    swap(B[1],B[2]);
}
ceil(ln!)
是“信息论”的下限,而
B(n)
是插入二进制排序中的最大比较数。由于
n=4
的上下限相等,因此需要进行5次比较

int[4] B=new int[4];
/* 
The idea: put minimum values in even cells and maximum values in odd cells using check. 
Then swap (if needed) between minimum values and also between maximum values.
And finally, swap the second element (max of minimums) 
and the third element (min of maximums) if needed.
*/
if (check(A[0],A[1])==1){ //A[0]<=A[1]
    B[0]=A[0];
    B[2]=A[1];
}
else{
    B[0]=A[1];
    B[2]=A[0];  
}
if (check(A[2],A[3])==1){ //A[2]<=A[3]
    B[1]=A[2];
    B[3]=A[3];
}
else{
    B[1]=A[3];
    B[3]=A[2]; 
}
if (check(B[0],B[1])==0){ //B[0]>B[1]
    swap(B[0],B[1]);
}
if (check(B[2],B[3])==0){ //B[2]>B[3]
    swap(B[2],B[3]);
}
if (check(B[1],B[2])==0){ // B[1]>B[2]
    swap(B[1],B[2]);
}
信息论界是通过认识到存在
n唯一项目的可能顺序。我们通过问
S
yes-no问题来区分这些情况,在计算机编程艺术中,以
is X的形式,p。183(第3.5.1节),Donald Knuth提供了以下最小比较次数的上下限表:

int[4] B=new int[4];
/* 
The idea: put minimum values in even cells and maximum values in odd cells using check. 
Then swap (if needed) between minimum values and also between maximum values.
And finally, swap the second element (max of minimums) 
and the third element (min of maximums) if needed.
*/
if (check(A[0],A[1])==1){ //A[0]<=A[1]
    B[0]=A[0];
    B[2]=A[1];
}
else{
    B[0]=A[1];
    B[2]=A[0];  
}
if (check(A[2],A[3])==1){ //A[2]<=A[3]
    B[1]=A[2];
    B[3]=A[3];
}
else{
    B[1]=A[3];
    B[3]=A[2]; 
}
if (check(B[0],B[1])==0){ //B[0]>B[1]
    swap(B[0],B[1]);
}
if (check(B[2],B[3])==0){ //B[2]>B[3]
    swap(B[2],B[3]);
}
if (check(B[1],B[2])==0){ // B[1]>B[2]
    swap(B[1],B[2]);
}

int[4] B=new int[4];
/* 
The idea: put minimum values in even cells and maximum values in odd cells using check. 
Then swap (if needed) between minimum values and also between maximum values.
And finally, swap the second element (max of minimums) 
and the third element (min of maximums) if needed.
*/
if (check(A[0],A[1])==1){ //A[0]<=A[1]
    B[0]=A[0];
    B[2]=A[1];
}
else{
    B[0]=A[1];
    B[2]=A[0];  
}
if (check(A[2],A[3])==1){ //A[2]<=A[3]
    B[1]=A[2];
    B[3]=A[3];
}
else{
    B[1]=A[3];
    B[3]=A[2]; 
}
if (check(B[0],B[1])==0){ //B[0]>B[1]
    swap(B[0],B[1]);
}
if (check(B[2],B[3])==0){ //B[2]>B[3]
    swap(B[2],B[3]);
}
if (check(B[1],B[2])==0){ // B[1]>B[2]
    swap(B[1],B[2]);
}
ceil(ln!)
是“信息论”的下限,而
B(n)
是插入二进制排序中的最大比较数。由于
n=4
的上下限相等,因此需要进行5次比较

int[4] B=new int[4];
/* 
The idea: put minimum values in even cells and maximum values in odd cells using check. 
Then swap (if needed) between minimum values and also between maximum values.
And finally, swap the second element (max of minimums) 
and the third element (min of maximums) if needed.
*/
if (check(A[0],A[1])==1){ //A[0]<=A[1]
    B[0]=A[0];
    B[2]=A[1];
}
else{
    B[0]=A[1];
    B[2]=A[0];  
}
if (check(A[2],A[3])==1){ //A[2]<=A[3]
    B[1]=A[2];
    B[3]=A[3];
}
else{
    B[1]=A[3];
    B[3]=A[2]; 
}
if (check(B[0],B[1])==0){ //B[0]>B[1]
    swap(B[0],B[1]);
}
if (check(B[2],B[3])==0){ //B[2]>B[3]
    swap(B[2],B[3]);
}
if (check(B[1],B[2])==0){ // B[1]>B[2]
    swap(B[1],B[2]);
}

信息论界是通过认识到存在
n唯一项目的可能顺序。我们通过询问
S
yes-no以
is-XThanks的形式提问来区分这些情况。你能解释为什么4个比较给我4个信息位吗?这对我来说是有意义的,但我不知道如何才能得到它。一点是一个是非信息。每次比较都会让你问一个是非问题:答案是X吗。你能解释为什么4个比较给我4个信息位吗?这对我来说是有意义的,但我不知道如何才能得到它。一点是一个是非信息。每次比较都允许你问一个是非问题:X是吗