C Ubuntu给了我错误的输出

C Ubuntu给了我错误的输出,c,algorithm,mergesort,C,Algorithm,Mergesort,我面临一个非常不寻常的问题。这是我使用merge sort制作的程序,用于打印重复次数最多的数字 #include<stdio.h> int n; int merge(int a[],int low,int mid,int high){ int i=low,j=mid+1,c[n],k=low; while((i<=mid) && (j<=high)){ if(a[i]<=a[j]){ c[k]

我面临一个非常不寻常的问题。这是我使用merge sort制作的程序,用于打印重复次数最多的数字

#include<stdio.h>
int n;
int merge(int a[],int low,int mid,int high){
    int i=low,j=mid+1,c[n],k=low;
    while((i<=mid) && (j<=high)){
        if(a[i]<=a[j]){
            c[k]=a[i];
            i=i+1;
            k=k+1;
        }

        else{
            c[k]=a[j];
            k=k+1;
            j=j+1;
        }
    }

    while(i<=mid){
        c[k]=a[i];
        i=i+1;
        k=k+1;
    }

    while(j<=high){
        c[k]=a[j];
        j=j+1;
        k=k+1;
    }

    for(i=low;i<=high;i++){
        a[i]=c[i];
    }
    return 0;
}


int mergeSort(int a[],int low,int high){
    int mid=0;
    if(low<high){
        mid=(low+high)/2;

        mergeSort(a,low,mid);
        mergeSort(a,mid+1,high);

        merge(a,low,mid,high);
    }
    return 0;
}

int main(){
    int i,a[n];
    printf("\nEnter the size of array:\n");
    scanf("%d",&n);
    printf("\nEnter the elements:\n");
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    mergeSort(a,0,n-1);
    printf("\nThe array after merge sort is-\n");
    for(i=0;i<n;i++){
        printf("%d ",a[i]);
    }
    i=0;
    int j=i+1,count=1,maxCount=1,flag=0,f=0;
    int arr1[n],arr2[n];
    //printf("%d ",n);
    while(i<(n-1) && j<n){
        j=i+1;
        while(a[i]==a[j]){
            count++;
            j++;
        }
        //printf("%d %d %d %d %d\n",count,a[i],a[j],i,j);
        if(count>1 && count>=maxCount){
            //printf("%d repeated %d times",a[i],count);
            arr1[f]=count;
            arr2[f]=a[i];
            flag=1;
            f++;
            maxCount=count;
        }
        i=j;
        count=1;
    }
    for(i=0;i<f;i++){
        if(arr1[i]==maxCount)
            printf("\n%d repeated %d times\n",arr2[i],arr1[i]);
    }
    if(flag==0){
        printf("\nNo repetitions\n");
    }
    return 0;
}
#包括
int n;
整数合并(整数a[],整数低,整数中,整数高){
int i=低,j=中+1,c[n],k=低;

而((i程序中至少有一个错误:

int i,a[n];
printf("\nEnter the size of array:\n");
scanf("%d",&n);
创建数组
a
时,尚未设置
n


n
(这是一个带有
extern
存储的变量)初始化为零,因此数组的大小将为零。由于您随后将项目放入其中,因此您将在其分配的空间之外存储数据,这是未定义的行为。任何情况都可能发生,并且在不同的平台上可能不同。

程序中至少有一个错误:

int i,a[n];
printf("\nEnter the size of array:\n");
scanf("%d",&n);
创建数组
a
时,尚未设置
n


n
(这是一个带有
extern
存储的变量)初始化为零,因此您的数组大小为零。既然您将项目放入其中,您将在其分配的空间之外存储数据,这是未定义的行为。任何情况都可能发生,并且在不同的平台上可能会有所不同。

为什么时间复杂性会影响输出???不同平台上的不同行为是…未定义行为的强烈标志。请仔细检查越界访问。OT:变量名(和参数名)应表示
内容
用法
。像
a[]
这样的名称是无意义的(与其他几个变量名一样),即使在当前上下文中也是如此OT:在调用任何
scanf()时
函数系列,始终检查返回值(而不是参数值)以确保操作成功。为什么时间复杂性会影响输出???不同平台上的不同行为是…未定义行为的强烈迹象。请仔细检查越界访问。OT:变量名(和参数名)应指示
内容
用法
。像
a[]
这样的名称(以及其他几个变量名)是没有意义的,即使在当前上下文中也是如此。t:调用任何
scanf()
函数族时,始终检查返回值(而不是参数值)确保手术成功非常感谢!非常有帮助,我非常感谢你。我非常沮丧,因为我找不到错误。再次感谢!:)非常感谢!非常有帮助,我非常感谢你。我非常沮丧,因为我找不到错误。再次感谢!:)