wangcham233 2021-12-04 10:46 采纳率: 50%
浏览 21
已结题

c语言冒泡排序:我在做选择排序但是我不知道为什么他前面会给我一个奇怪的数字?

这是题目:

img

#include <stdio.h>
int bubble(int *p,int j;               //"bubble" means using bubble sort 
int main()
{
    int number,i;int z;        //"number" is the number you want
    scanf("%d",&number);
    int ch[number];           //q is quantity of the array"ch"
    for(i=0;i<number;i++)       //i is used to put the number into array 
    {
        scanf("%d",&ch[i]);
    }
    bubble(ch,number);     // make the array to the function "ch"
    for(z=0;z<number;z++)
    {
        printf("%d",ch[z]);
        if(z!=(number-1))
        {
            printf(" ");      //do the " " thing
        }
    }
    
    return 0;
 } 
 int bubble(int *p,int j)
 {    
 int x,y;                   //these variable are used to do the bubble sort
 int t;                         //t is used to exchange variables
     for(x=0;x<j;x++)
     {
         for(y=0;y<=j-x-1;y++)
         {
             if(p[y]<=p[y+1])
             {
                 t=p[y+1];
                 p[y+1]=p[y];
                 p[y]=t;
             }
         }
     }
 }

两个例子,为什么会有差别?
对的:

img

错的:

img

  • 写回答

2条回答 默认 最新

  • 关注

    (1)
    int bubble(int *p,int j; 这里,你最后漏了一个)
    应该是:

    int bubble(int *p,int j); 
    
    

    (2)C语言不支持变量定义数组长度,题目说明n不大于10 了,数组直接用int ch[10];就可以了。
    (3)冒泡排序内循环错误,for(y=0;y<=j-x-1;y++)应该是:

    for(y=0;y<j-x-1;y++) //不是<=是<
    
    

    冒泡排序的外循环:for(x=0;x<j;x++)最好改成for(x=0;x<j-1;x++)

    代码修改如下:

    #include <stdio.h>
    int bubble(int *p,int j);               //"bubble" means using bubble sort 
    int main()
    {
        int number,i;
        int z;        //"number" is the number you want
        int ch[10];  
    
        scanf("%d",&number);
        
        for(i=0;i<number;i++)       //i is used to put the number into array 
        {
            scanf("%d",&ch[i]);
        }
        bubble(ch,number);     // make the array to the function "ch"
        for(z=0;z<number;z++)
        {
            printf("%d",ch[z]);
            if(z!=(number-1))
            {
                printf(" ");      //do the " " thing
            }
        }
        return 0;
    } 
    int bubble(int *p,int j)
    {    
        int x,y;                   //these variable are used to do the bubble sort
        int t;                         //t is used to exchange variables
        for(x=0;x<j-1;x++) //这里最好用x<j-1,虽然用x<j也可以,但是在有些平台提交的时候会报错
        {
            for(y=0;y<j-x-1;y++)
            {
                if(p[y]<=p[y+1])
                {
                    t=p[y+1];
                    p[y+1]=p[y];
                    p[y]=t;
                }
            }
        }
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月12日
  • 已采纳回答 12月4日
  • 创建了问题 12月4日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料