循环读取数组输入意外行为?

时间:2015-06-22 14:09:58

标签: c arrays loops global-variables mergesort

我编写了一个递归合并排序程序,但它似乎在读取数组输入时卡住(循环从不终止),我不是专业人士,当谈到C时,我知道这是一个简单的尴尬错误,之后小时没有不好,请帮助:

//merge sort
#include<stdio.h>
#include<stdlib.h>

int a[20];

void mergeSort(int,int);
void merge(int,int,int);

void main()
{
//int a[20];
int n=0;
int i=0;
int x=0;

printf("Enter n");
scanf("%d",&n);

    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }

printf("Loop is over");

int low=0;
int high=n;

//start the sorting
mergeSort(low,high);

//done, print the result
//
for(i=0;i<n;i++)
{
printf("  %d",a[i]);
}

//return 0;
}//main ends

void mergeSort(int low,int high)
{
    while(low<high)
    {
    //split
    int mid=(low+high)/2;
    mergeSort(low,mid);
    mergeSort(mid+1,high);
    merge(low,mid,high);
    }
}//mergeSort ends


void merge(int low,int mid,int high)
{
//two lists , sort them
int b[50];

int i,k,h,j;//a[],b[],h->low,j->mid+1
i=0;k=0;h=low;j=mid+1;

while(h<=mid && j<high)
{
    if(a[h]<a[j]) 
    {
    b[i]=a[h];
    h++;
    }
    else
    {
    b[i]=a[j];
    j++;
    }

    i++;
}

if(h>mid)//left has emptied, copy whats left in right
{
    for(k=j;k<high;k++)
    {
    b[i]=a[k];
    i++;
    }
}

else//right has emptied
{
    for(k=h;k<=mid;k++)
    {
    b[i]=a[k];
    i++;
    }

}
//copy b to a

for(k=0;k<high;k++)
{
a[k]=b[k];
}


}//merge ends

3 个答案:

答案 0 :(得分:0)

添加以下检查,您应该能够找出

if(scanf("%d",&n) != 1)
{
  printf("Enter a valid integer\n");
  return 1;
}

if(n<0 || n>20)
{
  printf("Array range is 1 to 20");
  return 1;
}

for(i=0;i<n;i++)
{
  if(scanf("%d",&a[i]) !=1)
  printf("Scan failed\n");
}

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h>



void mergeSort(int,int);
void merge(int,int,int);

int a[20];

int  main()
{

int n;
int i;
int x=0;

printf("Enter n: ");
scanf("%d",&n); //this is working fine don't give n value more than or equal to 20 as you have declared an global array of size 20 

    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        printf("%d\n",i);
    }
printf("Loop is over\n");

//But after this the programme is not correct it is going in infinite time so above this all is fine but below the programme of merge sort is not correct 

我希望以下链接可以帮助您: - http://geeksquiz.com/merge-sort/

答案 2 :(得分:0)

#include<stdio.h>

int a[20];

void mergeSort(int low, int high);//[low .. high-1]
void merge(int low, int mid, int high);

int  main(void){
    int i, n=0;

    printf("Enter n:");
    scanf("%d", &n);

    for(i=0; i<n; i++){
        scanf("%d", &a[i]);
    }

    printf("Loop is over\n");

    int low=0;
    int high=n;

    mergeSort(low, high);

    for(i=0;i<n;i++)
        printf(" %d", a[i]);
    printf("\n");

    return 0;
}

void mergeSort(int low,int high){
    if(1 < high - low){
        int mid = (low+high)/2;
        mergeSort(low, mid);
        mergeSort(mid, high);
        merge(low, mid, high);
    }
}

void merge(int low, int mid, int high){
    int b[20];
    int i = 0, k, h = low, j = mid;

    while(h<mid && j<high){
        if(a[h]<a[j])
            b[i++]=a[h++];
        else
            b[i++]=a[j++];
    }
    if(h<mid)
        b[i++]=a[h++];
    if(j<high)
        b[i++]=a[j++];

    for(i=0, k=low; k<high; ++k, ++i){
        a[k]=b[i];
    }
}