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

(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;
}


不好意思删注释去了半个括号求补上~