Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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
C 数组a[i]按降序打印,但我的逻辑是按升序打印_C_Arrays_Sorting - Fatal编程技术网

C 数组a[i]按降序打印,但我的逻辑是按升序打印

C 数组a[i]按降序打印,但我的逻辑是按升序打印,c,arrays,sorting,C,Arrays,Sorting,你能告诉我为什么我总是只按降序得到数组a[I]?请帮忙 for(i=0;i<10;i++) { for (j=0;j<10;j++) { if(a[i]>=a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } (i=0;i)的 你能告诉我为什么我总是只按降序得到数组a[I] 因为您实现了降序逻

你能告诉我为什么我总是只按降序得到数组
a[I]
?请帮忙

for(i=0;i<10;i++)  
{    
    for (j=0;j<10;j++)
    {   
       if(a[i]>=a[j])
       { 
          temp=a[i];
          a[i]=a[j];
          a[j]=temp;
       }
    }
}
(i=0;i)的

你能告诉我为什么我总是只按降序得到数组
a[I]

因为您实现了降序逻辑(比较)。更改

if(a[i]>=a[j])  


问题是排序运行两个完整循环,比较所有
i
j
对的
a
s,包括
i
大于
j
的循环(此时不应交换项目)。为了使选择排序*工作,它只需要从数组的未排序部分中选择其交换候选项

以下是如何修复您的实现:

for(i=0;i<10-1;i++) // You do not need to touch the last element, so end at 10-1
{    
    for (j=i+1;j<10;j++) // Make it j = i+1
    {   
       if(a[i] > a[j]) // >, not >= : no need to swap when two items are equal
       { 
          temp=a[i];
          a[i]=a[j];
          a[j]=temp;
       }
    }
}
for(i=0;i,not>=:当两个项目相等时,无需交换
{ 
温度=a[i];
a[i]=a[j];
a[j]=温度;
}
}
}

*是您正在实现的排序算法的别致名称。

1.不要惊慌。2.使用调试器。3.惊慌。您是否尝试更改比较?“但我的逻辑是升序”-不,不是。当你复制别人的代码而不了解它的工作原理时,就会发生这种情况。不,这段代码是我自己写的,但我犯了错误,在第二个循环中,它应该是我的j=i+1;嘿..我犯了我的错误…@Roddy;我不知道为什么人们不考虑逻辑就开始向下投票。我发布了完整的代码,并更正了我告诉abo的错误ut.现在你和所有的选民能解释一下这个代码有什么问题吗?@Priyanka很有兴趣理解为什么Haccks的答案是正确的!!hind是在交换
a[i]
之后,a[i]
会比
a[j]
更大。你正在安排
a[i]
对于所有
i=0
i=9
@Roddy这是正确的答案,我在comment@GrijeshChauhan;有时人们只是因为不理解程序的逻辑而开始投反对票:)。但我认为情况并非如此,因为这段代码太简单。可能还有其他原因:)+1了解此算法为何按相反顺序对数组排序实际上非常有启发性。结果表明,循环中
i
j
被“反转”的部分(即
j
小于
i
)只是反转数组的排序部分。
#include <stdio.h>

int main()
{
int a[10] = {3,15,9,4,15,65,0,2,1,1};
int temp; 
    for(int i=0;i<10;i++)
    {
          for (int j=0;j<10;j++)
          {
             if(a[j] > a[i])
             {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
             }
          }
     }

     for(int i=0;i<10;i++)
        printf("%d ", a[i]);
}  
Pass 1: 15   3   9   4  15  65   0   2   1   1  
        65   3   9   4  15  15   0   2   1   1      

Pass 2:  3  65   9   4  15  15   0   2   1   1  

Pass 3:  3   9  65   4  15  15   0   2   1   1  

Pass 4:  3   4  65   9  15  15   0   2   1   1  
         3   4   9  65  15  15   0   2   1   1  

Pass 5:  3   4   9  15  65  15   0   2   1   1  

Pass 6:  3   4   9  15  15  65   0   2   1   1      

Pass 7:  0   4   9  15  15  65   3   2   1   1  
         0   3   9  15  15  65   4   2   1   1  
         0   3   4  15  15  65   9   2   1   1  
         0   3   4   9  15  65  15   2   1   1  
         0   3   4   9  15  15  65   2   1   1 

Pass 8:  0   2   4   9  15  15  65   3   1   1  
         0   2   3   9  15  15  65   4   1   1  
         0   2   3   4  15  15  65   9   1   1  
         0   2   3   4   9  15  65  15   1   1  
         0   2   3   4   9  15  15  65   1   1  

Pass 9:  0   1   3   4   9  15  15  65   2   1  
         0   1   2   4   9  15  15  65   3   1  
         0   1   2   3   9  15  15  65   4   1  
         0   1   2   3   4  15  15  65   9   1  
         0   1   2   3   4   9  15  65  15   1  
         0   1   2   3   4   9  15  15  65   1  

Pass 10: 0   1   1   3   4   9  15  15  65   2  
         0   1   1   2   4   9  15  15  65   3  
         0   1   1   2   3   9  15  15  65   4  
         0   1   1   2   3   4  15  15  65   9  
         0   1   1   2   3   4   9  15  65  15  
         0   1   1   2   3   4   9  15  15  65  
for(i=0;i<10-1;i++) // You do not need to touch the last element, so end at 10-1
{    
    for (j=i+1;j<10;j++) // Make it j = i+1
    {   
       if(a[i] > a[j]) // >, not >= : no need to swap when two items are equal
       { 
          temp=a[i];
          a[i]=a[j];
          a[j]=temp;
       }
    }
}